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 }