目录:资产/tessdata [英] Directory: assets/tessdata

查看:70
本文介绍了目录:资产/tessdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从github下载了一个OCR文本识别器.

I've downloaded an OCR text recognizer from github.

我的问题是:我想在不在线的情况下启动我的应用程序,但是每次我在手机上安装apk时,它都会开始下载英语和tesseract OCR引擎. 我找到了一个在线指南,该指南说我必须在资产文件夹中创建一个名为"tessdata"的文件夹,并将eng.traineddata和osd.traineddata放入此文件夹.

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?

推荐答案

首先,在计算机中的项目目录(YourProjectDirectory\app\src\main)中创建assets文件夹,然后在此文件夹中创建另一个tessdata文件夹.将.traineddata文件放在tessdata文件夹中,这些文件将在您的项目开始运行时在您的手机中传输.您可以为您的语言此处下载.traineddata文件.

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.

为了将.traineddata文件传输到手机中,我使用以下代码:

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

此代码检查电话中是否存在目录为/AndroidOCR/tessdata/eng.traineddata的文件,如果不存在,则创建一个目录并将.traineddata文件放在此处. 为此,必须在OnCreate中创建AssetManager,这将允许您访问放置在项目中的计算机中的.traineddata文件.

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.

因此在MainActivity的OnCreate中:

So in your OnCreate in MainActivity:

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

此外,要使您的Android项目将数据以AndroidManifest.xml文件形式写入手机,您需要添加渗透线:

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" />

这是我个人使用的方法,它可以正常工作而不会出现任何错误.如果有,请在google中搜索答案,如果仍然找不到答案,请在评论中发布.

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.

这篇关于目录:资产/tessdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