我正在开发一个需要OCR的Android应用。我决定使用Tesseract作为API,但是一直出现以下错误信息:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
以下是我的解决方案:
1. 已经将文件 "eng.traineddata" 复制到指定位置。 2. 使用 Android Studio 2.1.2 (SDK 23)。 3. 在运行Android Lollipop 5.1.1 的API 22设备上进行测试(了解Marshmallow权限问题)。
以下是我使用的代码:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
以下是我的解决方案:
1. 已经将文件 "eng.traineddata" 复制到指定位置。 2. 使用 Android Studio 2.1.2 (SDK 23)。 3. 在运行Android Lollipop 5.1.1 的API 22设备上进行测试(了解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依赖中使用了这行代码:
compile 'com.rmtheis:tess-two:6.0.2'
此外,我通过下载到特定的目录中,将"eng.traineddata
"拷贝到名为tessdata的文件夹中。