我正在开发一个需要 OCR 的 Android 应用程序。我决定使用Tesseract作为 API,但我不断收到此错误:
E/Tesseract(native): 无法使用 language=eng 初始化 Tesseract API!
- 我已经将文件复制
"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,0,0,w,h,matrix,false);
} else {
imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,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 依赖项中使用了这一行:
编译'com.rmtheis:tess-two:6.0.2'
另外,我通过在特定的指定目录中下载直接复制了eng.traineddata
名为tessdata的文件夹中的“。