IdentifyFont.vue 13 KB

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