Android Tesseract 错误。找不到数据文件

Android Tesseract Error. Data file not found at

我正在学习 android 将 NDK 与 opencv 结合使用。

我使用 ndk 成功了。所以我得到了可用的数据(我的意思是数据是由 canny 完成的。)

当我使用 Tesseract 时,

Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata

发生此错误。

我已经检查过 adroid/app/src/main/assets/tessdata/eng.traineddata 有训练有素的数据.... 我不知道为什么会出现错误。

请...请帮助我...

public class ocrActivity extends AppCompatActivity {

private static final String TAG = "opencv";
private Mat matInput=new Mat();
private Mat matResult=new Mat();
private Mat matFinal=new Mat();
public Uri luck;
public Bitmap testbm;
public Bitmap resultbm;

public native void ConvertRGBtoGray(long matAddrInput, long matAddrResult);

static {
    System.loadLibrary("opencv_java3");
    System.loadLibrary("native-lib");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_ocr);
    ImageView iv1 = findViewById(R.id.imageview01);

    Uri data = null;

    Intent takePhoto = getIntent();
    String flag = takePhoto.getStringExtra("flag");


    if (flag.equals("imageUri")) {
        data = takePhoto.getParcelableExtra("imageUri");
    } else if (flag.equals("albumURI")) {
        data = takePhoto.getParcelableExtra("albumURI");
    }


    luck = data;
    testbm=test123();
    //

    Utils.bitmapToMat(testbm,matInput);

    if ( matResult == null )
        matResult = new Mat(matInput.rows(), matInput.cols(), matInput.type());

    ConvertRGBtoGray(matInput.getNativeObjAddr(), matResult.getNativeObjAddr());

    Utils.matToBitmap(matResult,testbm);
    iv1.setImageBitmap(testbm);

----------------Doing very Well--------------------------
    TessOCR mTessOCR = new TessOCR();
    String result = mTessOCR.getOCRResult(testbm);

    System.out.println(result);
}

public class TessOCR {
    private TessBaseAPI mTess;

    public TessOCR() {
        // TODO Auto-generated constructor stub
        mTess = new TessBaseAPI();
        String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
        String language = "eng";
        File dir = new File(datapath + "tessdata/");
        if (!dir.exists())
            dir.mkdirs();
        mTess.init(datapath, language);
    }

    public String getOCRResult(Bitmap bitmap) {

        mTess.setImage(bitmap);
        String result = mTess.getUTF8Text();

        return result;
    }

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

}


/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
public native String stringFromJNI();

public Bitmap test123() {
    Uri photo = fuck;
    Bitmap bm=null;
    try {
        bm = MediaStore.Images.Media.getBitmap(getContentResolver(), photo);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bm;
}
}

错误代码!

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.z2_sooodt.ocr/com.example.z2_sooodt.ocr.ocrActivity}: java.lang.IllegalArgumentException: Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.IllegalArgumentException: Data file not found at /storage/emulated/0/tesseract/tessdata/eng.traineddata
                  at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:339)
                  at com.googlecode.tesseract.android.TessBaseAPI.init(TessBaseAPI.java:303)
                  at com.example.z2_sooodt.ocr.ocrActivity$TessOCR.<init>(ocrActivity.java:110)
                  at com.example.z2_sooodt.ocr.ocrActivity.onCreate(ocrActivity.java:88)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

您收到一条错误消息,指出无法在

中找到经过训练的数据文件

/storage/emulated/0/tesseract/tessdata/eng.traineddata

这是您设备上 tessdata 文件夹的路径,而

adroid/app/src/main/assets/tessdata/eng.traineddata

是您 PC 上训练数据文件的路径(它随您的 apk 一起提供,但除非您这样做,否则不会被提取)。

您需要将文件从资产中提取到设备的存储空间中。参见 How to copy files from 'assets' folder to sdcard?