java 图片识别 tess4j_JAVA使用Tess4J进行ocr识别

java 图片识别 tess4j_JAVA使用Tess4J进行ocr识别Tess4J是对TesseractOCRAPI.的JavaJNA封装。使java能够通过调用Tess4J的API来使用TesseractOCR。支持的格式:TIFF,JPEG,GIF,PNG,BMP,JPEG,andPDFTesseract的github地址:https://github.com/tesseract-ocr/tesseractTess4J的github地址:https…

大家好,又见面了,我是你们的朋友全栈君。

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 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 formats = new ArrayList(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 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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136868.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2020年精心收集的十个Java开发网站

    2020年精心收集的十个Java开发网站不管谁手里都藏着些许自己觉得好用的网站,今天专门找大厂出来的同学同事觉得好用的网站分享给大家,如果这里有你没收藏还不知道觉得还蛮有用的网站可以给我点个赞,大家一起进步,一起学习,同时也可以分享你觉得好用实用的网站,分享快乐0.0好了,废话不多说,咋们上干货:一、GithubGitHub是一个面向开源及私有软件项目的托管平台,一个拥有数十亿行代码的网站,每天有数百万开发者聚集在一起,研究开源软件中存在的问题。对于我们程序猿来说,如果不想重复造轮子,就必须要站在巨人的肩膀上,那么巨人是谁呢?就是Gi

    2022年7月7日
    17
  • linux中更改用户名_linux修改用户名和主目录

    linux中更改用户名_linux修改用户名和主目录Linux将用户名修改后,还需要修改组名+家目录+UID这只会更改用户名,而其他的东西,比如用户组,家目录,UID等都保持不变。1、修改用户名$usermod-l新用户旧用户这只会更改用户名,而其他的东西,比如用户组、家目录、ID等都保持不变。注意:你需要从要改名的帐号中登出并杀掉该用户的所有进程,要杀掉该用户的所有进程可以执行下面命令$sudopkill-u旧用户名$…

    2022年9月18日
    2
  • kettle 性能优化_kettle过滤记录

    kettle 性能优化_kettle过滤记录性能调优在整个工程中是非常重要的,也是非常有必要的。但有的时候我们往往都不知道如何对性能进行调优。其实性能调优主要分两个方面:一方面是硬件调优,一方面是软件调优。本章主要是介绍Kettle的性能优化及效率提升。……

    2022年9月20日
    3
  • Spring Boot拦截器配置拦截登陆「建议收藏」

    Spring Boot拦截器配置拦截登陆「建议收藏」一,pom.xml的配置这里很简单,先引入spring-boot-starter-parent,parent是父模块,由父模块统一进行spring-boot版本管理,dependencies中与spring-boot启动绑定的包不需要再指定版本。<parent><groupId>org.springframework.boot<…

    2022年7月25日
    18
  • 17款漂亮的 HTML5 网站模板免费下载「建议收藏」

    17款漂亮的 HTML5 网站模板免费下载「建议收藏」在过去的几年,设计师们已开始关注和使用HTML5了,如今HTML5得到了更加广泛的应用。今天,本文与大家分享17款免费的HTML5网站模板,看看有没自己喜欢的。

    2022年8月6日
    7
  • 数学建模【规划模型–线性规划(整数规划、0-1规划)、非线性规划-附:案例分析、奶制品的生产和销售(详细求解过程)】

    数学建模【规划模型–线性规划(整数规划、0-1规划)、非线性规划-附:案例分析、奶制品的生产和销售(详细求解过程)】4.1数学规划介绍1、数学规划模型的定义2、数学规划模型2.1、企业生产计划3、例1加工奶制品的生产计划3.1、整数规划(IntegerProgramming,简记IP)4、0-1规划模型选课策略5、非线性规划模型5.1、非线性规划5.2、基本概念5.3、算法概述5.4、MATLAB软件求解4.2奶制品的生产和销售1、优化模型和优化软件的重要意义2、优化(Optimization),规划(Programming)3、优化问题的一般形式

    2022年7月14日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号