我正在開發一個使用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,請忽略右邊的代碼。