7

我正在开发一个需要 OCR 的 Android 应用程序。我决定使用Tesseract作为 API,但我不断收到此错误:

E/Tesseract(native): 无法使用 language=eng 初始化 Tesseract API!

  1. 我已经将文件复制"eng.traineddata"到该位置。
  2. 我正在使用 Android Studio 2.1.2 (SDK 23)
  3. 使用 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的文件夹中的“。

4

7 回答 7

3

你在使用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

阅读更多内容: 无法使用 language=eng 初始化 Tesseract API!

于 2016-07-10T15:53:37.937 回答
3

在Activity中释放manifest的权限:

在清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在 onCreate 中:

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);
        }
    }
于 2017-10-02T17:04:59.043 回答
3

Tesseract-two 没有使用最新版本的 OCR 引擎,它使用的是 3.05,所以我们被迫使用来自here的数据。新数据似乎使用了不同的模型,神经网络。4.0 之前的先前模型的工作方式不同。

我曾尝试使用此处此处的数据。这些数据集仅与 tesseract 的最新版本 4.0 ( source ) 兼容,因此如果您使用的是旧版本的 tesseract,它将无法工作。

于 2018-05-03T15:19:42.437 回答
2

如果您不使用 Marshmallow 并且仍然有问题,请尝试清理并重建项目。

于 2017-09-04T14:37:29.643 回答
1

我遇到了同样的问题,问题是 Marshmallow 特别需要一种新的方式让您的应用程序获得对存储的读/写权限。这篇博文解决了我的问题。

在我的主要活动中,我有以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ...
    getStorageAccessPermissions(); // Request storage read/write permissions from the user
}

@TargetApi(23)
private void getStorageAccessPermissions() {
    int hasWriteStoragePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (hasWriteStoragePermission != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_PERMISSIONS);
    }
}

其中 REQUEST_CODE_WRITE_EXTERNAL_PERMISSIONS 是全局声明的整数常量。

在我扩展TessBaseAPI的类中,我添加了以下内容,仅用于记录目的,以确保我实际上可以访问存储。

/* Checks if external storage is available to at least write to and returns the path name */
private static String isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    String retval = "External storage is not writable";
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        retval = Environment.getExternalStorageDirectory().toString();
    }
    return retval;
}

/* Checks if external storage is available to at least read from and returns the path name */
private static String isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    String retval = "External storage is not readable";
    if (Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        retval = Environment.getExternalStorageDirectory().toString();
    }
    return retval;
}
于 2016-07-24T02:54:03.307 回答
0
  • 使用从外部存储(不是资产)到tessdata目录的绝对路径
    ,例如,如果您的模型正在 /storage/emulated/0/Android/data/com.xxx.yyy/files/tessmodels/tessdata/ 使用此路径 /storage/emulated/0/Android/data/com.xxx.yyy/files/tessmodels/
  • 确保您具有写入/读取外部存储权限
  • 使用这个模型,经过测试tess-two:9.0.0。我从tess-two 示例应用程序中得到它
于 2018-05-15T11:18:17.747 回答
0

较新版本的 tess-two 检查以确保可以在设备上找到训练数据文件。如果未找到这些训练数据文件,将显示比您看到的错误消息更丰富的消息。

因此,当您在较新版本的 tess-2 上看到此错误消息时,这意味着在预期位置找到了训练数据文件,但它们是错误的版本,或者无法读取。检查以确保您使用的是正确版本的训练数据文件。

于 2018-05-15T14:13:25.430 回答