功能:简单实现,图片验证码的文字识别,填充到文本框 注:文件读写权限
- 引入依赖:源码地址
implementation 'com.rmtheis:tess-two:9.1.0'`
- 下载语言包,放到assets目录下,我这里文字都是数据,只引入了eng的语言包
https://github.com/tesseract-ocr/tessdata/tree/3.04.00 - 识别功能核心代码
public static void recognition(final Context context, final Bitmap bitmap) {
new Thread(new Runnable() {
@Override
public void run() {
String DATAPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
String LANGUAGE_PATH = DATAPATH + File.separator + "tessdata";
String FILE_PATH = LANGUAGE_PATH + File.separator + "eng.traineddata";
File file = new File(FILE_PATH);
if (!file.exists()) {
SDUtils.assets2SD(context.getApplicationContext(), LANGUAGE_PATH, "eng.traineddata");
}
TessBaseAPI tessBaseAPI = new TessBaseAPI();
tessBaseAPI.setDebug(true);
tessBaseAPI.init(DATAPATH, "eng");
tessBaseAPI.setImage(bitmap);
String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
LiveEventBus.get("imgCode").post(text);
}
}).start();
}
|