Java使用Tess4j实现对于图片(jpg/png/jpeg)内容的提取

本文详细介绍了如何在Windows和Linux系统中使用TesseractOCR库进行图像文字识别,包括依赖管理、网络资源处理、文件操作以及在不同环境下的Tesseract配置和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Windows使用

1.引入依赖

<dependency>
   <groupId>net.sourceforge.tess4j</groupId>
   <artifactId>tess4j</artifactId>
   <version>4.5.5</version>
</dependency>

<dependency>
   <groupId>org.apache.lucene</groupId>
   <artifactId>lucene-core</artifactId>
   <version>8.11.1</version> <!-- 版本号根据您的实际需求进行调整 -->
</dependency>

<dependency>
   <groupId>org.apache.ant</groupId>
   <artifactId>ant</artifactId>
   <version>1.10.11</version>
</dependency>

2.代码实现

try {
    //创建一个网络资源对象
     URL image = new URL(url);
     //创建文件对象
     File file = new File("命名一个临时文件");
     //将网络资源文件对象中的内容复制到文件中
     FileUtils.copyURLToFile(image, file);
     //创建Tesseract识别对象
     Tesseract tesseract = new Tesseract();
     tesseract.setDatapath("训练库地址");
     //设置识别语言,对应训练库名称
     tesseract.setLanguage("chi_sim");
     String result;
     BufferedImage imageRead = ImageIO.read(file);
     result = tesseract.doOCR(imageRead);
     stringContent.append(result);
     file.delete();
     } catch (IOException e) {

     } catch (TesseractException e) {

     }

2.Linux使用

1.在linux系统上安装Tesseract

参靠下面博主的文章

【tesseract】Linux环境安装tesseract教程(一)_linux tesseract-优快云博客

2.程序中拷贝生成so文件

将下面文件夹中的这两个文件拷贝到Java程序中resource下的linux-x86-64文件夹中

3.程序中使用同windows一样的依赖

4.代码实现

try {
    //创建一个网络资源对象
     URL image = new URL(url);
     //创建文件对象
     File file = new File("命名一个临时文件");
     //将网络资源文件对象中的内容复制到文件中
     FileUtils.copyURLToFile(image, file);
     //创建Tesseract识别对象
     Tesseract tesseract = new Tesseract();
     //linux环境
     //安装Tessercat的时候,将这个路径配置在环境变量中了
     String dataPath = System.getenv("TESSDATA_PREFIX");
     tesseract.setDatapath(dataPath);
     //设置识别语言,对应训练库名称
     tesseract.setLanguage("chi_sim");
     String result;
     BufferedImage imageRead = ImageIO.read(file);
     result = tesseract.doOCR(imageRead);
     stringContent.append(result);
     file.delete();
     } catch (IOException e) {

     } catch (TesseractException e) {

     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aqua️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值