一、源码及语言包下载
首先说一下,用的时候发现,图片比较清晰的准,稍微模糊的就差强人意了。
Tess4J - JNA wrapper for Tesseract
二、mave导包
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.2.1</version>
</dependency>
三、执行代码
public static void main(String[] args) {
// 识别图片的路径(修改为自己的图片路径)
String path = "E:\\temp\\test.png";
ImageUtils.toGrayImge(new File(path));
// 语言库位置(需要自己根据第一条的路径下载,修改为自己语言库保存到的文件夹的路径)
String lagnguagePath = "C:\\Program Files\\Tess4J\\tessdata";
File file = new File(path);
ITesseract instance = new Tesseract();
//设置训练库的位置
instance.setDatapath(lagnguagePath);
//chi_sim :简体中文, eng 根据需求选择语言库
instance.setLanguage("chi_sim");
String result = null;
try {
long startTime = System.currentTimeMillis();
result = instance.doOCR(file);
long endTime = System.currentTimeMillis();
System.out.println("Time is:" + (endTime - startTime) + " 毫秒");
} catch (TesseractException e) {
e.printStackTrace();
}
System.out.println("result: ");
System.out.println(result);
}