Directory: assets/tessdata

↘锁芯ラ 提交于 2019-12-06 13:16:24

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!