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


相关推荐

  • cmd cd到d盘切换不过去_cmd删除盘符

    cmd cd到d盘切换不过去_cmd删除盘符今天使用dos命令行切换盘符突然发现无法切换(Win7系统)。瞬间感觉就不好了。解决办法:1直接用命令:e:即可2命令:cd/de:可是cde:命令是干什么用的呢?是用来切换e盘的工作目录的.(你cdf:,就是切换f盘的工作目录)如果我们输入cde:之后将盘符切换到E盘,运行截图为(好像效果不明显):如

    2022年9月27日
    7
  • portraiture 3 mac(最强ps人像美化磨皮滤镜)内附安装教程

    portraiture 3 mac(最强ps人像美化磨皮滤镜)内附安装教程imagenomicportraituremac是一款非常强大的ps人像美化磨皮滤镜,可以更好的辅佐我们的ps进行人像滤镜美化处理。portraituremac激活成功教程版能够智能的对图像中的肤色、毛发以及眉毛等部位进行滤镜抛光处理,细节处理,以减少瑕疵。portraituremac激活成功教程版基本上是人人都能用得上的ps辅助工具,有了它处理人像效果更加显著。小编现为您带来portraiture3滤镜下载,需要的小伙伴快来下载吧!下载地址:https://mac.orsoon.com/Mac/167.

    2022年7月22日
    12
  • SOAP协议详解「建议收藏」

    SOAP协议详解「建议收藏」一.概念:SOAP:简单对象访问协议,是交换数据的一种协议规范,它是一个轻量级\简单的\基于xml的协议;它被设计成在WEB上交换结构化的和固化的信息。二.相关定义1.基于类对象的传输协议;2.SOAP封装(envelop),它定义了一个框架,描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们;3.SOAP编码规则(encodingrules),它定义了一种序列化机制,用

    2022年7月12日
    37
  • matplotlib绘图基础[通俗易懂]

    matplotlib绘图基础[通俗易懂]http://blog.csdn.net/pipisorry/article/details/37742423matplotlib介绍matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。它的文档相当完备,并且Gallery页面中有上百幅缩略图,打开之后都

    2022年5月24日
    46
  • matlab 三维数据多项式拟合,matlab多项式拟合

    matlab 三维数据多项式拟合,matlab多项式拟合在命令窗口下输入 x y cftool 在 CurveFitting 界面中 单击 Data 选择 xData 和 yData 选择 Createdatase 选择 Close 在 CurveFitting 界面中 单击 Fitting 选择 Newfit 选择 Typeoffit 选择 Polynomial 选择 9thdegreepol 打勾 Centerandsca

    2025年11月12日
    3
  • tensor数据转cpu——gpu的tensor转numpy

    tensor数据转cpu——gpu的tensor转numpy仅作为记录,大佬请跳过。用target.cpu().numpy()展示参考传送门

    2022年10月18日
    6

发表回复

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

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