소스 검색

读取图片文字信息

zhaoxian 2 년 전
부모
커밋
fadcedec46

+ 9 - 13
ruixuan-live/src/main/java/com/ruixuan/isc/controller/KuaishouPromoterController.java

@@ -1,5 +1,6 @@
 package com.ruixuan.isc.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ruixuan.common.core.controller.BaseController;
 import com.ruixuan.common.core.domain.AjaxResult;
 import com.ruixuan.common.core.page.TableDataInfo;
@@ -109,23 +110,18 @@ public class KuaishouPromoterController extends BaseController {
      */
     @PostMapping(value = "/readImage")
     @ApiOperation(value = "读取图片文字信息")
-    public String readImage(@RequestParam("file") MultipartFile file) {
+    public JSONObject readImage(@RequestParam("file") MultipartFile file) {
+        JSONObject result = new JSONObject();
         try {
-            Tess4jClient t = new Tess4jClient();
-//            ITesseract tesseract = new Tesseract();
-//            File savefile = new File("C:\\Users\\Administrator\\Desktop\\test005.png");
-//            //设置中文字体库路径
-//            tesseract.setDatapath("D:\\download\\tessdata");
-//            //中文识别
-//            tesseract.setLanguage("chi_sim");
-//            //执行ocr识别
-//            String result = tesseract.doOCR(savefile);
-            String result = t.getWords(file);
-            System.out.println(result);
+            result.put("code", 0);
+            result.put("message", "success");
+            result.put("data", Tess4jClient.getWords(file));
             return result;
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return "";
+        result.put("code", -1);
+        result.put("message", "fail");
+        return result;
     }
 }

+ 4 - 5
ruixuan-live/src/main/java/com/ruixuan/isc/utils/Tess4jClient.java

@@ -22,7 +22,7 @@ public class Tess4jClient {
     private static String LANGUAGE = "chi_sim";
 
     // 入参:图片流
-    public String doOCR(BufferedImage image) throws Exception {
+    public static String doOCR(BufferedImage image) throws Exception {
         //创建Tesseract对象
         ITesseract tesseract = new Tesseract();
         //设置中文字体库路径
@@ -32,12 +32,11 @@ public class Tess4jClient {
         //执行ocr识别
         String result = tesseract.doOCR(image);
         //替换回车和tal键  使结果为一行
-        System.out.println(result.replaceAll(" ", ""));
-        result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
-        return result;
+//        result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
+        return result.replaceAll(" ", "");
     }
 
-    public String getWords(MultipartFile file) throws Exception {
+    public static String getWords(MultipartFile file) throws Exception {
         InputStream inputStream = file.getInputStream();
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         byte[] buff = new byte[100];