android ocr 图像识别之tess-two试用

不需要任何so库,也兼容6.0以上版本

把tess-two添加到项目里,在app 的build.gradle里添加下面

dependencies {
    compile 'com.rmtheis:tess-two:7.0.0'
}
 
 
  • 1
  • 2
  • 3

然后从https://github.com/tesseract-ocr/tessdata/tree/3.04.00 下载项目需要的训练语言数据,下载后复制,到assets/tessdata目录下,之后把它复制到SD卡。

  /**
     * 初始化ocr识别需要用到的训练数据
     */
private void initOcr() {
        datapath = getFilesDir() + "/tesseract/";
        checkFile(new File(datapath + "tessdata/"), "chi_sim");
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
/**
     * @param dir
     * @param language chi_sim eng
     */
    private void checkFile(File dir, String language) {
        //如果目前不存在则创建方面,然后在判断训练数据文件是否存在
        if (!dir.exists() && dir.mkdirs()) {
            copyFiles(lag);
        }
        if (dir.exists()) {
            String datafilepath = datapath + "/tessdata/" + language + ".traineddata";
            File datafile = new File(datafilepath);
            if (!datafile.exists()) {
                copyFiles(lag);
            }
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
/**
 把训练数据放到手机内存
 * @param language "chi_sim" ,"eng"
 */
    private void copyFiles(String language) {
        try {
            String filepath = datapath + "/tessdata/" + language + ".traineddata";
            AssetManager assetManager = getAssets();
            InputStream instream = assetManager.open("tessdata/" + language + ".traineddata");
            OutputStream outstream = new FileOutputStream(filepath);
            byte[] buffer = new byte[1024];
            int read;
            while ((read = instream.read(buffer)) != -1) {
                outstream.write(buffer, 0, read);
            }
            outstream.flush();
            outstream.close();
            instream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

做完准备工作之后,待相机取到图片之后,调用doOcr方法即可获得识别结果,需要注意的是,该方法需要再子线程进行。

  public void doOcr(Bitmap bitmap, String language) {

        TessBaseAPI baseApi = new TessBaseAPI();
        baseApi.init(datapath, language);
        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        baseApi.setImage(bitmap);
        String resultTxt = baseApi.getUTF8Text();
        baseApi.clear();
        baseApi.end();
        //get resultTxt to do something
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值