Android Tesseract App crashes on OCR Function

2020-07-27 07:26发布

问题:

I am trying to implement Tesseract into my android project but am getting a crash when trying to complete the OCR.

Here is how I'm setting up Tesseract:

 TessBaseAPI baseApi = new TessBaseAPI();
        baseApi.setDebug(true);
        baseApi.init(imagePath, "eng");
        baseApi.setImage(bitmap);
        String recognizedText = baseApi.getUTF8Text();
        baseApi.end();

This is how I'm setting up the image information to pass into the TesseractAPI:

        destination = new File(Environment.getExternalStorageDirectory(), name + ".png");

        imagePath = destination.getAbsolutePath();

        String name =   dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");

Here is the Logcat:

10734-10734/www.rshdev.com.ocr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: www.rshdev.com.ocr, PID: 10734
    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/www.rshdev.com.ocr-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libpngt.so"
            at java.lang.Runtime.loadLibrary(Runtime.java:366)
            at java.lang.System.loadLibrary(System.java:988)
            at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:43)
            at www.rshdev.com.ocr.MainActivity.ocr(MainActivity.java:140)
            at www.rshdev.com.ocr.MainActivity.onActivityResult(MainActivity.java:86)
            at android.app.Activity.dispatchActivityResult(Activity.java:6192)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
            at android.app.ActivityThread.access$1300(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

回答1:

Your tesseract does not crash in the OCR function, it crashes trying to load a library:

java.lang.UnsatisfiedLinkError: ... couldn't find "libpngt.so"
...
at com.googlecode.tesseract.android.TessBaseAPI.<clinit>(TessBaseAPI.java:43)

But line 43 in the source that I have reads:

    System.loadLibrary("tess");

therefore it tries to load libtess.so but reports a failure on libpngt.so.

Either:

1) your source code of TessBaseAPI.java is different, it contains System.loadLibrary("pngt"); and that library is missing. Make sure the .apk contains it. Eclipse used to have a misfeature: if your code depends on a library, you configure that dependency for compilation in one place and for delivery in another place. And IIRC .so dependency was specified in a 3rd place.

2) libtess.so is compiled with dynamic linking (try to use static linking then)

3) you are trying to run it in the emulator (try on a real device then).

This is all what can be said from the information you provided.



回答2:

More info about solution:

I faced with this issue when I moved from Windows to Linux. My Linux had no NDK installed.

I've downloaded it from official source.

Instructions for installing under Linux:

Go to the directory where you downloaded it. Execute the package. For example:

ndk$ chmod a+x android-ndk-r10c-darwin-x86_64.bin
ndk$ ./android-ndk-r10c-darwin-x86_64.bin

You also need to rebuild tess-two under Linux. Follow the instruction in oficial source.



回答3:

Make sure that you are using the same gradle version on your project and in the tess lib project. Example:

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}

The value on classpath must be equals on both projects.



回答4:

  1. do not update any gradle aur sdk update... i repeat ..do ignore all the update if asking and it will work smoothly. Just import the project of priyankverma######## from github and IGNORE all the updates ... i am sure you wont get this libpngt.so error.
    1. Download or clone the project
    2. import in android studio
    3. let the gradle and sdk versions as it is and
    4. IGNORE all the UPDATES even it is saying "Strongly Recommended.
    5. simply run the project. :)