2

Anybody knows how can I handle exceptions coming from native method calls?

I'm running the Barcode Reader example from Google Vision API, it works very well reading some 2d - pdf417 codes, but in some cases it crashes with a native exception attempting to use NewStringUTF like this:

    art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0x90
    art/runtime/java_vm_ext.cc:410]     in call to NewStringUTF
    art/runtime/java_vm_ext.cc:410]     from com.google.android.gms.vision.barcode.internal.NativeBarcode[] com.google.android.gms.vision.barcode.internal.NativeBarcodeDetector.recognizeNative(int, int, byte[], com.google.android.gms.vision.barcode.internal.NativeOptions)
    art/runtime/java_vm_ext.cc:410] "Thread-4533" prio=5 tid=19 Runnable
    art/runtime/java_vm_ext.cc:410]   | group="main" sCount=0 dsCount=0 obj=0x12c8e0a0 self=0x7f849f4200
    art/runtime/java_vm_ext.cc:410]   | sysTid=9051 nice=0 cgrp=default sched=0/0 handle=0x7f7b7bf440
    art/runtime/java_vm_ext.cc:410]   | state=R schedstat=( 1663092757 5060164 94 ) utm=165 stm=1 core=2 HZ=100
    art/runtime/java_vm_ext.cc:410]   | stack=0x7f7b6bd000-0x7f7b6bf000 stackSize=1037KB
    art/runtime/java_vm_ext.cc:410]   | held mutexes= "mutator lock"(shared held)
    art/runtime/java_vm_ext.cc:410]   native: #00 pc 00000000004897a8  /system/lib64/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiP12BacktraceMapPKcPNS_9ArtMethodEPv+200)
    art/runtime/java_vm_ext.cc:410]   native: #01 pc 0000000000458644  /system/lib64/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEP12BacktraceMap+224)
    art/runtime/java_vm_ext.cc:410]   native: #02 pc 000000000030c9e4  /system/lib64/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1004)
    art/runtime/java_vm_ext.cc:410]   native: #03 pc 000000000030d29c  /system/lib64/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+116)
    art/runtime/java_vm_ext.cc:410]   native: #04 pc 0000000000141f9c  /system/lib64/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+144)
    art/runtime/java_vm_ext.cc:410]   native: #05 pc 000000000014a1d0  /system/lib64/libart.so (_ZN3art11ScopedCheck5CheckERNS_18ScopedObjectAccessEbPKcPNS_12JniValueTypeE.constprop.116+11084)
    art/runtime/java_vm_ext.cc:410]   native: #06 pc 0000000000153418  /system/lib64/libart.so (_ZN3art8CheckJNI12NewStringUTFEP7_JNIEnvPKc+468)
    art/runtime/java_vm_ext.cc:410]   native: #07 pc 0000000000005774  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (_ZN9barhopper9JniObject14SetStringFieldEPKcRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE+168)
    art/runtime/java_vm_ext.cc:410]   native: #08 pc 00000000000036f8  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (???)
    art/runtime/java_vm_ext.cc:410]   native: #09 pc 0000000000004c8c  /data/data/com.google.android.gms/files/com.google.android.gms.vision/barcode/libs/arm64-v8a/libbarhopper.so (Java_com_google_android_gms_vision_barcode_internal_NativeBarcodeDetector_recognizeNative+84)

I would like to handle the crash in order to catch it and give some feedback to the user.

2 Answers2

1

Unfortunately there is no way to handle exceptions from not your own native code.

I had the similar issue with google-vision. And I reported the issue to google vision github https://github.com/googlesamples/android-vision

Sadly but this library has some bugs. In my case it was the bug in the barcode text parser. It will be cool if you report your problem too.

It may be interesting that there is a way to catch exception from your own native code: Catching exceptions thrown from native code running on Android

Community
  • 1
  • 1
Ivan Shafran
  • 591
  • 2
  • 17
  • 1
    Thanks @Ivan I've reported the [issue](https://github.com/googlesamples/android-vision/issues/221) but it seems that google engineers are not very attentive on that repo, I think we'll be using the ZXing library but it is very slow for reading 2d codes, do you have any quick project showing real time 2d code detection with ABBYY for Android? – David Coronel May 03 '17 at 17:15
  • [You could look here](https://www.abbyy.com/mobile-ocr/features/) There is barcode reading. But no realtime. If it works fast you can write you own real time by using it. Sample is available from "Get trial". – Ivan Shafran May 03 '17 at 21:04
  • In order to convert frames from camera to acceptable format fast you can use https://developer.android.com/reference/android/renderscript/ScriptIntrinsic.html – Ivan Shafran May 03 '17 at 21:11
  • Thank you very much @Ivan! We'll take a look on it – David Coronel May 04 '17 at 03:18
0

There is an issue with Google's vision library which causes it to crash when scanning certain QR Codes. (See https://github.com/googlesamples/android-vision/issues/136). If you only need to scan normal barcodes, you can do something like this to avoid the vision library detecting QR Codes at all:

    val barcodeFormatsToScan = Barcode.CODE_128 or Barcode.CODE_39 or Barcode.CODE_93 or Barcode.CODABAR or Barcode.DATA_MATRIX or Barcode.EAN_13 or
            Barcode.EAN_8 or Barcode.ITF or Barcode.UPC_A or Barcode.UPC_E or Barcode.PDF417 or Barcode.AZTEC
    val barcodeDetector = BarcodeDetector.Builder(context).setBarcodeFormats(barcodeFormatsToScan).build()
    val barcodeFactory = BarcodeTrackerFactory(fragment_barcode_scanner_graphic_overlay as GraphicOverlay<BarcodeGraphic>, true, callback)
    barcodeDetector.setProcessor(MultiProcessor.Builder<Barcode>(barcodeFactory).build())
Anthony.
  • 643
  • 7
  • 14