RecognizeImage.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div ref="arbitrateImgRef" class="arbitrate-img" @scroll="onImgScroll">
  3. <div class="arbitrate-img-box">
  4. <img ref="imgRef" :src="imgUri" alt="扫描结果" @load="onImgLoad" />
  5. <div class="img-recogs">
  6. <div class="recog-block is-active" :style="fillAreaStyle">
  7. <div
  8. v-for="(option, oindex) in fillOptionStyles"
  9. :key="oindex"
  10. :style="option"
  11. class="recog-item"
  12. ></div>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. <div ref="imgThumbRef" class="arbitrate-img-thumb">
  18. <img :src="imgUri" alt="扫描结果" />
  19. <div
  20. class="arbitrate-img-area"
  21. v-ele-move-directive.prevent.stop="{
  22. moveStart: moveAreaStart,
  23. moveElement: moveArea,
  24. moveStop: moveAreaStop,
  25. }"
  26. :style="areaStyle"
  27. ></div>
  28. </div>
  29. </template>
  30. <script setup lang="ts">
  31. import { computed, ref, reactive, onMounted, nextTick, watch } from "vue";
  32. import { vEleMoveDirective } from "@/directives/eleMove";
  33. import { getFileUrl } from "@/utils/tool";
  34. import { RecognizeArbitrateTaskDetail } from "@/ap/types/recognizeCheck";
  35. import { useUserStore } from "@/store";
  36. defineOptions({
  37. name: "RecognizeImage",
  38. });
  39. const props = defineProps<{
  40. recogData: RecognizeArbitrateTaskDetail;
  41. }>();
  42. const userStore = useUserStore();
  43. const arbitrateImgRef = ref();
  44. const imgRef = ref();
  45. const imgUri = computed(() => {
  46. return getFileUrl(props.recogData.uri);
  47. });
  48. // img
  49. function updateImgAreaSize() {
  50. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  51. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  52. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  53. const areaRate = Math.min(imgBoxDom.clientHeight / imgDom.clientHeight, 1);
  54. const areaHeight = clientHeight * areaRate;
  55. areaSize.height = areaHeight;
  56. }
  57. const fillAreaStyle = ref({} as Record<string, any>);
  58. const fillOptionStyles = ref([] as Array<Record<string, any>>);
  59. function updateRecogStyle() {
  60. const imgDom = imgRef.value as HTMLImageElement;
  61. const rate = imgDom.clientWidth / imgDom.naturalWidth;
  62. const { unfillColor, fillColor, borderWidth } = userStore.recogFillSet;
  63. const curBorderWidth = Math.max(1, borderWidth * rate);
  64. fillAreaStyle.value = {
  65. position: "absolute",
  66. left: `${props.recogData.fillArea.x * rate}px`,
  67. top: `${props.recogData.fillArea.y * rate}px`,
  68. width: `${props.recogData.fillArea.w * rate}px`,
  69. height: `${props.recogData.fillArea.h * rate}px`,
  70. zIndex: 9,
  71. };
  72. if (props.recogData.type !== "question") {
  73. fillOptionStyles.value = [];
  74. return;
  75. }
  76. fillOptionStyles.value = props.recogData.optionSizes.map((op) => {
  77. const opStyle = {
  78. position: "absolute",
  79. left: `${op.x * rate}px`,
  80. top: `${op.y * rate}px`,
  81. width: `${op.w * rate}px`,
  82. height: `${op.h * rate}px`,
  83. zIndex: 9,
  84. border: "",
  85. };
  86. if (op.filled) {
  87. opStyle.border = `${curBorderWidth}px solid ${fillColor}`;
  88. } else {
  89. opStyle.border = `${curBorderWidth}px solid ${unfillColor}`;
  90. }
  91. return opStyle;
  92. });
  93. }
  94. function onImgLoad() {
  95. updateImgAreaSize();
  96. nextTick(() => {
  97. updateRecogStyle();
  98. });
  99. }
  100. watch(
  101. () => props.recogData,
  102. (val, oldval) => {
  103. if (val && oldval && val.uri === oldval.uri) {
  104. updateRecogStyle();
  105. }
  106. }
  107. );
  108. let areaIsMoving = false;
  109. function onImgScroll(e: Event) {
  110. if (areaIsMoving) {
  111. e.preventDefault();
  112. return;
  113. }
  114. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  115. if (!imgBoxDom) return;
  116. const scrollTop = imgBoxDom.scrollTop;
  117. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  118. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  119. areaSize.top = (clientHeight * scrollTop) / imgDom.clientHeight;
  120. }
  121. // img-thumb area
  122. const imgThumbRef = ref();
  123. const areaSize = reactive({
  124. height: 40,
  125. top: 0,
  126. });
  127. const areaOriginTop = ref(0);
  128. const areaStyle = computed(() => {
  129. return { height: `${areaSize.height}px`, top: `${areaSize.top}px` };
  130. });
  131. interface MovePos {
  132. left: number;
  133. top: number;
  134. }
  135. function moveAreaStart() {
  136. areaOriginTop.value = areaSize.top;
  137. areaIsMoving = false;
  138. }
  139. function moveArea(pos: MovePos) {
  140. areaIsMoving = true;
  141. const areaTop = pos.top + areaOriginTop.value;
  142. const { clientHeight } = imgThumbRef.value as HTMLDivElement;
  143. areaSize.top = Math.min(areaTop, clientHeight - areaSize.height);
  144. areaSize.top = Math.max(0, areaSize.top);
  145. const imgBoxDom = arbitrateImgRef.value as HTMLDivElement;
  146. const imgDom = imgBoxDom.firstChild as HTMLImageElement;
  147. imgBoxDom.scrollTop = (imgDom.clientHeight * areaSize.top) / clientHeight;
  148. }
  149. function moveAreaStop(pos: MovePos) {
  150. moveArea(pos);
  151. areaIsMoving = false;
  152. }
  153. </script>
  154. <style lang="less" scoped>
  155. .arbitrate-img {
  156. .recog-block {
  157. &:hover {
  158. background-color: rgba(241, 214, 110, 0.3);
  159. }
  160. &.is-active {
  161. background-color: rgba(241, 214, 110, 0.3);
  162. &::after {
  163. content: "";
  164. display: block;
  165. position: absolute;
  166. z-index: 1;
  167. top: 0;
  168. left: 0;
  169. right: 0;
  170. bottom: 0;
  171. border: 1px dashed #000;
  172. }
  173. }
  174. }
  175. }
  176. </style>