Tesseract:找不到数据文件错误

时间:2015-12-01 07:32:44

标签: java android tesseract

我正在开发一款使用Tesseract的Android应用程序。以下教程后我收到此错误:

Caused by: java.lang.IllegalArgumentException: Data file not found at /storage/emulated/0/Define/tessdata/eng.traineddata
  at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:353)
  at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:318)
  at com.example.mississauga.define.MainActivity.onActivityResult(MainActivity.java:130)

这是我在MainActivity中的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    textcaptured = (TextView)findViewById(R.id.textView);

    String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };

    for (String path : paths) {
        File dir = new File(path);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                return;
            } else {
                Log.v(TAG, "Created directory " + path + " on sdcard");
            }
        }

    }

    // lang.traineddata file with the app (in assets folder)
    // You can get them at:
    // http://code.google.com/p/tesseract-ocr/downloads/list
    // This area needs work and optimization
    if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
        try {

            AssetManager assetManager = getAssets();
            InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
            //GZIPInputStream gin = new GZIPInputStream(in);
            OutputStream out = new FileOutputStream(DATA_PATH
                    + "tessdata/" + lang + ".traineddata");

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            //while ((lenf = gin.read(buff)) > 0) {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            //gin.close();
            out.close();

            Log.v(TAG, "Copied " + lang + " traineddata");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
        }
    }


}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode==Start_Camera && resultCode== RESULT_OK){
        //Toast.makeText(this,"Picture Taken",  Toast.LENGTH_SHORT).show();
        Bundle extras = data.getExtras();
        Bitmap photoBitmap = (Bitmap) extras.get("data");
        TessBaseAPI baseApi = new TessBaseAPI();

        baseApi.setDebug(true);
        baseApi.init(DATA_PATH, "eng");
        baseApi.setImage(photoBitmap);
        String recognizedText = baseApi.getUTF8Text(); // Log or otherwise display this string...
        baseApi.end();
        textcaptured.setText(recognizedText);

    }
}

这就是我设置directory的方法,请忽略右侧的代码。

1 个答案:

答案 0 :(得分:0)

确保文件AndroidManifest.xml中存在权限

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