2015-12-01 70 views
3

我正在開發一個使用Tesseract的android應用程序。下面的教程後,我收到此錯誤:Tesseract:找不到數據文件錯誤

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,請忽略右邊的代碼。

回答

0

Tesseract OCR庫要求您提供一個文件,其中包含特定語言的光學字符識別數據,以便以特定語言檢測並形成儘可能有效的單詞。

這個例外是告訴你.traineddata無法找到,事實上,根據你的目錄,它在那裏。 找不到文件的原因是因爲您的數據路徑中有/ tessdata /。

您應該不是 have/tessdata /在您的文件路徑中。 Tesseract OCR庫本身將確保搜索/ tessdata /文件夾中的.traineddata文件。

所以,你的路徑應該是這樣的:

private static final String DATA_PATH = "/storage/0/storage/emulated/0/Define/"; 
private static final String LANG = "eng"; 

我會建議測試與硬編碼路徑第一。您可以隨時嘗試,比較和使用相對路徑。

最後,你有你的課程路徑初始化的Tesseract OCR庫:

baseApi.init(DATA_PATH, LANG); 

現在你應該是好去。

0

確保在AndroidManifest.xml中的文件存在的權限

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