JAVA Tesseract OCR引擎

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

一、POM依赖 

   <!--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

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;
    }
}

三、使用

    @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 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值