Tess4J是对Tesseract OCR API.的Java JNA 封装。使java能够通过调用Tess4J的API来使用Tesseract OCR。支持的格式:TIFF,JPEG,GIF,PNG,BMP,JPEG,and PDF
Tesseract 的github地址:https://github.com/tesseract-ocr/tesseract
Tess4J的github地址:https://github.com/nguyenq/tess4j

Tess4J API 提供的功能:
1、直接识别支持的文件
2、识别图片流
3、识别图片的某块区域
4、将识别结果保存为 TEXT/ HOCR/ PDF/ UNLV/ BOX
5、通过设置取词的等级,提取识别出来的文字
6、获得每一个识别区域的具体坐标范围
7、调整倾斜的图片
8、裁剪图片
9、调整图片分辨率
10、从粘贴板获得图像
11、克隆一个图像(目的:创建一份一模一样的图片,与原图在操作修改上,不相 互影响)
12、图片转换为二进制、黑白图像、灰度图像
13、反转图片颜色
demo.java:

    /**
* Test of doOCR method, of class Tesseract.
* 根据图片文件进行识别
* @throws Exception while processing image.
*/
@Test
public void testDoOCR_File() throws Exception {
logger.info("doOCR on a jpg image");
File imageFile = new File(this.testResourcesDataPath, "ocr.png");
//set language
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim");
String result = instance.doOCR(imageFile);
logger.info(result);
} /**
* Test of doOCR method, of class Tesseract.
* 根据图片流进行识别
* @throws Exception while processing image.
*/
@Test
public void testDoOCR_BufferedImage() throws Exception {
logger.info("doOCR on a buffered image of a PNG");
File imageFile = new File(this.testResourcesDataPath, "ocr.png");
BufferedImage bi = ImageIO.read(imageFile); //set language
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim"); String result = instance.doOCR(bi);
logger.info(result);
} /**
* Test of getSegmentedRegions method, of class Tesseract.
* 得到每一个划分区域的具体坐标
* @throws java.lang.Exception
*/
@Test
public void testGetSegmentedRegions() throws Exception {
logger.info("getSegmentedRegions at given TessPageIteratorLevel");
File imageFile = new File(testResourcesDataPath, "ocr.png");
BufferedImage bi = ImageIO.read(imageFile);
int level = TessPageIteratorLevel.RIL_SYMBOL;
logger.info("PageIteratorLevel: " + Utils.getConstantName(level, TessPageIteratorLevel.class));
List<Rectangle> result = instance.getSegmentedRegions(bi, level);
for (int i = 0; i < result.size(); i++) {
Rectangle rect = result.get(i);
logger.info(String.format("Box[%d]: x=%d, y=%d, w=%d, h=%d", i, rect.x, rect.y, rect.width, rect.height));
} assertTrue(result.size() > 0);
} /**
* Test of doOCR method, of class Tesseract.
* 根据定义坐标范围进行识别
* @throws Exception while processing image.
*/
@Test
public void testDoOCR_File_Rectangle() throws Exception {
logger.info("doOCR on a BMP image with bounding rectangle");
File imageFile = new File(this.testResourcesDataPath, "ocr.png");
//设置语言库
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim");
//划定区域
// x,y是以左上角为原点,width和height是以xy为基础
Rectangle rect = new Rectangle(84, 21, 15, 13);
String result = instance.doOCR(imageFile, rect);
logger.info(result);
} /**
* Test of createDocuments method, of class Tesseract.
* 存储结果
* @throws java.lang.Exception
*/
@Test
public void testCreateDocuments() throws Exception {
logger.info("createDocuments for png");
File imageFile = new File(this.testResourcesDataPath, "ocr.png");
String outputbase = "target/test-classes/docrenderer-2";
List<RenderedFormat> formats = new ArrayList<RenderedFormat>(Arrays.asList(RenderedFormat.HOCR, RenderedFormat.TEXT)); //设置语言库
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim"); instance.createDocuments(new String[]{imageFile.getPath()}, new String[]{outputbase}, formats);
} /**
* Test of getWords method, of class Tesseract.
* 取词方法
* @throws java.lang.Exception
*/
@Test
public void testGetWords() throws Exception {
logger.info("getWords");
File imageFile = new File(this.testResourcesDataPath, "ocr.png"); //设置语言库
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim"); //按照每个字取词
int pageIteratorLevel = TessPageIteratorLevel.RIL_SYMBOL;
logger.info("PageIteratorLevel: " + Utils.getConstantName(pageIteratorLevel, TessPageIteratorLevel.class));
BufferedImage bi = ImageIO.read(imageFile);
List<Word> result = instance.getWords(bi, pageIteratorLevel); //print the complete result
for (Word word : result) {
logger.info(word.toString());
}
} /**
* Test of Invalid memory access.
* 处理倾斜
* @throws Exception while processing image.
*/
@Test
public void testDoOCR_SkewedImage() throws Exception {
//设置语言库
instance.setDatapath(testResourcesLanguagePath);
instance.setLanguage("chi_sim"); logger.info("doOCR on a skewed PNG image");
File imageFile = new File(this.testResourcesDataPath, "ocr_skewed.jpg");
BufferedImage bi = ImageIO.read(imageFile);
ImageDeskew id = new ImageDeskew(bi);
double imageSkewAngle = id.getSkewAngle(); // determine skew angle
if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD))) {
bi = ImageHelper.rotateImage(bi, -imageSkewAngle); // deskew image
} String result = instance.doOCR(bi);
logger.info(result);
}

