您的位置:首页 > 其它

使用tess4j3.3.0实现OCR图片识别

2017-09-18 17:51 555 查看
前几天用tesseract-ocr写了一篇博客,但是感觉代码量有点多了,于是发现java开源库有个tess4j,相对来说简洁多了,于是去tess4j的网站上下载最新版 http://tess4j.sourceforge.net/ (需翻墙)

然后打开lib文件夹,把下面图片中的jar引入到项目中:



注意前提是你要先安装tesseract-ocr-setup-3.05.00dev.exe,之前我windows10安装的是3.02,导致一直报错,换成这个最新的就好了,所以tess4j可能跟tesseract版本有对应关系吧



安装的时候可以选择中文字库 ,然后贴一下我项目中测试的代码片段:

File imageFile = new File("d:\\9.jpg");

ITesseract instance = new Tesseract(); // JNA Interface Mapping // JNA Interface Mapping
// ITesseract instance = new Tesseract1(); // JNA Direct Mapping
instance.setDatapath("D:\\Java\\Tesseract-OCR\\tessdata"); // replace <parentPath> with path to parent directory of tessdata
instance.setLanguage("eng");

try {
BufferedImage bi = ImageIO.read(imageFile);

//            String result = instance.doOCR(imageFile);
String result = instance.doOCR(bi);
System.out.println(result);

} catch (TesseractException e) {
System.err.println(e.getMessage());
} catch (Exception e) {
System.err.println(e.getMessage());
}

这样你会发现tess4j的代码简单多了,当然你还可以根据自己项目的ocr需求进行相应优化,tess4j也有很强大的图片处理工具类,什么灰度化去噪等等

分享下载:

Tess4J-3.3.0-src.zip

tesseract-ocr-setup-3.05.00dev.exe

贴个我项目中优化的方式吧 感觉效果还不错

二值化处理:

BufferedImage grayImage = ImageHelper.convertImageToBinary(ImageIO
.read(imageFile));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息