【Java-TesseractOCR】通过Java实现OCR

通过Java实现OCR

一、TesseractOCR

本文使用的是TesseractOCR进行识别

二、引入pom

xml 复制代码
 <dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>5.8.0</version>
</dependency>

训练集下载地址

三、引入训练集

训练集下载地址

下载训练集 放入到 resources下的tessdata文件夹下

三、使用

java 复制代码
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import java.io.File;

/**
 * @author Mr.NaCl
 * @since 2024/2/5
 */
public class OCRServer {

    public static void main(String[] args) throws TesseractException {
        long start = System.currentTimeMillis();
        System.out.println("开始OCR文字识图,请稍后...");
        //加载要识别的图片
        File image = new File("14-OCR/src/main/resources/test.jpg");
        //设置配置文件夹位置、识别语言、识别模式
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath("14-OCR/src/main/resources/tessdata");
        //设置识别语言为中文简体,(如果要设置为英文可改为"eng")
        tesseract.setLanguage("chi_sim");
        //使用 OSD 进行自动页面分割以进行图像处理
        tesseract.setPageSegMode(1);
        //设置引擎模式是神经网络LSTM引擎
        tesseract.setOcrEngineMode(1);
        //开始识别整张图片中的文字
        String result = tesseract.doOCR(image);
        //如果只想识别图片中特定部分的文字,可以像下面这样选定长宽范围识别:
        //String result = tesseract.doOCR(image, new Rectangle(300, 200));
        long time = System.currentTimeMillis() - start;
        System.out.println("识别结束,耗时:" + time + " 毫秒,识别结果如下:");
        System.out.println(result);
    }
}
相关推荐
Java成神之路-1 分钟前
Spring AI Alibaba 实现多轮对话记忆:ChatMemory 与 Redis 持久化实战
java·springaialibaba
JAVA面经实录9174 分钟前
网络编程基础(Java Web/分布式前置·完整版)(十一)
java·前端·网络
W_3260010 分钟前
Python 常用标准 / 第三方库:random、tqdm、turtle、jieba 用法
开发语言·python
Escalating_xu18 分钟前
【Linux】基础 I/O 深度解析:FILE、文件描述符、open/read/write、重定向与缓冲区
java·linux·服务器
城管不管21 分钟前
MySQL 慢查询完整排查
java·服务器·jvm·数据库·mysql·spring·面试
ZC跨境爬虫32 分钟前
LeetCode 88. 合并两个有序数组(双指针详解 + Java Python 实现)
java·python·算法·leetcode
caimouse36 分钟前
ReactOS 图形系统分析(17):区域填充 — EngPaint / IntEngPaint(paint.c)
c语言·开发语言·算法
北风toto43 分钟前
研发效能与后端核心技术全景指南:从CI/CD到共性组件实战
java·开发语言·ci/cd
Billy121381 小时前
Day12-C++20 Coroutines(上):协程原理与Promise/Awaitable机制
开发语言·c++进阶学习
01二进制代码漫游日记1 小时前
C++基础入门速通
java·开发语言·c++