问题
I'm studying android using NDK with opencv.
I success using ndk. So I get usable data ( I mean the data was done by canny.)
When I use Tesseract,
Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata
This Error is occured.
I already checked adroid/app/src/main/assets/tessdata/eng.traineddata there are the traineddata.... I don't know why I get an error there.
plz... help me please...
public class ocrActivity extends AppCompatActivity {
private static final String TAG = "opencv";
private Mat matInput=new Mat();
private Mat matResult=new Mat();
private Mat matFinal=new Mat();
public Uri luck;
public Bitmap testbm;
public Bitmap resultbm;
public native void ConvertRGBtoGray(long matAddrInput, long matAddrResult);
static {
System.loadLibrary("opencv_java3");
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_ocr);
ImageView iv1 = findViewById(R.id.imageview01);
Uri data = null;
Intent takePhoto = getIntent();
String flag = takePhoto.getStringExtra("flag");
if (flag.equals("imageUri")) {
data = takePhoto.getParcelableExtra("imageUri");
} else if (flag.equals("albumURI")) {
data = takePhoto.getParcelableExtra("albumURI");
}
luck = data;
testbm=test123();
//
Utils.bitmapToMat(testbm,matInput);
if ( matResult == null )
matResult = new Mat(matInput.rows(), matInput.cols(), matInput.type());
ConvertRGBtoGray(matInput.getNativeObjAddr(), matResult.getNativeObjAddr());
Utils.matToBitmap(matResult,testbm);
iv1.setImageBitmap(testbm);
----------------Doing very Well--------------------------
TessOCR mTessOCR = new TessOCR();
String result = mTessOCR.getOCRResult(testbm);
System.out.println(result);
}
public class TessOCR {
private TessBaseAPI mTess;
public TessOCR() {
// TODO Auto-generated constructor stub
mTess = new TessBaseAPI();
String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
String language = "eng";
File dir = new File(datapath + "tessdata/");
if (!dir.exists())
dir.mkdirs();
mTess.init(datapath, language);
}
public String getOCRResult(Bitmap bitmap) {
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public Bitmap test123() {
Uri photo = fuck;
Bitmap bm=null;
try {
bm = MediaStore.Images.Media.getBitmap(getContentResolver(), photo);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}
}
Error Code!
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.z2_sooodt.ocr/com.example.z2_sooodt.ocr.ocrActivity}: java.lang.IllegalArgumentException: Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata
at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:339)
at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:303)
at com.example.z2_sooodt.ocr.ocrActivity$TessOCR.<init>(ocrActivity.java:110)
at com.example.z2_sooodt.ocr.ocrActivity.onCreate(ocrActivity.java:88)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
回答1:
You are getting an error which states that traineddata file cannot be found in
/storage/emulated/0/tesseract/tessdata/eng.traineddata
which is a path to tessdata folder on your device, while
adroid/app/src/main/assets/tessdata/eng.traineddata
is a path to traineddata file on your PC (which is shipped with your apk but isn't extracted unless you do it).
You need to extract file from assets to the device's storage. See How to copy files from 'assets' folder to sdcard?
来源:https://stackoverflow.com/questions/52139751/android-tesseract-error-data-file-not-found-at