Java 使用 Tesseract 识别图片文字

Java 使用 Tesseract 识别图片文字

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>5.8.0</version>
</dependency>
  • 找一个磁盘放语言训练包
  • 代码测试(Java)
/**
 * 图片识别
 *
 * @param sb          字符串
 * @param file        文件
 * @param isIncreaseContrast 是否对比度增强
 * @return 结果
 */
public static String getImageContentStr(StringBuilder sb, File file, boolean isIncreaseContrast) {
    try {
        ITesseract tesseract = new Tesseract();
        // 设置识别训练包
        tesseract.setDatapath("D:\\tessdata");
        // 设置识别语言
        tesseract.setLanguage("chi_sim");
        // 解决图片编码方式导致的识别问题
        BufferedImage bufferedImage = ImageIO.read(file);
        if (isIncreaseContrast) {
            // 对比度增强算法
            bufferedImage = increaseContrast(bufferedImage);
        }

        // 识别图片内容
        String text = tesseract.doOCR(bufferedImage);
        sb.append(text);
    } catch (TesseractException | IOException e) {
        System.out.println("图片识别失败:" + e);
        e.printStackTrace();
        return "";
    }
    return sb.toString();
}

public static void main(String[] args) {
    File file = new File("F:\\桌面\\休闲\\图片识别5.png");
    StringBuilder sb = new StringBuilder();
    String imageContentStr = FileManageService.getImageContentStr(sb, file, true);
    System.out.println("图片识别成功:" + imageContentStr);
}
  • 识别图
    在这里插入图片描述

  • 效果图

在这里插入图片描述

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Tesseract是一个开源的OCR(Optical Character Recognition,光学字符识别)引擎,可以将图片中的文字转换为可编辑的文本。在Java中,可以使用Tesseract4J库来集成Tesseract引擎。 以下是使用Tesseract4J进行文本识别的步骤。 步骤1:安装Tesseract引擎 首先,需要安装Tesseract引擎。在Windows系统中,可以从Tesseract官网下载安装程序并进行安装。在Linux系统中,可以使用以下命令安装: sudo apt-get install tesseract-ocr 步骤2:添加Tesseract4J依赖项 在Java项目中,需要添加Tesseract4J依赖项。可以使用Maven或Gradle管理依赖项,也可以手动下载jar文件并添加到项目中。 Maven依赖项: <dependency> <groupId>com.github.wtekiela</groupId> <artifactId>tesseract4j</artifactId> <version>1.5.1</version> </dependency> Gradle依赖项: implementation 'com.github.wtekiela:tesseract4j:1.5.1' 步骤3:编写代码 在Java代码中,可以使用Tesseract4J提供的Tesseract类来识别图片中的文本。以下是一个简单的示例代码: import java.io.File; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; public class OCR { public static void main(String[] args) { File imageFile = new File("image.jpg"); Tesseract tesseract = new Tesseract(); try { String result = tesseract.doOCR(imageFile); System.out.println(result); } catch (TesseractException e) { System.err.println(e.getMessage()); } } } 在上面的示例中,首先创建了一个File对象来表示待识别图片文件。然后创建了一个Tesseract对象,并调用doOCR()方法来进行文本识别。最后将识别结果输出到控制台。 步骤4:运行代码 将上述代码保存为Java文件,并编译运行。程序将加载Tesseract引擎并使用它来识别图片中的文本。在控制台上将输出识别结果。 需要注意的是,Tesseract引擎不是100%准确的。在一些情况下,它可能无法正确识别文本,或者产生一些错误的识别结果。因此,在实际应用中,需要根据具体情况对识别结果进行检查和修正。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值