Tess4JDemo码云地址:https://gitee.com/zhaohuihbwj/Tess4JDemo

Java OCR文字识别(Tess4J)

2017年10月17日 10:11:10

阅读数:6372

OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程;即,针对印刷体字符,采用光学的方式将纸质文档中的文字转换成为黑白点阵的图像文件,并通过识别软件将图像中的文字转换成文本格式,供文字处理软件进一步编辑加工的技术。如何除错或利用辅助信息提高识别正确率,是OCR最重要的课题,ICR(Intelligent Character Recognition)的名词也因此而产生。衡量一个OCR系统性能好坏的主要指标有:拒识率、误识率、识别速度、用户界面的友好性,产品的稳定性,易用性及可行性等。

Tess4J是对google tesseract ocr的java库的一种实现

1.maven添加依赖

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.2.1</version>
</dependency>

2.工具类编辑

/**
* tesseract for javaocrOptical Character Recognition,光学字符识别)
* 工具类
* @author wind
*/
public class Tess4jUtils {
/**
* 从图片中提取文字,默认设置英文字库,使用classpath目录下的训练库
* @param path
* @return
*/
public static String readChar(String path){
// JNA Interface Mapping
ITesseract instance = new Tesseract();
// JNA Direct Mapping
// ITesseract instance = new Tesseract1();
File imageFile = new File(path);
//In case you don't have your own tessdata, let it also be extracted for you
//这样就能使用classpath目录下的训练库了
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
//Set the tessdata path
instance.setDatapath(tessDataFolder.getAbsolutePath());
//英文库识别数字比较准确
instance.setLanguage(Const.ENG);
return getOCRText(instance, imageFile);
} /**
* 从图片中提取文字
* @param path 图片路径
* @param dataPath 训练库路径
* @param language 语言字库
* @return
*/
public static String readChar(String path, String dataPath, String language){
File imageFile = new File(path);
ITesseract instance = new Tesseract();
instance.setDatapath(dataPath);
//英文库识别数字比较准确
instance.setLanguage(language);
return getOCRText(instance, imageFile);
} /**
* 识别图片文件中的文字
* @param instance
* @param imageFile
* @return
*/
private static String getOCRText(ITesseract instance, File imageFile){
String result = null;
try {
result = instance.doOCR(imageFile);
} catch (TesseractException e) {
e.printStackTrace();
}
return result;
} public static void main(String[] args) {
/*String path = "src/main/resources/image/text.png";
System.out.println(readChar(path));*/ String ch = "src/main/resources/image/ch.png";
System.out.println(readChar(ch, "src/main/resources", Const.CHI_SIM));
} }
注释:中文结果不够准确,需要自己训练字库
具体训练字库,以及完整代码请移步https://github.com/followwwind/javautils

