Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tesseract datapath does not exist

I am trying to extract text from any captured image in Android. So I have created an Intent to access the camera and started it with startActivityForResult.

This is my onActivityResult code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            Bundle extras = data.getExtras();
            Bitmap bitmap = (Bitmap) extras.get("data");
            TextView res = (TextView) findViewById(R.id.hello);
            //imageView.setImageBitmap(imageBitmap);
            //Image image = ImageIO.read(imageFile);
            //BufferedImage buffimg = (BufferedImage) image;
            //BufferedImage img = ImageHelper.convertImageToGrayscale(buffimg);
            //ITesseract instance = new Tesseract();  // JNA Interface Mapping
            //ITesseract instance = new Tesseract1(); // JNA Direct Mapping

            //TessDataManager.initTessTrainedData(context);
   if(isStoragePermissionGranted() == true) {
TessBaseAPI tessBaseAPI = new TessBaseAPI();

String path = Environment.getExternalStorageDirectory() + "/";
//String path = "/mnt/sdcard/";

tessBaseAPI.setDebug(true);
tessBaseAPI.init(path, "eng");


tessBaseAPI.setImage(bitmap);

String text = tessBaseAPI.getUTF8Text();
tessBaseAPI.end();
res.setText(text);
}
        }
        else
        {
            TextView res = (TextView) findViewById(R.id.hello);
            res.setText("Well damn");
        }


    }


}
public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG, "Permission is granted");
        return true;
    }


}

The error that I receive is:

java.lang.RuntimeException: Unable to resume activity {com.example.abc.snaptravel/com.example.abc.snaptravel.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=content: (has extras) }} to activity {com.example.abc.snaptravel/com.example.abc.snaptravel.MainActivity}: java.lang.IllegalArgumentException: Data path does not exist!

Specifically:

Data path does not exist

I have a read a lot about this problem and tried many different solutions. I have tried with and without "/tessdata/" in the path name too.

If you have any suggestions, I will really appreciate them. Thank You!

like image 325
Akshansh Jain Avatar asked Aug 13 '16 15:08

Akshansh Jain


1 Answers

Well, I thought Environment.getExternalStorageDirectory() points to my SD card but it doesn't. It points to my internal storage. That's where I was wrong. I copied the tessdata folder in my internal storage and now it works perfectly.

like image 97
Akshansh Jain Avatar answered Nov 03 '22 21:11

Akshansh Jain