index.vue 11 KB

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