IdentifyFont.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <template>
  2. <!-- <el-dialog v-model="isOpen" title="文字识别" width="55%"> -->
  3. <div class="container">
  4. <div v-if="thisStep === 'start'" class="up_box">
  5. <div class="left_box">
  6. <div class="upimg_box boder_box">
  7. <el-upload
  8. v-if="!showImg"
  9. class="avatar-uploader"
  10. :drag="true"
  11. :action="actionUrl"
  12. :headers="headersObj"
  13. :show-file-list="false"
  14. :on-success="handleAvatarSuccess"
  15. :before-upload="beforeUp"
  16. >
  17. <img src="@/assets/images/Frame427319159.png" class="avatar" />
  18. <span>将图片拖入框内/点击按钮上传图片</span>
  19. </el-upload>
  20. <div v-else class="img_show">
  21. <img :src="toRaw(showImg)" class="avatar" />
  22. </div>
  23. </div>
  24. <div class="sunmit_btn">
  25. <el-upload
  26. class="avatar-uploader"
  27. :action="actionUrl"
  28. :headers="headersObj"
  29. :show-file-list="false"
  30. :on-success="handleAvatarSuccess"
  31. :before-upload="beforeUp"
  32. >
  33. <span>选择本地图片</span>
  34. </el-upload>
  35. </div>
  36. </div>
  37. <div class="right_box">
  38. <div class="upimg_box boder_box">
  39. <el-upload
  40. disabled
  41. class="avatar-uploader"
  42. :action="actionUrl"
  43. :show-file-list="false"
  44. :on-success="handleAvatarSuccess"
  45. >
  46. <img src="@/assets/images/FrameYun.png" class="avatar" />
  47. <span>点击按钮上传图片</span>
  48. </el-upload>
  49. </div>
  50. <div class="sunmit_btn hand" @click="addlibraryImg">添加文库图片</div>
  51. </div>
  52. </div>
  53. <div v-if="thisStep === 'loading'" class="load-box">
  54. <div class="light_box">
  55. <img :src="toRaw(showImg)" class="avatar" />
  56. <div class="light"></div>
  57. </div>
  58. <div class="text_box">
  59. <span>请耐心等待扫描完成</span>
  60. <Loading :small="true"></Loading>
  61. </div>
  62. </div>
  63. <div v-if="thisStep === 'end'" class="end_box">
  64. <div class="left_box">
  65. <div class="boder_box">
  66. <div class="title">原始图片</div>
  67. <div class="upimg_box">
  68. <div class="img_show">
  69. <img :src="toRaw(showImg)" class="avatar" />
  70. </div>
  71. </div>
  72. </div>
  73. <div class="sunmit_btn">
  74. <!-- <el-upload
  75. class="avatar-uploader"
  76. action="http://192.168.1.28:8080/api/upload"
  77. :show-file-list="false"
  78. :on-success="handleAvatarSuccess"
  79. :before-upload="beforeUp"
  80. > -->
  81. <span @click="reClick" class="hand">重新选择</span>
  82. <!-- </el-upload> -->
  83. </div>
  84. </div>
  85. <div class="right_box">
  86. <div class="boder_box">
  87. <div class="title">识别结果</div>
  88. <div class="upimg_box">
  89. <el-scrollbar>
  90. <div class="text_box">
  91. {{ textData }}
  92. </div>
  93. </el-scrollbar>
  94. </div>
  95. </div>
  96. <div class="sunmit_btn copy-qb hand" @click="copyQbUrl()">复制结果</div>
  97. </div>
  98. </div>
  99. </div>
  100. <!-- </el-dialog> -->
  101. <PicTree
  102. :openFile="openFile"
  103. @close="openFile = false"
  104. :fileUserTreeData="treeData"
  105. @fileChangeMsg="fileChangeMsg"
  106. ></PicTree>
  107. <div
  108. v-loading.fullscreen="loadingPreview"
  109. v-if="loadingPreview"
  110. class="lodingBox"
  111. ></div>
  112. </template>
  113. <script setup>
  114. import { onMounted, ref, toRaw, watch, inject } from "vue";
  115. import { ElMessage } from "element-plus";
  116. import Clipboard from "clipboard";
  117. import Loading from "@/components/Loading/Loading.vue";
  118. import PicTree from "@/components/PicTree/PicTree.vue"; //选择文件发送的列表
  119. import { picTree } from "@/api/search/search.js";
  120. import { ocrRemark, preview } from "@/api/common/common.js";
  121. import Cookies from "js-cookie";
  122. const textData = ref(""); //解析出来的文字
  123. const showImg = ref(); //上传的图片
  124. const isOpen = ref(props.openFile);
  125. const thisStep = ref("start"); //解析进度 start,loading,end,
  126. const openFile = ref(false); //控制tree显示
  127. const treeData = ref(); //树节点数据
  128. const actionUrl = ref(
  129. `${window.location.origin}${import.meta.env.VITE_APP_BASE_API}/api/upload`
  130. );
  131. const headersObj = ref({
  132. Authorization:Cookies.get("Admin-Token")
  133. })
  134. // const actionUrl = ref(`${window.location.origin}/upload`);
  135. const loadingPreview = ref(true);
  136. const addFolderAdd = inject("addFolderAdd");
  137. const props = defineProps({
  138. openFile: {
  139. type: Boolean,
  140. default: false,
  141. },
  142. });
  143. watch(
  144. () => props.openFile,
  145. (newValue) => {
  146. // console.log(111);
  147. isOpen.value = newValue;
  148. }
  149. );
  150. onMounted(async () => {
  151. // console.log('textData.value',textData.value);
  152. const cTextData = sessionStorage.getItem('textData')
  153. const imgStr = sessionStorage.getItem('showImg')
  154. const ocrJSONData = sessionStorage.getItem('ocrData')
  155. const ocrData = ocrJSONData?JSON.parse(ocrJSONData):''
  156. const cImg = imgStr?JSON.parse(imgStr):''
  157. // console.log('cTextData',cTextData);
  158. // console.log('cImg',cImg);
  159. if(cImg && cTextData){
  160. // TODO
  161. thisStep.value = "end"
  162. textData.value = cTextData
  163. showImg.value = cImg
  164. loadingPreview.value = false;
  165. return
  166. }
  167. let route = useRoute();
  168. // let docId = route.query.docId;
  169. // let fileId = route.query.fileId;
  170. // let fileType = route.query.fileType;
  171. let docId = ocrData.docId;
  172. let fileId = ocrData.fileId;
  173. let fileType = ocrData.fileType;
  174. const JparData = history.state.clickRowId;
  175. const parData =JparData? JSON.parse(JparData):'';
  176. // console.log("parData", parData);
  177. if (parData && JparData != "N") {
  178. docId = parData.docId;
  179. fileId = parData.fileId;
  180. fileType = parData.fileType;
  181. }
  182. // console.log("fileId", fileId);
  183. // console.log("fileType", fileType);
  184. // console.log("textData", textData.value);
  185. if(textData.value){
  186. thisStep.value = "end";
  187. }else if (fileType === ".png" || fileType === ".jpg"|| fileType === ".JPG"|| fileType === ".PNG") {
  188. const imgRes = await preview(docId);
  189. thisStep.value = "loading";
  190. loadingPreview.value = false;
  191. showImg.value = URL.createObjectURL(imgRes);
  192. const res = await ocrRemark(fileId);
  193. // ocrRemark(fileId).then((res)=>{
  194. // console.log('thenews',res);
  195. // }).catch(err=>{
  196. // console.log('err',err);
  197. // })
  198. thisStep.value = "end";
  199. textData.value = res.msg;
  200. sessionStorage.setItem('textData',textData.value)
  201. sessionStorage.setItem('showImg',JSON.stringify(showImg.value))
  202. } else {
  203. loadingPreview.value = false;
  204. }
  205. });
  206. const handleAvatarSuccess = (msg, file) => {
  207. // console.log("re", msg);
  208. const flieData = toRaw(file);
  209. // console.log("file", flieData);
  210. // showImg.value = URL.createObjectURL(flieData.raw);
  211. if (msg.code === 200) {
  212. textData.value = msg.msg;
  213. thisStep.value = "end";
  214. sessionStorage.setItem('textData',textData.value)
  215. sessionStorage.setItem('showImg',JSON.stringify(showImg.value))
  216. } else {
  217. thisStep.value = "start";
  218. textData.value = null;
  219. showImg.value = null;
  220. ElMessage({ message: "识别失败", type: "error" });
  221. }
  222. // console.log("textData", textData.value);
  223. };
  224. //文件上传前的钩子
  225. const beforeUp = (raw) => {
  226. // console.log('raw',raw);
  227. showImg.value = URL.createObjectURL(raw);
  228. thisStep.value = "loading";
  229. };
  230. // 复制功能调用的方法
  231. const copyQbUrl = () => {
  232. let clipboard = new Clipboard(".copy-qb", {
  233. text: () => {
  234. return textData.value;
  235. },
  236. });
  237. clipboard.on("success", () => {
  238. // console.log('success');
  239. ElMessage({ message: "复制成功", type: "success" });
  240. clipboard.destroy();
  241. });
  242. clipboard.on("error", () => {
  243. // console.log('err');
  244. clipboard.destroy();
  245. });
  246. };
  247. // 选取文库图片
  248. const addlibraryImg = async () => {
  249. const res = await picTree();
  250. console.log("res", res);
  251. treeData.value = res;
  252. openFile.value = true;
  253. };
  254. //确定选中图片
  255. const fileChangeMsg = async (val) => {
  256. console.log("val", toRaw(val));
  257. const data = toRaw(val);
  258. const imgRes = await preview(data.id);
  259. thisStep.value = "loading";
  260. showImg.value = URL.createObjectURL(imgRes);
  261. const res = await ocrRemark(data.remark);
  262. thisStep.value = "end";
  263. textData.value = res.msg;
  264. sessionStorage.setItem('textData',textData.value)
  265. sessionStorage.setItem('showImg',JSON.stringify(showImg.value))
  266. // console.log("res", res);
  267. // console.log("imgRes", imgRes);
  268. };
  269. const reClick = () => {
  270. thisStep.value = "start";
  271. textData.value = null;
  272. showImg.value = null;
  273. };
  274. </script>
  275. <style lang="scss" scoped>
  276. .lodingBox {
  277. position: absolute;
  278. top: 0;
  279. left: 0;
  280. width: 100vh;
  281. height: 100vh;
  282. z-index: 10000000;
  283. }
  284. .container {
  285. width: 100%;
  286. height: 87vh;
  287. background-color: #fff;
  288. display: flex;
  289. align-items: center;
  290. justify-content: center;
  291. .up_box {
  292. display: flex;
  293. width: 100%;
  294. justify-content: center;
  295. }
  296. .left_box {
  297. width: 45%;
  298. display: flex;
  299. flex-direction: column;
  300. align-items: center;
  301. margin-right: 48px;
  302. .boder_box {
  303. width: 100%;
  304. border: 1px solid #c1cce3;
  305. }
  306. .title {
  307. width: 100%;
  308. height: 40px;
  309. background: #ebeff6;
  310. line-height: 40px;
  311. font-size: 16px;
  312. font-weight: 400;
  313. text-align: center;
  314. }
  315. .img_show {
  316. width: 100%;
  317. height: calc(100% - 40px);
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. img {
  322. width: 100%;
  323. max-height: 100%;
  324. // opacity: 0;
  325. // object-fit:cover; // 保持原比例缩小
  326. object-fit: contain; // 保持原比例缩小
  327. }
  328. }
  329. .upimg_box {
  330. width: 100%;
  331. height: 500px;
  332. display: flex;
  333. align-items: center;
  334. justify-content: center;
  335. }
  336. .sunmit_btn {
  337. margin-top: 24px;
  338. width: 120px;
  339. height: 32px;
  340. background: #2e6bc8;
  341. border-radius: 4px 4px 4px 4px;
  342. color: #fff;
  343. font-size: 14px;
  344. line-height: 32px;
  345. font-weight: 400;
  346. text-align: center;
  347. }
  348. }
  349. .right_box {
  350. width: 45%;
  351. display: flex;
  352. flex-direction: column;
  353. align-items: center;
  354. .boder_box {
  355. width: 100%;
  356. border: 1px solid #c1cce3;
  357. }
  358. .title {
  359. width: 100%;
  360. height: 40px;
  361. background: #ebeff6;
  362. line-height: 40px;
  363. font-size: 16px;
  364. font-weight: 400;
  365. text-align: center;
  366. }
  367. .upimg_box {
  368. width: 100%;
  369. height: 500px;
  370. // border: 1px solid #c1cce3;
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. }
  375. .sunmit_btn {
  376. margin-top: 24px;
  377. width: 120px;
  378. height: 32px;
  379. background: #2e6bc8;
  380. border-radius: 4px 4px 4px 4px;
  381. color: #fff;
  382. font-size: 14px;
  383. line-height: 32px;
  384. font-weight: 400;
  385. text-align: center;
  386. }
  387. .text_box {
  388. padding: 16px;
  389. font-size: 14px;
  390. font-weight: 500;
  391. color: #000000;
  392. line-height: 22px;
  393. }
  394. }
  395. .load-box {
  396. display: flex;
  397. align-items: center;
  398. // justify-content: center;
  399. flex-direction: column;
  400. .text_box {
  401. width: 100%;
  402. margin-top: 20px;
  403. color: #06286c;
  404. display: flex;
  405. justify-content: center;
  406. align-items: center;
  407. span {
  408. margin-right: 15px;
  409. }
  410. }
  411. .light_box {
  412. width: 500px;
  413. height: 500px;
  414. background-image: url("@/assets/images/light_boder.png");
  415. background-repeat: no-repeat;
  416. background-size: contain;
  417. position: relative;
  418. display: flex;
  419. align-items: center;
  420. justify-content: center;
  421. padding: 0 15px;
  422. img {
  423. width: 100%;
  424. height: 100%;
  425. object-fit: contain; // 保持原比例缩小
  426. }
  427. .light {
  428. width: calc(100% - 30px);
  429. height: 1px;
  430. position: absolute;
  431. top: 0;
  432. left: 15px;
  433. right: 0;
  434. bottom: 0;
  435. background-color: rgba(0, 255, 255, 0.7);
  436. box-shadow: 0px 2px 10px 5px rgba(0, 255, 255, 0.3);
  437. animation: move 2s infinite linear;
  438. }
  439. @keyframes move {
  440. from {
  441. top: 0px;
  442. }
  443. /*网格移动到显示区域的外面*/
  444. to {
  445. top: 100%;
  446. }
  447. }
  448. }
  449. }
  450. .end_box {
  451. display: flex;
  452. justify-content: center;
  453. width: 100%;
  454. }
  455. }
  456. :deep(.el-dialog__header) {
  457. background-color: #eceff7;
  458. margin-right: 0px;
  459. }
  460. .avatar-uploader .el-upload {
  461. border: 1px dashed var(--el-border-color);
  462. border-radius: 6px;
  463. cursor: pointer;
  464. position: relative;
  465. overflow: hidden;
  466. transition: var(--el-transition-duration-fast);
  467. }
  468. ::v-deep .avatar-uploader .el-upload {
  469. display: flex;
  470. flex-direction: column;
  471. img {
  472. width: 220;
  473. height: 220px;
  474. margin-bottom: 16px;
  475. }
  476. span {
  477. font-size: 12px;
  478. font-weight: 400;
  479. color: #06286c;
  480. }
  481. }
  482. ::v-deep .sunmit_btn .avatar-uploader .el-upload {
  483. span {
  484. font-size: 14px;
  485. font-weight: 400;
  486. color: #fff;
  487. }
  488. }
  489. ::v-deep .avatar-uploader .el-upload .el-upload-dragger {
  490. display: flex;
  491. flex-direction: column;
  492. align-items: center;
  493. border: none;
  494. }
  495. .avatar-uploader .el-upload:hover {
  496. border-color: var(--el-color-primary);
  497. }
  498. .hand{
  499. cursor: pointer;
  500. }
  501. .el-icon.avatar-uploader-icon {
  502. font-size: 28px;
  503. color: #8c939d;
  504. width: 178px;
  505. height: 178px;
  506. text-align: center;
  507. }
  508. </style>