|
@@ -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);
|
|
|
//再结合敏感词过滤算法,审核图片中的文字是否包含敏感词
|
|
|
}
|
|
|
|