1.文件结构目录
2.具体实现
①添加maven依赖
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>3.2.1</version> </dependency>
②建立tessdata文字识别库,并添加识别库
资源地址 :https://github.com/tesseract-ocr/tessdata/tree/3.04.00
chi_sim.traineddata (中文)
eng.traineddata (英文)
③Tess4jTest.java
package com.hxlz.test;
import java.io.File;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;
public class Tess4jTest {
public static void main(String[] args) {
File imageFile = new File("C:\\1.jpeg");
Tesseract instance = Tesseract.getInstance();
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
instance.setLanguage("chi_sim");
//instance.setLanguage("eng");
instance.setDatapath(tessDataFolder.getAbsolutePath());
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
}
3.结果展示
你自己试试就晓得了