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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 安装 jce_JCE安装

    java 安装 jce_JCE安装#!/bin/bashKERNEL=`rpm-qa|grepkernel-smp-devel-2.6.9-67.EL>/dev/null;echo$?`aliascp=’cp-i’unaliascpif[$KERNEL-eq1];thenrpm-ivhkernel-smp-devel-2.6.9-67.EL.i686.rpmfiSWCSMDIR=`cd…

    2022年6月26日
    38
  • JBPM工作流的性能问题「建议收藏」

    JBPM工作流的性能问题「建议收藏」前言:我计划把我的blog从51cto移到javaeye,陆陆续续地把我对工作流的理解贴上来,和大家交流。我在项目中应用过jbpm工作流,总体而言,jbpm是诸多开源workflow中比较好的一个。他的面向图的设计比起那些一味宣称遵守所谓的WfMC规范的工作流好多了。在应用的过程中,我发现jbpm还是有不少问题,其中性能问题比较突出。主要表现为:1、任务分配的表设计不合理,如果…

    2022年9月9日
    0
  • 重复字符串 leetcode_求字符串的最长无重复字符串

    重复字符串 leetcode_求字符串的最长无重复字符串原题链接给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: s = “abcabcbb”输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: s = “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: s = “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。 请注意,你的答案必须是 子串 的长度,”pwk

    2022年8月9日
    3
  • Java7 新特性 —— java.nio.file 文件操作

    Java7 新特性 —— java.nio.file 文件操作

    2020年11月19日
    144
  • LAMP网站架构方案分析[转]

    LAMP网站架构方案分析[转]什么是LAMP架构?LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,该框架包括:Linux操作系统,Apache网络服务器,MySQL数据库,Perl、PH

    2022年7月2日
    21
  • c++贪吃蛇源代码 完整版

    c++贪吃蛇源代码 完整版c++实现贪吃蛇,完整源码文章结构:数据结构分析程序运行分析难点分析一点思考源代码总结数据结构分析:1.双向队列:这里我才用双向队列的数据结构存储蛇身节点(这里的蛇身节点我才用结构体来存储蛇身信息)(因为贪吃蛇的实现还是比较简单的,所以也只涉及到了这一种数据结构)程序运行分析程序开始用户随机按下w,a,s,d中任意按键开始游戏并且作为蛇运动的初始方向,然后进入循环持…

    2022年5月26日
    57

发表回复

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

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