Android图片文字识别

导言:
OCR,tess-two ,openCV等晕人的东西先分清,OCR,tess-two是图片文字识别,而openCV是图像识别比对

可运行的步骤:
1:添加依赖

 implementation 'com.rmtheis:tess-two:8.0.0'

2:下载字体识别库(chi_sim.traineddata 中文简体,chi_tra.traineddata 中文繁体,eng.traineddata 英文库)

3:为了apk的大小,我们需要将字体识别库文件拷贝到SD卡目录中,比如eng.traineddata的copy

  private String mDataPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  private String mFilePath = mDataPath + File.separator + "tessdata" + File.separator + "eng.traineddata";
  private void copyFile() {
        try {
            File mFile = new File(mFilePath);
            if (mFile.exists()) {
                mFile.delete();
            }
            if (!mFile.exists()) {
                File p = new File(mFile.getParent());
                if (!p.exists()) {
                    p.mkdirs();
                }
                try {
                    mFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            OutputStream os = new FileOutputStream(mFilePath);
            InputStream is = this.getAssets().open("eng.traineddata");
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4:tess two初始化

 TessBaseAPI baseApi;
 baseApi = new TessBaseAPI();
 baseApi.init(mDataPath, "eng");

5:处理bitmap图片并识别里面的内容

//OCR图片文字识别
baseApi.setImage(bitmap);
String result = baseApi.getUTF8Text().replace(" ", "").toLowerCase();

6:其他要求看需求,本文结束

猜你喜欢

转载自blog.csdn.net/ware00/article/details/80744587