问题
So I am trying to figure out how to use TessBase, and I get an error at baseApi.init(dataPath, "eng")
. The error I get is : directory must contain tessdata
. I can't figure out how to get the directory that contains tessdata. This is an image of the directory that contains eng.traineddata. This is my code:
Bundle extras = data.getExtras();
Bitmap photoBitmap = (Bitmap) extras.get("data");
TessBaseAPI baseApi = new TessBaseAPI();
//textcaptured.setText(DATA_PATH.toString());/*
String dataPath = Environment.getExternalStorageDirectory().toString() + "/Android/data/" + getApplicationContext().getPackageName() + "/";
textcaptured.setText(dataPath);
File tessdata = new File(dataPath);
if (!tessdata.exists() || !tessdata.isDirectory()) {
throw new IllegalArgumentException("Data path must contain subfolder tessdata!");
}
baseApi.init(dataPath, "eng");
baseApi.setImage(photoBitmap);
String recognizedText = baseApi.getUTF8Text(); // Log or otherwise display this string...
baseApi.end();
textcaptured.setText(recognizedText);
回答1:
The easiest way that I recommend is to make a folder in your sdcard by yourself and put a directory with subdirectory tessdata with eng.tessdata in it the structure shown here:
+SdCardOfPhone
--+YourAppName
----+tessdata
------eng.tessdata
now you can point to that directory by
String datapath = Environment.getExternalStorageDirectory() + "/YourAppName/";
TessBaseAPI tessBaseAPI = new TessBaseAPI();
tessBaseAPI.init(datapath, "eng");
you also have to have the following in your AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For testing purposes I don't recommend bundling your eng.tessdata inside the project it increases the build time and the size of your .apk file(>17mb).
The process of bundling eng.tessdata inside the project is quite involved and involves making a new directory by yourself and copying the bundled data to that directory in runtime.
来源:https://stackoverflow.com/questions/33927400/how-to-get-the-directory-that-needs-to-be-used-in-tessbase-initdirectory-en