Tesseract-ocr识别使用教程
第一步:安装Tesseract-ocr软件
在网上搜索
tesseract-ocr-setup-4.00.00dev.exe
自行下载
第二步:配置Tesseract-ocr环境变量
TESSDATA_PREFIX : tesseracr的安装路径,tessdata文件夹的上一级目录位置
将配置好的TESSDATA_PREFIX添加到系统Path中
第三步:IDEA工程导入tess4j的jar包
<!--OCR识别-->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
<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>
第四步:测试代码
测试主函数
public class OCRTest {
public static void main(String[] args) {
testMultiple();
}
/**
* 识别整个文件夹内的图片
*
*/
public static void testMultiple(){
try {
// 文件目录下所有的文件
File testDataDir = new File("E:\\temp\\photo");
// 遍历获取目录下的每一个文件
for (File file : testDataDir.listFiles()) {
// 调用识别方法,获取识别后的字符串
String recognizeText = new OCRHelper().recognizeText(file);
// 输出识别后的结果
System.out.println(recognizeText);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 识别指定图片
*
*/
public static void testSingle() {
try {
// 获取指定图片的识别结果
String recognizeText =
new OCRHelper().recognizeText(new File("E:\\temp\\photo\\7.png"));
// 输出识别后的结果
System.out.print(recognizeText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
OCR工具类
public class OCRHelper {
// 设置使用指定语言命令
private final String LANG_OPTION = "-l";
// 获取当前系统的换行符
private final String EOL = System.getProperty("line.separator");
// Tesseract-OCR的安装路径
private String tessPath = "E:\\tesseract_ocr\\Tesseract-OCR";
/**
* @param imageFile : 传入的图像文件
* @return 识别后的字符串
*/
public String recognizeText(File imageFile) throws Exception {
/**
* 设置输出文件的保存的文件目录
* 与当前图片在同一目录下
* getParentFile : 获取当前文件的父目录
*/
File outputFile = new File(imageFile.getParentFile(), "output");
// 缓冲流,用于向输出文件写入识别后的数据
StringBuffer strB = new StringBuffer();
// 存储cmd命令
// 注意:不识别空格
// 如 -psm 6要写成cmd.add("-psm"),cmd.add("6")
List<String> cmd = new ArrayList<>();
// 调用tesseract指令
cmd.add(tessPath + "\\tesseract");
cmd.add("");
// 输出文件名称
cmd.add(outputFile.getName());
// 指定识别类型
cmd.add("-psm");
// 6表示识别一行字符串
cmd.add("6");
// 使用指定语言包识别
cmd.add(LANG_OPTION);
// 使用英文语言包
cmd.add("eng");
ProcessBuilder pb = new ProcessBuilder();
/**
*Sets this process builder's working directory.
*/
// 调用cmd命令相关代码
pb.directory(imageFile.getParentFile());
cmd.set(1, imageFile.getName());
pb.command(cmd);
pb.redirectErrorStream(true);
// 设置开始时间
long startTime = System.currentTimeMillis();
System.out.println("开始时间:" + startTime);
Process process = pb.start();
// tesseract.exe 1.jpg 1 -l chi_sim
//不习惯使用ProcessBuilder的,也可以使用Runtime,效果一致
// Runtime.getRuntime().exec("tesseract.exe 1.jpg 1 -l chi_sim");
/**
* the exit value of the process. By convention, 0 indicates normal
* termination.
*/
int w = process.waitFor();
if (w == 0)// 0代表正常退出
{
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(outputFile.getAbsolutePath() + ".txt"),
"UTF-8"));
String str;
while ((str = in.readLine()) != null) {
strB.append(str).append(EOL);
}
in.close();
long endTime = System.currentTimeMillis();
System.out.println("结束时间:" + endTime);
System.out.println("耗时:" + (endTime - startTime) + "毫秒");
}
else {
// 设置异常信息提醒
String msg;
switch (w) {
case 1:
msg = "Errors accessing files. There may be spaces in your image's filename.";
break;
case 29:
msg = "Cannot recognize the image or its selected region.";
break;
case 31:
msg = "Unsupported image format.";
break;
default:
msg = "Errors occurred.";
}
throw new RuntimeException(msg);
}
// 删除生成的文件
new File(outputFile.getAbsolutePath() + ".txt").delete();
// 返回识别后的结果
return strB.toString().replaceAll("\\s*", "");
}
}
至此,可以识别出图片的内容。