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);

}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 3D编程软件(3d动画需要编程吗)

    本篇文章中,我们学习了UnityShader的基本写法框架,以及学习了Shader中Properties(属性)的详细写法,光照、材质与颜色的具体写法。写了6个Shader作为本文Shader讲解的实战内容,最后创建了一个逼真的暴风雪场景进行了Shader的测试。依旧是国际惯例先上本文配套程序的截图。先是一张远眺图:

  • 超详细的springBoot学习笔记

    超详细的springBoot学习笔记SpringBoot1.SpringBoot简介Spring诞生时是Java企业版(JavaEnterpriseEdition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的EnterpriseJavaBean(EJB),Spring为企业级Java开发提供了一种相…

    2022年10月21日
  • 学习Spring框架这一篇就够了

    学习Spring框架这一篇就够了1.spring概述1.1Spring是什么(理解)Spring是分层的JavaSE/EE应用full-stack轻量级开源框架,以IoC(InverseOfControl:反转控制)和AOP(AspectOrientedProgramming:面向切面编程)为内核。提供了展现层SpringMVC和持久层SpringJDBCTemplate以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的JavaEE企业应用

  • 查看tensorflow版本信息

    查看tensorflow版本信息1.输入cmd进入控制台2.输入python3.输入importtensorflowastf和tf.__version__4.如果想要查看tensorflow所在文件夹可以用tf.__path__

  • 无人驾驶安全报告分析

    摘要随着经济的快速发展,各国汽车保有量急剧增加,促使城市路况更加严峻繁杂,城市交通正面临着前所未有的巨大压力。加之疲劳驾驶、酒后驾驶等人为因素,使世界各国的交通事故率逐年上升,甚至多于世界大战死亡人数。随着汽车技术、信息通信技术与智能控制技术的高效融合,集自动控制、人工智能、体系结构视觉设计等众多技术于一体的无人驾驶汽车应运而生。通过在车辆内安装智能操纵控制系统与感应设备来获取信息用以控制车…

  • 三菱PLC学习方法分享

    三菱PLC学习方法分享  学习plc不是为了研究,而是为了工作需要和应用。下面是我学习PLC的方法,供大家参考!  与PLC自动化相关的知识点很多,比如PLC控制、模拟控制、定位、通信、配置、嵌入式系统、变频器、机械传动、液压系统(不知道是不是自动化)等。知识点太多。以常见的PLC为例,三菱、西门子、欧姆龙、松下、LG、达美、卡恩斯等品牌,加上各种国产品牌和众多知识点。如何学好它们?我理解的学习方法是“巧学”。  以我熟悉的三菱plc为例:  一、基础:一定要打好基础——基础是什么?  1.安装编程软件。  

发表回复

您的电子邮箱地址不会被公开。

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