Search code examples
androidocrtesseracttess-two

Get path of data directory(android)


I am using tesseract ocr in my app. In order to use tesseract i need to use several language files that are located at a directory called - 'tessdata'.

This is my method code:

    public String detectText(Bitmap bitmap) {
    TessBaseAPI tessBaseAPI = new TessBaseAPI();
    String DATA_PATH = Environment.getRootDirectory().getPath() + "/tessdata/";

    tessBaseAPI.setDebug(true);
    tessBaseAPI.init(DATA_PATH, "eng"); //Init the Tess with the trained data file, with english language

    tessBaseAPI.setImage(bitmap);

    String text = tessBaseAPI.getUTF8Text();

    tessBaseAPI.end();

    return text;
}

I've used many variations of:

String DATA_PATH = Environment.getRootDirectory().getPath() + "/tessdata/";

and every time the app fails with "path not found" exception. I need a good way to put this directory in the android phone and get the path of it regardless of which phone it is. Right now the 'tessdata' directory can be found at the app root directory.

How can i do that?


Solution

  • Don't include "/tessdata/" in your DATA_PATH variable--just leave that part off, but be sure that subfolder exists within the directory specified by DATA_PATH.