什么都不必说--简单图片金额识别OCR

需求 获取图片中金额
复制代码

1.添加maven

  <!--图片识别-->
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jna</groupId>
                    <artifactId>jna</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
复制代码

2.安装tessdata

brew install tesseract 
复制代码

3.工具类

public class OCRUtil {
    private static String  tessdataPath;
    private static String STRING_TESS_VARIABLE_KEY = "tessedit_char_whitelist";
    private static String STRING_TESS_VARIABLE_VALUE = "0123456789.¥";
    private static String STRING_TESS_NAME = "tessdata";

    private ITesseract instance;

    private static OCRUtil ocrUtil;

    private OCRUtil(ITesseract instance){
        this.instance = instance;
    }

    private static final LogUtil logUtil  = LogUtil.init(OCRUtil.class);

    //必须加锁,在高并发情况下会出现jvm崩坏情况
    public  synchronized String getAmount(BufferedImage bi) throws IOException {
        long start = System.currentTimeMillis();
        try {

            String ocrResult = instance.doOCR(bi);
            if(StringUtil.isEmpty(ocrResult)){
                return null;
            }
            if(ocrResult.indexOf("¥") == -1){
                return null;
            }
            logUtil.i("金额识别 为:%s\n金额识别耗时:%s毫秒",ocrResult,System.currentTimeMillis()-start);
            return ocrResult.replace("\n","").substring(ocrResult.indexOf("¥") + 1).trim();
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
        return null;
    }

    public static synchronized OCRUtil init(){

        if(null == ocrUtil){
            ITesseract instance = new Tesseract(); // JNA Direct Mapping
            instance.setTessVariable(STRING_TESS_VARIABLE_KEY, STRING_TESS_VARIABLE_VALUE);
//            File tessDataFolder = LoadLibs.extractTessResources(STRING_TESS_NAME);
//            logUtil.d(tessDataFolder.getAbsolutePath());
            //从外部获取tessdataPath或者查询tessdata所在位置
            instance.setDatapath(tessdataPath);
            ocrUtil = new OCRUtil(instance);
        }

        return ocrUtil;

    }

}

复制代码

转载于:https://juejin.im/post/5b39d1bb6fb9a00e43467df9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值