mintware-de / flutter_barcode_reader

A flutter plugin for reading 2D barcodes and QR codes.
MIT License
628 stars 463 forks source link

[Bug] Barcode Crash on scan() #226

Closed EArminjon closed 4 years ago

EArminjon commented 4 years ago

Describe the bug My emulator crash when use BarcodeScanner.scan();

Screenshots If applicable, add screenshots to help explain your problem.

Logs

V/CameraPreview( 3507): Cancelling autofocus
D/AndroidRuntime( 3507): Shutting down VM
E/AndroidRuntime( 3507): FATAL EXCEPTION: main
E/AndroidRuntime( 3507): Process: net.beyondgravity.enterprises.enterprises, PID: 3507
E/AndroidRuntime( 3507): java.lang.RuntimeException: cancelAutoFocus failed
E/AndroidRuntime( 3507):    at android.hardware.Camera.native_cancelAutoFocus(Native Method)
E/AndroidRuntime( 3507):    at android.hardware.Camera.cancelAutoFocus(Camera.java:1275)
E/AndroidRuntime( 3507):    at me.dm7.barcodescanner.core.CameraPreview.setAutoFocus(CameraPreview.java:289)
E/AndroidRuntime( 3507):    at me.dm7.barcodescanner.core.BarcodeScannerView.setAutoFocus(BarcodeScannerView.java:296)
E/AndroidRuntime( 3507):    at de.mintware.barcode_scan.ZXingAutofocusScannerView.setAutoFocus(ZXingAutofocusScannerView.kt:26)
E/AndroidRuntime( 3507):    at me.dm7.barcodescanner.core.BarcodeScannerView.setupCameraPreview(BarcodeScannerView.java:190)
E/AndroidRuntime( 3507):    at de.mintware.barcode_scan.ZXingAutofocusScannerView.setupCameraPreview(ZXingAutofocusScannerView.kt:22)
E/AndroidRuntime( 3507):    at me.dm7.barcodescanner.core.CameraHandlerThread$1$1.run(CameraHandlerThread.java:31)
E/AndroidRuntime( 3507):    at android.os.Handler.handleCallback(Handler.java:751)
E/AndroidRuntime( 3507):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 3507):    at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime( 3507):    at android.app.ActivityThread.main(ActivityThread.java:6077)
E/AndroidRuntime( 3507):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3507):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/AndroidRuntime( 3507):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Environment :

EArminjon commented 4 years ago

I clone git@github.com:mintware-de/flutter_barcode_reader.git and run the example on my emulator, same issue...

EArminjon commented 4 years ago

I test several emulator, app work when api >= 27 (Android 8.1)...

subinsv commented 4 years ago

Looks like this issue is related to https://github.com/dm77/barcodescanner/issues/208

prokons commented 4 years ago

I'm having same problem on API 23 emulator. I see that dm77/barcodescanner#208 is an old issue and it seems no one is looking at that.

prokons commented 4 years ago

In kotlin class ZXingAutofocusScannerView I inserted a try-catch to bypass error

    override fun setAutoFocus(state: Boolean) {
        try {
            super.setAutoFocus(callbackFocus)
        } catch (ex: Exception) {
        }
    }

It's just a workaround and actually I don't know if this create any other problem but now app isn't crashing anymore on cancelAutoFocus and it seems it's running smooth.

devtronic commented 4 years ago

@prokons feel free to create a PR

oznecniV97 commented 4 years ago

I have the same problem on an eumlated AVD device (API 24). I found this solution:

//solution found on https://stackoverflow.com/a/9127845/6395540
Camera.Parameters p = mCamera.getParameters();
List<String> focusModes = p.getSupportedFocusModes();

if(focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
    //Phone supports autofocus!
}
else {
    //Phone does not support autofocus!
}

I thinkthat a good thing can be insert the control inside the method that run setAutoFocus method (ZXingAutofocusScannerView.kt:26) and print log on autofocus failure instead of throwing the exception.

prokons commented 4 years ago

@oznecniV97 I think your solution is better and more complete than mine. I just don't know how to pass mCamera to setAutoFocus method without breaking the override

oznecniV97 commented 4 years ago

Add the fix as merge request (https://github.com/mintware-de/flutter_barcode_reader/pull/228)

devtronic commented 4 years ago

Merged