index.vue 10 KB

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