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


相关推荐

  • 源码网_python 源代码

    源码网_python 源代码源码结构目录可以看到此模块定义了4个属性和12个函数,我们依次来讲解属性源码分析#匹配http://或https://absolute_http_url_regexp=re.compil

    2022年7月29日
    5
  • 如何理解java方法的传值和传引用的参数传递方式_指针参数传递

    如何理解java方法的传值和传引用的参数传递方式_指针参数传递结论:1)当使用基本数据类型作为方法的形参时,在方法体中对形参的修改不会影响到实参的数值2)当使用引用数据类型作为方法的形参时,若在方法体中修改形参指向的数据内容,则会对实参变量的数值产生影响,因为形参变量和实参变量共享同一块堆区;3)当使用引用数据类型作为方法的形参时,若在方法体中修改形参变量的指向,此时不会对实参变量的数值产生影响,因此形参变量和实参变量分别指向不同的堆区例一:基本数据类型作为形参,运行结果不改变实参publicclassMain{publicstatic

    2022年8月30日
    9
  • pycharm设置背景色及字体_pycharm字体大小设置不了

    pycharm设置背景色及字体_pycharm字体大小设置不了1、首先在pycharm打开FILE,点击seting2、点击Editor,再点击Font可以设置字体大小;点击ColorScheme可以设置背景色。3、字体设置:点击Font可以选择不同字体,点击Size可以调节字体大小,右侧可以看预览效果,最后点击ok即可。4、背景色设置:先点击ColorSchemeFont,Scheme可以选择不同的背景,下面框框里是预览效果,最后点击Ok即可。…

    2022年8月29日
    2
  • cpu overclocking_cpu memory overclocking

    cpu overclocking_cpu memory overclockingWhydoeskworkerhogyourCPU?TofindoutwhyakworkeriswastingyourCPU,youcancreateCPUbacktraces:watchyourprocessorload(withtoporsomething)andinmomentsofhighloadthroughkworker,executeechol>/proc/sysrq-triggertocreatea

    2025年12月6日
    3
  • ubuntu eclipse 乱码「建议收藏」

    ubuntu eclipse 乱码「建议收藏」今天,把windows下的工程导入到了Linux下eclipse中,由于以前的工程代码,都是GBK编码的(Windows下的Eclipse默认会去读取系统的编码,所以Widnwos下的Eclipse的编码为GBK),而Ubuntu默认是不支持GBK编码的。所以,首先我们要先让Ubuntu支持GBK,方法如下:修改/var/lib/locales/supported.d/local文

    2022年5月25日
    36
  • JS 怎么控制 checkbox 选中

    JS 怎么控制 checkbox 选中2019独角兽企业重金招聘Python工程师标准>>>…

    2022年7月15日
    58

发表回复

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

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