index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <div ref="elRef" class="scan-image">
  3. <div
  4. class="img-body"
  5. :style="imageStyle"
  6. v-ele-move-directive.prevent.stop="{
  7. moveElement: onMoveImg,
  8. emitOriginLeftTop: true,
  9. }"
  10. >
  11. <img
  12. ref="imgRef"
  13. v-if="curPage"
  14. :src="getFileUrl(curPage.sheetUri)"
  15. alt="原图"
  16. @load="initImageSize"
  17. />
  18. <div class="img-recogs">
  19. <div
  20. v-for="(item, index) in recogBlocks"
  21. :key="index"
  22. class="recog-block"
  23. :style="item.fillAreaStyle"
  24. @click="onAreaClick(item)"
  25. >
  26. <div
  27. v-for="(option, oindex) in item.fillOptionStyles"
  28. :key="oindex"
  29. :style="option"
  30. class="recog-item"
  31. ></div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="img-guide">
  36. <div class="img-guide-icon is-left" @click="onPrev"><LeftOutlined /></div>
  37. <div class="img-guide-icon is-right" @click="onNext">
  38. <RightOutlined />
  39. </div>
  40. </div>
  41. <div class="img-actions">
  42. <ul>
  43. <li @click="onZoomIn"><ZoomInOutlined /></li>
  44. <li @click="onZoomOut"><ZoomOutOutlined /></li>
  45. <li @click="onZoomNormal">1:1</li>
  46. <li @click="onSetRecogStyle"><BgColorsOutlined /></li>
  47. </ul>
  48. </div>
  49. <import-btn
  50. upload-url="/api/admin/scan/answer/sheet/update"
  51. :format="['jpg', 'png', 'jpeg']"
  52. :upload-data="updateSheetData"
  53. @upload-success="updateSheetSuccess"
  54. >
  55. <a-tooltip placement="top">
  56. <template #title>
  57. <span>替换图片</span>
  58. </template>
  59. <a-button class="img-change">
  60. <template #icon><PictureFilled /></template>
  61. </a-button>
  62. </a-tooltip>
  63. </import-btn>
  64. </div>
  65. <!-- FillAreaSetDialog -->
  66. <FillAreaSetDialog ref="fillAreaSetDialogRef" @modified="parseRecogBlocks" />
  67. <!-- RecogEditDialog -->
  68. <RecogEditDialog
  69. v-if="curRecogBlock"
  70. ref="recogEditDialogRef"
  71. :recog-data="curRecogBlock"
  72. @confirm="onRecogEditConfirm"
  73. />
  74. </template>
  75. <script setup lang="ts">
  76. import {
  77. ZoomInOutlined,
  78. ZoomOutOutlined,
  79. BgColorsOutlined,
  80. LeftOutlined,
  81. RightOutlined,
  82. PictureFilled,
  83. } from "@ant-design/icons-vue";
  84. import { message } from "ant-design-vue";
  85. import {
  86. saveTemporaryImgViewConfig,
  87. getTemporaryImgViewConfig,
  88. } from "@/utils/index";
  89. import { computed, nextTick, ref, unref, watch } from "vue";
  90. import { useRoute } from "vue-router";
  91. import {
  92. objAssign,
  93. getFileUrl,
  94. getSliceFileUrl,
  95. getBoxImageSize,
  96. } from "@/utils/tool";
  97. import { vEleMoveDirective } from "@/directives/eleMove";
  98. import {
  99. parseRecogData,
  100. parseDetailSize,
  101. RecognizeArea,
  102. RecogBlock,
  103. } from "@/utils/recog/recog";
  104. import { useUserStore, useDataCheckStore } from "@/store";
  105. import { abc } from "@/constants/enumerate";
  106. import FillAreaSetDialog from "./FillAreaSetDialog.vue";
  107. import RecogEditDialog from "./RecogEditDialog.vue";
  108. import ImportBtn from "@/components/ImportBtn/index.vue";
  109. import { debounce } from "lodash-es";
  110. defineOptions({
  111. name: "ScanImage",
  112. });
  113. const route = useRoute();
  114. const emit = defineEmits(["next", "prev"]);
  115. const userStore = useUserStore();
  116. const dataCheckStore = useDataCheckStore();
  117. const curPage = computed(() => dataCheckStore.curPage);
  118. const updateSheetData = computed(() => {
  119. if (!curPage.value) return {};
  120. return {
  121. paperId: curPage.value.paperId,
  122. pageIndex: curPage.value.pageIndex + 1,
  123. };
  124. });
  125. const elRef = ref();
  126. const imgRef = ref();
  127. const imageSize = ref({
  128. width: 0,
  129. height: 0,
  130. top: 0,
  131. left: 0,
  132. scale: 1,
  133. });
  134. const saveImageSizeToSession = debounce(() => {
  135. saveTemporaryImgViewConfig(route.path, imageSize.value);
  136. }, 500);
  137. watch(
  138. imageSize,
  139. () => {
  140. saveImageSizeToSession();
  141. },
  142. { deep: true }
  143. );
  144. const imageStyle = computed(() => {
  145. return {
  146. width: `${imageSize.value.width}px`,
  147. height: `${imageSize.value.height}px`,
  148. top: `${imageSize.value.top}px`,
  149. left: `${imageSize.value.left}px`,
  150. transform: `scale(${imageSize.value.scale})`,
  151. };
  152. });
  153. function initImageSize() {
  154. const imgDom = imgRef.value as HTMLImageElement;
  155. const elDom = elRef.value as HTMLDivElement;
  156. const imgSize = getBoxImageSize({
  157. box: {
  158. width: elDom.clientWidth,
  159. height: elDom.clientHeight,
  160. },
  161. img: {
  162. width: imgDom.naturalWidth,
  163. height: imgDom.naturalHeight,
  164. },
  165. rotate: 0,
  166. });
  167. imageSize.value =
  168. getTemporaryImgViewConfig(route.path) ||
  169. objAssign(imageSize.value, imgSize);
  170. nextTick(() => {
  171. updateRecogList();
  172. });
  173. }
  174. function getNumberResult(
  175. result: Array<string | boolean>,
  176. sources: Array<string | boolean>
  177. ) {
  178. const nResult: number[] = [];
  179. result.forEach((item) => {
  180. const index = sources.indexOf(item);
  181. nResult[index] = 1;
  182. });
  183. return Array.from(nResult).map((item) => item || 0);
  184. }
  185. // recog data
  186. const recogList = ref<RecognizeArea[]>([]);
  187. function updateRecogList() {
  188. recogList.value = [] as RecognizeArea[];
  189. if (!dataCheckStore.curPage) return;
  190. const regdata = parseRecogData(dataCheckStore.curPage.recogData);
  191. if (!regdata) return;
  192. let index = 0;
  193. const ABC = abc.split("");
  194. regdata.question.forEach((gGroup) => {
  195. gGroup.fill_result.forEach((qRecog) => {
  196. const result = dataCheckStore.curPage?.question?.result[index] || "";
  197. qRecog.index = ++index;
  198. const questionResult = result ? result.split("") : [];
  199. const recogItem = parseDetailSize(
  200. qRecog,
  201. "question",
  202. qRecog.index,
  203. getNumberResult(questionResult, ABC)
  204. );
  205. recogList.value.push(recogItem);
  206. });
  207. });
  208. parseRecogBlocks();
  209. }
  210. // recogBlocks
  211. const recogBlocks = ref<RecogBlock[]>([]);
  212. const curRecogBlock = ref<RecogBlock | null>(null);
  213. function parseRecogBlocks() {
  214. const imgDom = imgRef.value as HTMLImageElement;
  215. const rate = imgDom.clientWidth / imgDom.naturalWidth;
  216. const { unfillColor, unfillShow, fillColor, fillShow, borderWidth } =
  217. userStore.recogFillSet;
  218. const curBorderWidth = Math.max(1, borderWidth * rate);
  219. recogBlocks.value = unref(recogList.value).map((item) => {
  220. const fillAreaStyle = {
  221. position: "absolute",
  222. left: `${item.fillArea.x * rate}px`,
  223. top: `${item.fillArea.y * rate}px`,
  224. width: `${item.fillArea.w * rate}px`,
  225. height: `${item.fillArea.h * rate}px`,
  226. zIndex: 9,
  227. };
  228. const fillOptionStyles = item.optionSizes
  229. .map((op) => {
  230. const opStyle = {
  231. position: "absolute",
  232. left: `${op.x * rate}px`,
  233. top: `${op.y * rate}px`,
  234. width: `${op.w * rate}px`,
  235. height: `${op.h * rate}px`,
  236. zIndex: 9,
  237. border: "",
  238. };
  239. if (op.filled && fillShow) {
  240. opStyle.border = `${curBorderWidth}px solid ${fillColor}`;
  241. return opStyle;
  242. }
  243. if (!op.filled && unfillShow) {
  244. opStyle.border = `${curBorderWidth}px solid ${unfillColor}`;
  245. return opStyle;
  246. }
  247. return opStyle;
  248. })
  249. .filter((item) => item);
  250. const nitem: RecogBlock = {
  251. ...item,
  252. fillAreaStyle,
  253. fillOptionStyles,
  254. areaImg: "",
  255. };
  256. return nitem;
  257. });
  258. }
  259. // area click
  260. const recogEditDialogRef = ref();
  261. async function onAreaClick(data: RecogBlock) {
  262. if (!curPage.value) return;
  263. curRecogBlock.value = data;
  264. // 基于 fillArea 四周扩展一个 fillSize 尺寸
  265. const area = {
  266. x: data.fillArea.x - data.fillSize.w,
  267. y: data.fillArea.y - data.fillSize.h,
  268. w: data.fillArea.w + data.fillSize.w * 2,
  269. h: data.fillArea.h + data.fillSize.h * 2,
  270. };
  271. curRecogBlock.value.areaImg = await getSliceFileUrl(
  272. curPage.value.sheetUri,
  273. area
  274. );
  275. nextTick(() => {
  276. recogEditDialogRef.value?.open();
  277. });
  278. }
  279. async function onRecogEditConfirm(result: string[]) {
  280. if (!curRecogBlock.value || !dataCheckStore.curPage) return;
  281. const data = curRecogBlock.value;
  282. if (data.type === "question") {
  283. const index = data.index - 1;
  284. dataCheckStore.curPage.question.result.splice(index, 1, result.join(""));
  285. await dataCheckStore.updateField({
  286. field: "QUESTION",
  287. value: JSON.stringify(dataCheckStore.curPage.question),
  288. });
  289. curRecogBlock.value.result = result;
  290. }
  291. }
  292. // img action
  293. function onZoomIn() {
  294. const scale = imageSize.value.scale;
  295. if (scale >= 2) return;
  296. imageSize.value.scale = Math.min(2, scale * 1.2);
  297. }
  298. function onZoomOut() {
  299. const scale = imageSize.value.scale;
  300. if (scale <= 1) return;
  301. imageSize.value.scale = Math.max(1, scale * 0.8);
  302. }
  303. function onZoomNormal() {
  304. initImageSize();
  305. imageSize.value.scale = 1;
  306. }
  307. interface PosSize {
  308. left: number;
  309. top: number;
  310. }
  311. function onMoveImg({ left, top }: PosSize) {
  312. imageSize.value.left = left;
  313. imageSize.value.top = top;
  314. }
  315. function onPrev() {
  316. emit("prev");
  317. }
  318. function onNext() {
  319. emit("next");
  320. }
  321. // change image
  322. function updateSheetSuccess(data: { uri: string }) {
  323. if (!curPage.value) return;
  324. dataCheckStore.modifySheetUri({
  325. paperIndex: curPage.value.paperIndex,
  326. pageIndex: curPage.value.pageIndex,
  327. uri: data.uri,
  328. });
  329. message.success("上传成功!");
  330. }
  331. // set recog style
  332. const fillAreaSetDialogRef = ref();
  333. function onSetRecogStyle() {
  334. fillAreaSetDialogRef.value?.open();
  335. }
  336. // 监听question.result,同步修改客观题的识别结果
  337. watch(
  338. () => dataCheckStore.curPage?.question?.result,
  339. (val) => {
  340. if (!val) return;
  341. updateRecogList();
  342. },
  343. {
  344. deep: true,
  345. }
  346. );
  347. </script>
  348. <style lang="less" scoped>
  349. .scan-image {
  350. overflow: hidden;
  351. position: relative;
  352. height: 100%;
  353. .img-guide {
  354. &-icon {
  355. position: absolute;
  356. top: 50%;
  357. width: 28px;
  358. height: 32px;
  359. margin-top: -16px;
  360. background: #ffffff;
  361. border-radius: 6px;
  362. border: 1px solid @border-color1;
  363. line-height: 32px;
  364. text-align: center;
  365. z-index: 9;
  366. cursor: pointer;
  367. &:hover {
  368. background-color: #e8f3ff;
  369. border-color: @brand-color;
  370. color: @brand-color;
  371. }
  372. &.is-left {
  373. left: 12px;
  374. }
  375. &.is-right {
  376. right: 12px;
  377. }
  378. }
  379. }
  380. .img-change {
  381. position: absolute;
  382. top: 12px;
  383. right: 12px;
  384. width: 32px;
  385. height: 32px;
  386. line-height: 32px;
  387. background: #e8f3ff;
  388. padding: 0;
  389. border-radius: 6px;
  390. border: 1px solid #bedaff;
  391. color: #4080ff;
  392. text-align: center;
  393. z-index: 9;
  394. &:hover {
  395. opacity: 0.8;
  396. }
  397. }
  398. .img-actions {
  399. position: absolute;
  400. bottom: 12px;
  401. right: 12px;
  402. background: rgba(89, 89, 89, 0.6);
  403. border-radius: 8px;
  404. padding: 4px 8px;
  405. z-index: 9;
  406. li {
  407. display: inline-block;
  408. vertical-align: middle;
  409. width: 26px;
  410. height: 26px;
  411. border-radius: 6px;
  412. line-height: 26px;
  413. text-align: center;
  414. color: #fff;
  415. font-size: 16px;
  416. cursor: pointer;
  417. &:not(:last-child) {
  418. margin-right: 4px;
  419. }
  420. &:hover {
  421. background: rgba(89, 89, 89, 0.6);
  422. }
  423. }
  424. }
  425. .img-body {
  426. position: absolute;
  427. z-index: 2;
  428. }
  429. }
  430. </style>