需求 獲取圖片中金額
複製代碼
1.添加mavenbash
<!--圖片識別-->
<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.工具類app
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;
}
}
複製代碼