JAVA Tesseract OCR引擎

Tess4j是一个基于Tesseract OCR引擎的Java库, Tesseract库最初由惠普实验室于1985年开发,后来被Google收购并于2006年开源。识别效果不好,速度还慢,但是好早好早了。

一、POM依赖

XML 复制代码
   <!--OCR识别
        https://digi.bib.uni-mannheim.de/tesseract/tessdata_fast/ 训练文件下载
        -->
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>5.12.0</version>            
        </dependency>

        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.14.0</version>
        </dependency>

记的要下载训练文件 chi_sim.traineddata

二、配置文件 TesseractOcrConfiguration

java 复制代码
import cn.cakeerp.util.StrUtil;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TesseractOcrConfiguration {

    @Bean
    public Tesseract tesseract() {
        Tesseract tesseract = new Tesseract();
        // 设置训练数据文件夹路径
        tesseract.setDatapath(StrUtil.erpConfig.getTraineddata());
        // 设置为中文简体
        tesseract.setLanguage("chi_sim");
        return tesseract;
    }
}

三、使用

java 复制代码
    @Resource
    private Tesseract tesseract;

    //直接就可以识别,也可以
    System.out.println(tesseract.doOCR(new File("d:\\2.jpg")));

    //也可以从 MultipartFile imageFile 里面识别

        InputStream is = null;
        try {
            is = new ByteArrayInputStream(imageFile.getBytes());
            BufferedImage bufferedImage = ImageIO.read(is);
            String textStr = tesseract.doOCR(bufferedImage);
            if (null == textStr || textStr.trim().equals("")) {
                return JsonResult.failed("识别失败,结果为空.");
            }          
            log.error("识别内容为:{}", textStr);
            return JsonResult.failed("未识别到订单号", -1);
        } catch (Exception e) {
            return JsonResult.failed(e.getMessage(), -1);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

后续 在官方的仓库中下载到了最新的训练文件 50.2MB结果测试效果还是没啥变化,里面也没有什么可配置的参数。不知道别人怎么用呢。

tessdoc/Data-Files.md at main · tesseract-ocr/tessdoc · GitHub

相关推荐
居7然19 小时前
DeepSeek OCR:重新定义AI文档处理的“降本增效”新范式
人工智能·算法·语言模型·自然语言处理·大模型·ocr
AI人工智能+1 天前
从海量文档到精准数据:文档抽取技术驱动金融财税决策新范式
人工智能·nlp·ocr·文档抽取
余俊晖2 天前
RLVR训练多模态文档解析模型-olmOCR 2技术方案(模型、数据和代码均开源)
人工智能·算法·ocr·grpo
Coovally AI模型快速验证2 天前
突破性开源模型DepthLM问世:视觉语言模型首次实现精准三维空间理解
人工智能·语言模型·自然语言处理·ocr·音视频·ai编程
山川而川-R3 天前
图像进行拼接-后进行ocr检测识别
人工智能·计算机视觉·ocr
AI人工智能+3 天前
从“手动填”到“自动识”:营业执照识别技术革新政务体验
人工智能·深度学习·ocr·营业执照识别
乾坤瞬间3 天前
【Deepseek OCR】重磅测试,mac环境下的体验【本人已经本地实验成功】
macos·ocr
周杰伦_Jay3 天前
【PaddleOCR深度解析与DeepSeek-OCR对比】开源OCR工具库的技术路线与场景适配
人工智能·机器学习·云原生·架构·开源·ocr
wwlsm_zql3 天前
DeepSeek-OCR 技术文档:视觉-文本压缩驱动的端到端OCR模型
ocr
翔云 OCR API3 天前
OCR文字识别技术-云端识别引擎-OCR API接口
ocr