Search code examples
androidandroid-ndktesseracttess-two

Android Tesseract App crashes on OCR Function


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)

Solution

  • 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.