1、引入tess4j依赖
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.11.0</version>
</dependency>
2、在项目resources下创建tessdata文件夹
3、引入语言包
可以在我的百度网盘上提取,进入网盘
(链接: https://pan.baidu.com/s/17TiWby83LTK0cL8XXG_mUw?pwd=nd2d 提取码: nd2d )
中文字库 chi_sim.traineddata ,英文字库 eng.traineddata
4、测试类
@SneakyThrows
public static void main(String[] args) throws Exception {
File imageFile = new File("C:\\Users\\Administrator\\Desktop\\1722237818223.png");
Tesseract tesseract = new Tesseract();
//设置 tessdata 目录
tesseract.setDatapath("wms/src/main/resources/tessdata");
//设置语言 中文
tesseract.setLanguage("chi_sim");
try {
String extractedText = tesseract.doOCR(imageFile);
System.out.println(extractedText);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
测试文章图片及代码解析结果