Explorar el Código

读取图片文字信息

zhaoxian hace 2 años
padre
commit
8a15638b9d

+ 11 - 0
ruixuan-live/pom.xml

@@ -64,6 +64,17 @@
             <version>1.0.3</version>
         </dependency>
 
+        <dependency>
+            <groupId>net.sourceforge.tess4j</groupId>
+            <artifactId>tess4j</artifactId>
+            <version>4.1.1</version>
+        </dependency>
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>7.1.0</version>
+        </dependency>
+
     </dependencies>
 
 

+ 28 - 0
ruixuan-live/src/main/java/com/ruixuan/isc/controller/KuaishouPromoterController.java

@@ -5,6 +5,7 @@ import com.ruixuan.common.core.domain.AjaxResult;
 import com.ruixuan.common.core.page.TableDataInfo;
 import com.ruixuan.isc.entity.KuaishouPromoter;
 import com.ruixuan.isc.service.IKuaishouPromoterService;
+import com.ruixuan.isc.utils.Tess4jClient;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 
@@ -100,4 +102,30 @@ public class KuaishouPromoterController extends BaseController {
     ) {
         return toAjax(kuaishouPromoterService.deleteKuaishouPromoterById(id, userId));
     }
+
+
+    /**
+     * 读取图片文字信息
+     */
+    @PostMapping(value = "/readImage")
+    @ApiOperation(value = "读取图片文字信息")
+    public String readImage(@RequestParam("file") MultipartFile file) {
+        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);
+            return result;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
 }

+ 90 - 0
ruixuan-live/src/main/java/com/ruixuan/isc/utils/Tess4jClient.java

@@ -0,0 +1,90 @@
+package com.ruixuan.isc.utils;
+
+import io.minio.GetObjectArgs;
+import lombok.Data;
+import net.sourceforge.tess4j.ITesseract;
+import net.sourceforge.tess4j.Tesseract;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+@Data
+@Component
+public class Tess4jClient {
+
+    private String dataPath = "/data/webapp/RuiXuan-Cloud/tessdata";
+    private String language = "chi_sim";
+    // 入参:图片流
+    public String doOCR(BufferedImage image) throws Exception {
+        //创建Tesseract对象
+        ITesseract tesseract = new Tesseract();
+        //设置中文字体库路径
+        tesseract.setDatapath(dataPath);
+        //中文识别
+        tesseract.setLanguage(language);
+        //执行ocr识别
+        String result = tesseract.doOCR(image);
+        //替换回车和tal键  使结果为一行
+        System.out.println(result.replaceAll(" ", ""));
+        result = result.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
+        return result;
+    }
+
+    public String getWords(MultipartFile file) throws Exception {
+        InputStream inputStream = file.getInputStream();
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        byte[] buff = new byte[100];
+        int rc = 0;
+        while (true) {
+            try {
+                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            byteArrayOutputStream.write(buff, 0, rc);
+        }
+        //从byte[]转换为butteredImage
+        ByteArrayInputStream in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
+        BufferedImage imageFile = ImageIO.read(in);
+        //识别图片的文字
+        return doOCR(imageFile);
+        //再结合敏感词过滤算法,审核图片中的文字是否包含敏感词
+    }
+
+//    public byte[] downLoadFile(String pathUrl)  {
+//        String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");
+//        int index = key.indexOf("/");
+//        String bucket = key.substring(0,index);
+//        String filePath = key.substring(index+1);
+//        InputStream inputStream = null;
+//        try {
+//            inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
+//        } catch (Exception e) {
+//            log.error("minio down file error.  pathUrl:{}",pathUrl);
+//            e.printStackTrace();
+//        }
+//
+//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+//        byte[] buff = new byte[100];
+//        int rc = 0;
+//        while (true) {
+//            try {
+//                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
+//            } catch (IOException e) {
+//                e.printStackTrace();
+//            }
+//            byteArrayOutputStream.write(buff, 0, rc);
+//        }
+//        return byteArrayOutputStream.toByteArray();
+//    }
+
+
+}