ソースを参照

读取图片文字信息

zhaoxian 2 年 前
コミット
cca8e323ed

+ 6 - 3
ruixuan-admin/src/main/resources/application-dev.yml

@@ -50,9 +50,9 @@ spring:
         url: jdbc:mysql://139.186.165.84:3306/ruixuan?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
         username: hcst
         password: hcst@2020
-#         url: jdbc:mysql://139.186.27.96:3390/ruixuan?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
-#         username: data
-#         password: hcst@2021
+      #         url: jdbc:mysql://139.186.27.96:3390/ruixuan?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+      #         username: data
+      #         password: hcst@2021
       # 从库数据源
       slave:
         # 从数据源开关/默认关闭
@@ -192,4 +192,7 @@ promoter:
   #内网数据库服务IP
   internal_ip_path: http://api.tjyourong.com.cn/material/
 
+  #读取图片汉字字符集
+tessdata:
+  path: /usr/local/tesseract/share/tessdata
 

+ 5 - 1
ruixuan-admin/src/main/resources/application-prod.yml

@@ -187,4 +187,8 @@ promoter:
   #启动宇鹏清洗数据的服务IP
   start_ip_path: http://111.206.86.186:9999/
   #内网数据库服务IP
-  internal_ip_path: http://api.tjyourong.com.cn/material/
+  internal_ip_path: http://api.tjyourong.com.cn/material/
+
+  #读取图片汉字字符集
+tessdata:
+  path: /usr/local/tesseract/share/tessdata

+ 4 - 1
ruixuan-live/src/main/java/com/ruixuan/isc/controller/KuaishouPromoterController.java

@@ -11,6 +11,7 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -35,6 +36,8 @@ public class KuaishouPromoterController extends BaseController {
     @Autowired
     private IKuaishouPromoterService kuaishouPromoterService;
 
+    @Value("${tessdata.path}")
+    private String path;
     /**
      * 查询快手达人 信息列表
      */
@@ -115,7 +118,7 @@ public class KuaishouPromoterController extends BaseController {
         try {
             result.put("code", 0);
             result.put("message", "success");
-            result.put("data", Tess4jClient.getWords(file));
+            result.put("data", Tess4jClient.getWords(file,path));
             return result;
         } catch (Exception e) {
             e.printStackTrace();

+ 6 - 10
ruixuan-live/src/main/java/com/ruixuan/isc/utils/Tess4jClient.java

@@ -1,9 +1,7 @@
 package com.ruixuan.isc.utils;
 
-import lombok.Data;
 import net.sourceforge.tess4j.ITesseract;
 import net.sourceforge.tess4j.Tesseract;
-import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.imageio.ImageIO;
@@ -13,20 +11,18 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
-@Data
-@Component
+
 public class Tess4jClient {
 
-    private static String DATA_PATH = "/usr/local/tesseract/share/tessdata";
-//    private static String DATA_PATH = "/download/tessdata";
+    //    private static String DATA_PATH = "/usr/local/tesseract/share/tessdata";
     private static String LANGUAGE = "chi_sim";
 
     // 入参:图片流
-    public static String doOCR(BufferedImage image) throws Exception {
+    public String doOCR(BufferedImage image, String path) throws Exception {
         //创建Tesseract对象
         ITesseract tesseract = new Tesseract();
         //设置中文字体库路径
-        tesseract.setDatapath(DATA_PATH);
+        tesseract.setDatapath(path);
         //中文识别
         tesseract.setLanguage(LANGUAGE);
         //执行ocr识别
@@ -36,7 +32,7 @@ public class Tess4jClient {
         return result.replaceAll(" ", "");
     }
 
-    public static String getWords(MultipartFile file) throws Exception {
+    public static String getWords(MultipartFile file, String path) throws Exception {
         InputStream inputStream = file.getInputStream();
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         byte[] buff = new byte[100];
@@ -53,7 +49,7 @@ public class Tess4jClient {
         ByteArrayInputStream in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
         BufferedImage imageFile = ImageIO.read(in);
         //识别图片的文字
-        return doOCR(imageFile);
+        return new Tess4jClient().doOCR(imageFile, path);
         //再结合敏感词过滤算法,审核图片中的文字是否包含敏感词
     }