文字识别一般都用的tesseract-ocr。
GitHub:https://github.com/tesseract-ocr/tesseract
而Android对应的比较推荐的有个tess-two。
GitHub:https://github.com/rmtheis/tess-two
第一步:下载文件tess-two必须下载 当作module导入,依赖包
从githup下载:https://github.com/rmtheis/tess-two
或者 引入依赖
dependencies{ compile'com.rmtheis:tess-two:6.2.0'}
chi_sim.tessdata 这是中文的 eng是英文的 的
官方下载地址:https://code.google.com/p/tesseract-ocr/,
简单的用法其实就几行代码:
TessBaseAPI tessBaseAPI =newTessBaseAPI();
tessBaseAPI.init(DATAPATH, DEFAULT_LANGUAGE);//参数后面有说明。
tessBaseAPI.setImage(bitmap);
Stringtext = tessBaseAPI.getUTF8Text();
packagecom.example.administrator.ui.fragment2.activity;
importandroid.app.Dialog;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.os.Handler;
*
* $name
*
*@author${LiuTao}
*@date2017/11/17/017
*/
public class TestORCActivity extends BaseActivity {
private static finalStringTAG="TestORCActivity";
@BindView(R.id.orc_clic)
ButtonmOrcClic;
@BindView(R.id.orc_text)
TextViewmOrcText;
@BindView(R.id.orc_img)
ImageViewmOrcImg;
//private TessBaseAPI mTess;
privateStringmResult;
privateHandlermHandler;
private DialogmDialog;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_orc);
ButterKnife.bind(this);
mHandler=newHandler();
mDialog= ProgressDialogUtils.createLoadingDialog(this,"正在初始化需要的资源....");
ThreadPoolProxyFactory.getNormalThreadPoolProxy().submit(init);
//CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
}
@Override
protected voidonDestroy() {
ThreadPoolProxyFactory.getNormalThreadPoolProxy().remove(LoadRunable);
ThreadPoolProxyFactory.getNormalThreadPoolProxy().remove(init);
ProgressDialogUtils.closeDialog(mDialog);
super.onDestroy();
}
privateRunnableinit=newRunnable() {
@Override
public voidrun() {
Log.e(TAG,"init:"+ Thread.currentThread().getName());
//mTess = new TessBaseAPI();这里拷贝到手机/android/tesseract/tessdata/目录
//存放tessdata的文件路径 就是chi_sim.traineddata文件的位置chi_sim.traineddata
Stringdatapath= Environment.getExternalStorageDirectory() +"/tesseract/";
//选择语言chi_sim简体中文eng英文
Stringlanguage="chi_sim";
Filedir=newFile(datapath+"tessdata/");
if(!dir.exists()) {
dir.mkdirs();
}
// boolean result = mTess.init(datapath, language);
// /* if (result) {
// ProgressDialogUtils.closeDialog(mDialog);
// }*/
}
};
@OnClick(R.id.orc_clic)
public voidonViewClicked() {
Bitmapbitmap= BitmapFactory.decodeResource(TestORCActivity.this.getResources(),R.drawable.testimg);
//将图片设置到mTess进行识别
// mTess.setImage(bitmap);
ThreadPoolProxyFactory.getNormalThreadPoolProxy().submit(LoadRunable);
Log.e(TAG,"::::::=>"+ Thread.currentThread().getName());
mOrcImg.setImageBitmap(bitmap);
}
privateRunnableLoadRunable=newRunnable() {
@Override
public voidrun() {
Log.e(TAG,Thread.currentThread().getName());
//获取识别的文字(这里会等一段时间,这里的代码是在主线程的,建议将这部分代码放到子线程)
// mResult = mTess.getUTF8Text();
/* runOnUiThread(new Runnable() {
@Override
public void run() {
mOrcText.setText("识别结果为:" + mResult);
}
});*/
}
};
}
相关连接:http://blog.csdn.net/jia20003/article/details/53257199