大家好,又见面了,我是你们的朋友全栈君。
需求 获取图片中金额
复制代码
1.添加maven
<!--图片识别-->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>2.0.1</version>
<exclusions>
<exclusion>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
复制代码
2.安装tessdata
brew install tesseract
复制代码
3.工具类
public class OCRUtil {
private static String tessdataPath;
private static String STRING_TESS_VARIABLE_KEY = "tessedit_char_whitelist";
private static String STRING_TESS_VARIABLE_VALUE = "0123456789.¥";
private static String STRING_TESS_NAME = "tessdata";
private ITesseract instance;
private static OCRUtil ocrUtil;
private OCRUtil(ITesseract instance){
this.instance = instance;
}
private static final LogUtil logUtil = LogUtil.init(OCRUtil.class);
//必须加锁,在高并发情况下会出现jvm崩坏情况
public synchronized String getAmount(BufferedImage bi) throws IOException {
long start = System.currentTimeMillis();
try {
String ocrResult = instance.doOCR(bi);
if(StringUtil.isEmpty(ocrResult)){
return null;
}
if(ocrResult.indexOf("¥") == -1){
return null;
}
logUtil.i("金额识别 为:%s\n金额识别耗时:%s毫秒",ocrResult,System.currentTimeMillis()-start);
return ocrResult.replace("\n","").substring(ocrResult.indexOf("¥") + 1).trim();
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
return null;
}
public static synchronized OCRUtil init(){
if(null == ocrUtil){
ITesseract instance = new Tesseract(); // JNA Direct Mapping
instance.setTessVariable(STRING_TESS_VARIABLE_KEY, STRING_TESS_VARIABLE_VALUE);
// File tessDataFolder = LoadLibs.extractTessResources(STRING_TESS_NAME);
// logUtil.d(tessDataFolder.getAbsolutePath());
//从外部获取tessdataPath或者查询tessdata所在位置
instance.setDatapath(tessdataPath);
ocrUtil = new OCRUtil(instance);
}
return ocrUtil;
}
}
复制代码
转载于:https://juejin.im/post/5b39d1bb6fb9a00e43467df9
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107444.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...