先导库
implementation 'com.rmtheis:tess-two:9.0.0'
9.0.0是目前最新的,https://github.com/rmtheis/tess-two可以去这个地方查看最新地址。
既然是图片识别,那一想就可知,需要传入一个图片bitmap,那么返回的肯定是图片中的内容String。
TessBaseAPI tessBaseAPI = new TessBaseAPI();
tessBaseAPI.init(DATAPATH, DEFAULT_LANGUAGE);
tessBaseAPI.setImage(bitmap);
String text = tessBaseAPI.getUTF8Text();
3行代码,就这么简单。单身么,先new个TessBaseAPI对象。 初始化。DATAPATH 和
DEFAULT_LANGUAGE如下 private static final String DATAPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
private static final String DEFAULT_LANGUAGE = "chi_sim";
然后呢,就做这些肯定是会报错的。我就踩了这个坑。SD卡权限。别忘了加。
6.0以上动态申请权限。你懂的。自己百度去吧。
然后有个copyToSD方法是将assets中的识别库复制到SD卡中。待会贴上完整代码。
assets目录下需要的文件在这里,自己去下。
https://github.com/tesseract-ocr/tessdata
有很多,我目前只用了
chi_sim.traineddata
eng.traineddata
hebing.traineddata
nor.traineddata
normal.traineddata
这5个,你可以根据需要加到assets下
第三个方法就是设置一个bitmap进去了,然后 tessBaseAPI.getUTF8Text();就可以获取读取到的内容了。非常简单。 贴上完整代码
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{ private static final String TAG = "Main2Activity"; private Button btn; private TextView tv; private ImageView cropView; /** * TessBaseAPI初始化用到的第一个参数,是个目录。 */ private static final String DATAPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator; /** * 在DATAPATH中新建这个目录,TessBaseAPI初始化要求必须有这个目录。 */ private static final String tessdata = DATAPATH + File.separator + "tessdata"; /** * TessBaseAPI初始化测第二个参数,就是识别库的名字不要后缀名。 */ private static final String DEFAULT_LANGUAGE = "chi_sim"; /** * assets中的文件名 */ private static final String DEFAULT_LANGUAGE_NAME = DEFAULT_LANGUAGE + ".traineddata"; /** * 保存到SD卡中的完整文件名 */ private static final String LANGUAGE_PATH = tessdata + File.separator + DEFAULT_LANGUAGE_NAME; /** * 权限请求值 */ private static final int PERMISSION_REQUEST_CODE=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.tv); cropView = (ImageView) findViewById(R.id.iv); Log.d("dddd",DATAPATH); btn.setOnClickListener(this); if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } } //Android6.0之前安装时就能复制,6.0之后要先请求权限,所以6.0以上的这个方法无用。 copyToSD(LANGUAGE_PATH, DEFAULT_LANGUAGE_NAME); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Log.i(TAG, "run: kaishi " + System.currentTimeMillis()); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yinni); Log.i(TAG, "run: bitmap " + System.currentTimeMillis()); TessBaseAPI tessBaseAPI = new TessBaseAPI(); tessBaseAPI.init(DATAPATH, DEFAULT_LANGUAGE); tessBaseAPI.setImage(bitmap); final String text = tessBaseAPI.getUTF8Text(); Log.i(TAG, "run: text " + System.currentTimeMillis() + text); runOnUiThread(new Runnable() { @Override public void run() { tv.setText(text); Log.d("dddd","识别结果"+text); } }); tessBaseAPI.end(); } }).start(); } }); } @Override protected void onResume() { super.onResume(); runOnUiThread(new Runnable() { @Override public void run() { cropView.setImageDrawable(getResources().getDrawable(R.drawable.yinni)); } }); } /** * 将assets中的识别库复制到SD卡中 * @param path 要存放在SD卡中的 完整的文件名。这里是"/storage/emulated/0//tessdata/chi_sim.traineddata" * @param name assets中的文件名 这里是 "chi_sim.traineddata" */ public void copyToSD(String path, String name) { Log.i(TAG, "copyToSD: "+path); Log.i(TAG, "copyToSD: "+name); //如果存在就删掉 File f = new File(path); if (f.exists()){ f.delete(); } if (!f.exists()){ File p = new File(f.getParent()); if (!p.exists()){ p.mkdirs(); } try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } InputStream is=null; OutputStream os=null; try { is = this.getAssets().open(name); File file = new File(path); os = new FileOutputStream(file); byte[] bytes = new byte[2048]; int len = 0; while ((len = is.read(bytes)) != -1) { os.write(bytes, 0, len); } os.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (os != null) os.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onClick(View v) { // switch (v.getId()) { // case R.id.btn: // break; // } } public boolean checkTraineddataExists(){ File file = new File(LANGUAGE_PATH); return file.exists(); } /** * 请求到权限后在这里复制识别库 * @param requestCode * @param permissions * @param grantResults */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { Log.i(TAG, "onRequestPermissionsResult: "+grantResults[0]); switch (requestCode){ case PERMISSION_REQUEST_CODE: if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){ Log.i(TAG, "onRequestPermissionsResult: copy"); copyToSD(LANGUAGE_PATH, DEFAULT_LANGUAGE_NAME); } break; default: break; } } /** * 识别图像 * * @param bitmap */ private void recognition(final Bitmap bitmap) { new Thread(new Runnable() { @Override public void run() { String text=""; if (!checkTraineddataExists()){ text+=LANGUAGE_PATH+"不存在,开始复制\r\n"; Log.i(TAG, "run: "+LANGUAGE_PATH+"不存在,开始复制\r\n"); assets2SD(getApplicationContext(), LANGUAGE_PATH, DEFAULT_LANGUAGE_NAME); } text+=LANGUAGE_PATH+"已经存在,开始识别\r\n"; Log.i(TAG, "run: "+LANGUAGE_PATH+"已经存在,开始识别\r\n"); long startTime = System.currentTimeMillis(); Log.i(TAG, "run: kaishi " + startTime); TessBaseAPI tessBaseAPI = new TessBaseAPI(); tessBaseAPI.init(DATAPATH, DEFAULT_LANGUAGE); tessBaseAPI.setImage(bitmap); text =text+"识别结果:"+ tessBaseAPI.getUTF8Text(); long finishTime = System.currentTimeMillis(); Log.i(TAG, "run: jieshu " + finishTime); Log.i(TAG, "run: text " + text); text = text + "\r\n" + " 耗时" + (finishTime - startTime) + "毫秒"; final String finalText = text; final Bitmap finalBitmap = bitmap; runOnUiThread(new Runnable() { @Override public void run() { tv.setText(finalText); cropView.setImageBitmap(finalBitmap); } }); tessBaseAPI.end(); } }).start(); } }