JAVA使用Tess4J进行ocr识别的更多相关文章

  1. java 使用tess4j实现OCR的最简单样例

    网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...

  2. 【Selenium-WebDriver实战篇】基于java的selenium之验证码识别内容

    ==================================================================================================== ...

  3. 百度DMLC分布式深度机器学习开源项目(简称“深盟”)上线了如xgboost(速度快效果好的Boosting模型)、CXXNET(极致的C++深度学习库)、Minerva(高效灵活的并行深度学习引擎)以及Parameter Server(一小时训练600T数据)等产品,在语音识别、OCR识别、人脸识别以及计算效率提升上发布了多个成熟产品。

    百度为何开源深度机器学习平台?   有一系列领先优势的百度却选择开源其深度机器学习平台,为何交底自己的核心技术?深思之下,却是在面对业界无奈时的远见之举.   5月20日,百度在github上开源了其 ...

  4. 基于Tesseract的OCR识别小程序

    一.背景 先说下开发背景,今年有次搬家找房子(2020了应该叫去年了),发现每天都要对着各种租房广告打很多电话.(当然网上也找了实地也找),每次基本都是对着墙面看电话号码然后拨打,次数一多就感觉非常麻 ...

  5. 基于Tesseract组件的OCR识别

    基于Tesseract组件的OCR识别 背景以及介绍 欲研究C#端如何进行图像的基本OCR识别,找到一款开源的OCR识别组件.该组件当前已经已经升级到了4.0版本.和传统的版本(3.x)比,4.0时代 ...

  6. Atitit  ocr识别原理 与概论 attilax总结

    Atitit  ocr识别原理 与概论 attilax总结 1.1. Ocr的过程与流程1 1.2. OCR不同技术细分略有不同,但大概原理是一样的. 即主要技术过程是:二值化(又叫归一化)----- ...

  7. 基于Python实现对PDF文件的OCR识别

    http://www.jb51.net/article/89955.htm https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-pytho ...

  8. OCR识别-python版(一)

    需求:识别图片中的文字信息环境:windows系统 开发语言:python 使用工具类:1.pyocr 2.PIL 3.tesseract-ocr 步骤: 1.pyocr 网络通直接使用命令:pip ...

  9. 汽车Vin码识别——可以嵌入到手机里的新OCR识别技术

              汽车Vin码识别(车架号识别),顾名思义,就是识别汽车的Vin码(车架号),汽车Vin码识别(车架号识别)利用的是OCR识别技术,支持视频流获取图像,自动触发识别,另外汽车Vin码 ...

随机推荐

  1. c++获取键盘输入cin、scanf使用详解

    cin是c++标准,scanf是在c中使用的 #include<cstdio> #include<iostream> #include<cstring> using ...

  2. Python实例讲解 -- wxpython 基本的控件 (按钮)

    使用按钮工作 在wxPython 中有很多不同类型的按钮.这一节,我们将讨论文本按钮.位图按钮.开关按钮(toggle buttons )和通用(generic )按钮. 如何生成一个按钮? 在第一部 ...

  3. system.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0) 问题

    页面控件没有做限制.提交后还可以继续点击,造成了在短时间内的多次请求.查看日志两次错误在200ms之内. 错误信息 system.Data.Entity.Infrastructure.DbUpdate ...

  4. HBase教程

    https://www.yiibai.com/hbase/ 自1970年以来,关系数据库用于数据存储和维护有关问题的解决方案.大数据的出现后,好多公司实现处理大数据并从中受益,并开始选择像 Hadoo ...

  5. 查询某个SPID,session_id对应的执行sql.

    select er.session_id, CAST(csql.text AS varchar(255)) AS CallingSQL from master.sys.dm_exec_requests ...

  6. vue项目中, 字段信息为空时不渲染,是真的不渲染吗

    代码 样式,宽度pro_proj 此class设置有宽度,考虑字数如果太多就进入下一行就没有设置高度 样式改为 数据 渲染. 开始以为,null时vue不渲染,然后审查元素,发现渲染的dom是存在的, ...

  7. S域传递函数的零点和极点

    传递函数的极点就是对应微分方程的特征根(回忆一下,$\frac{1}{s+a}$是$e^{-a t}$的Laplace变换),因此它们决定了系统自由运动的模态. 传递函数的零点不直接形成自由运动的模态 ...

  8. CentOS 7下NFS Server作rootfs时的兼容性问题

    最近新装CentOS 7,发现原先CentOS 6.3下可用的一块ARM Dev board不能用了,表现为VFS mount挂载rootfs失败. 使用WireShark发现,服务器对client发 ...

  9. [蓝桥杯]ALGO-116.算法训练_最大的算式

    问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号 ...

  10. gcc编译工具生成动态库和静态库之一----介绍

     1.库的分类 根据链接时期的不同,库又有静态库和动态库之分. 静态库是在链接阶段被链接的(好像是废话,但事实就是这样),所以生成的可执行文件就不受库的影响了,即使库被删除了,程序依然可以成功运行. ...