我正在开发一款需要OCR的
Android应用.我决定使用
Tesseract作为API,但我一直在收到此错误:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
>我已将文件“eng.traineddata”复制到该位置.
>我使用的是Android Studio 2.1.2(SDK 23)
>使用API 22 Android Lollipop 5.1.1在设备上进行测试(阅读Marshmallow上的权限问题)
这是我正在使用的代码:
- public void reads(View view) {
- TextView textView = (TextView) findViewById(R.id.textView);
- int rotation = 0;
- try {
- ExifInterface exifInterface = new ExifInterface(mCurrentPhotoPath);
- int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
- switch (orientation){
- case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break;
- case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break;
- case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break;
- }
- } catch(Exception e) {
- }
- int w = imageBitmap.getWidth();
- int h = imageBitmap.getHeight();
- if (rotation != 0) {
- Matrix matrix = new Matrix();
- matrix.preRotate(rotation);
- imageBitmap = Bitmap.createBitmap(imageBitmap,w,h,matrix,false);
- } else {
- imageBitmap = Bitmap.createBitmap(imageBitmap,h);
- }
- imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888,true);
- TessBaseAPI ReadIt = new TessBaseAPI();
- ReadIt.init("/storage/emulated/0/","eng");
- ReadIt.setImage(imageBitmap);
- String Text = ReadIt.getUTF8Text();
- if (Text!=null) textView.setText(Text);
- }
我在build.gradle依赖项中使用了这一行:
compile ‘com.rmtheis:tess-two:6.0.2’
另外,我已经通过在特定的目录下载直接复制了名为tessdata的文件夹中的“eng.traineddata”.
解决方法
你在用
tess-two吗?在你的代码中:
- TessBaseAPI ReadIt = new TessBaseAPI();
- ReadIt.init("/storage/emulated/0/","eng");
“/ storage / emulated / 0 /”路径应该指向您的数据文件.您必须有一个子目录
名为“tessdata”.看到
https://github.com/rmtheis/tess-two/blob/d7a45fd2e08b7ec315cd1e29d1a7e0c72fb24a66/tess-two/src/com/googlecode/tesseract/android/TessBaseAPI.java#L176