Mirco
Mirco

Reputation: 75

Directory: assets/tessdata

I've downloaded an OCR text recognizer from github.

My problem is: I want to launch my app without being online, but everytime I install the apk on my phone, it starts downloading the english language and the tesseract OCR engine. I've found an online guide which says I have to create a folder in the assets folder called "tessdata" and put the eng.traineddata and the osd.traineddata in this folder.

I've tried but the download process still starts when I install the app for the first time. What can I do to make this app completely offline?

Upvotes: 2

Views: 4253

Answers (1)

Dainius Šaltenis
Dainius Šaltenis

Reputation: 1734

First, in your project directory in computer (YourProjectDirectory\app\src\main) create assets folder, int this folder create another tessdata folder. In tessdata folder put your .traineddata files, these will be transferred in your phone when your project starts running. You can download .traineddata files for your language HERE.

For transferring .traineddata files into phone I use this code:

public class TessOCR {
public static final String PACKAGE_NAME = "com.example.dainius.ocr";
public static final String DATA_PATH = Environment
        .getExternalStorageDirectory().toString() + "/AndroidOCR/";
public static final String lang = "eng";

private static final String TAG = "TESSERACT";
private AssetManager assetManager;

private TessBaseAPI mTess;

public TessOCR(AssetManager assetManager) {

    Log.i(TAG, DATA_PATH);

    this.assetManager = assetManager;

    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");
            }
        }
    }

    if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
        try {
            InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
            OutputStream out = new FileOutputStream(new File(DATA_PATH + "tessdata/", lang + ".traineddata"));

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();

            Log.v(TAG, "Copied " + lang + " traineddata");
        } catch (IOException e) {
            Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
        }
    }

    mTess = new TessBaseAPI();
    mTess.setDebug(true);
    mTess.init(DATA_PATH, lang);

}


public String getResults(Bitmap bitmap)
{
    mTess.setImage(bitmap);
    String result = mTess.getUTF8Text();
    return result;
}

public void onDestroy() {
    if (mTess != null)
        mTess.end();
}
}

This code checks whether in your phone exists file with directory /AndroidOCR/tessdata/eng.traineddata and if not, creates one and puts .traineddata file here. For this to occur, in your OnCreate you will have to create AssetManager, which will let you to access that .traineddata file you placed in your computer in your project.

So in your OnCreate in MainActivity:

AssetManager assetManager = getAssets();
TessOCR tess = new TessOCR(assetManager);

Also, to allow your Android project write data into your phone in AndroidManifest.xml file you need to add permision line:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This is the method I use personally and it works without any errors. If you have any, search in google for answers and if you still can't find an answer, post in comments.

Upvotes: 5

Related Questions