意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Android Studio 教你3步会用tesseract

来源:恒创科技 编辑:恒创科技编辑部
2024-02-02 22:23:59


Android Studio 教你3步会用tesseract_初始化

资源链接:

Tesseract 两个重要的github连接:
​​​https://github.com/rmtheis/tess-two​​​
​​​https://github.com/tesseract-ocr/tessdata​​


Android Studio 教你3步会用tesseract

tesseract具体使用:

1、添加依赖 ​​compile 'com.rmtheis:tess-two:8.0.0'​​(这应该再熟悉不过了)

2、从上面的第二个tessdata的链接下载对应的字库,创建assets目录,添加字库,如下图

Android Studio 教你3步会用tesseract_创建目录_02


3、编码

(1)初始化TessBaseAPI

路径

private  String mDataPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/tessdata/";

下面创建目录跟复制字库我都放在了onCreate方法中,但是这样的话activity会出现空白一小会儿。

//创建父目录
File parentfile=new File(mDataPath);
if (!parentfile.exists()){
parentfile.mkdir();
}
copyFiles();//复制字库,具体代码见下文
String lang = "chi_sim+eng";//中文简体+英文
mTess = new TessBaseAPI();
mTess.init(mFilePath, lang);//mFilePath不知道?

(2)复制字库

private void copyFiles() {
//循环复制2中字库
String[] datafilepaths = new String[]{mDataPath + "/chi_sim.traineddata",mDataPath+"/eng.traineddata"};
for (String datafilepath : datafilepaths) {
copyFile(datafilepath);
}
}

private void copyFile(String datafilepath) {
try {
String filepath = datafilepath;
String[] filesegment = filepath.split(File.separator);
String filename = filesegment[(filesegment.length - 1)];//获取chi_sim.traineddata和eng.traineddata文件名
AssetManager assetManager = getAssets();
InputStream instream = assetManager.open(filename);//打开chi_sim.traineddata和eng.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();
File file = new File(filepath);
if (!file.exists()) {
throw new FileNotFoundException();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch

(3)获得结果
这一步属于是耗时操作,建议异步进行AsyncTask或者Rxjava

Long starttime=System.currentTimeMillis();
String OCRresult = null;
mTess.setImage(bitmap);
OCRresult = mTess.getUTF8Text();
Long endtime=System.currentTimeMillis();
Log.e("耗时时间",(endtime-starttime)+"");

总结:
1、两种语言进行的识别时间会比单纯一种语言的识别慢很多倍,如果是识别少量文字,速度还可以,但是如果需要识别大量文字,几乎是要崩溃……这个有待改进
2、上图我还没有加osd.traineddata,试了下旋转过的图片效果不好
3、识别效果的好坏还跟你处理的图片的好坏有关系,多方面因素吧。


上一篇: AndroidWebView下载apk 下一篇: 手机怎么远程登录云服务器?