nisrulz / qreader

:white_square_button: [Android Library] Read QR codes using google's mobile vision api, but without the hassle
http://nisrulz.github.io/qreader
Apache License 2.0
371 stars 108 forks source link

Bug : Fail to connect to camera service - With explanation, steps to reproduce and possible fix #55

Open LainCoubrt opened 6 years ago

LainCoubrt commented 6 years ago

Hi. I had the RuntimeException: Fail to connect to camera service. Log:

java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:545)
    at android.hardware.Camera.open(Camera.java:385)
    at com.google.android.gms.vision.CameraSource.zzbjo(Unknown Source)
    at com.google.android.gms.vision.CameraSource.start(Unknown Source)
    at github.nisrulz.qreader.QREader.startCameraView(QREader.java:221)
    at github.nisrulz.qreader.QREader.access$400(QREader.java:40)
    at github.nisrulz.qreader.QREader$1.surfaceCreated(QREader.java:68)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:607)
    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:246)
    at android.view.View.dispatchWindowVisibilityChanged(View.java:9322)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1285)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
    at android.view.Choreographer.doFrame(Choreographer.java:560)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5951)
    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:1400)

This is due to the camera being used from another app and not be released when the app goes to background.

I can give the steps to reproduce this issue if you need.

This exception is thrown in QREader.java in line 221, and is not caught by the IOException beacause it's a RunTimeException and we can't catch it because it's thrown in a background thread when surface is created.

So to just fix the crash just change the IOException for just an Exception.

Although this resolves the issue i think it's important to send feedback to the user and just catch the exception and print the error we cant do that. I think it would be useful some sort of listener so we could know the errors when the camera fails.

Please if you could change this as soon as possible would be very helpful. And if you need any help just ask.

LainCoubrt commented 6 years ago

The steps to reproduce:

We need an app that doesn't release the camera when it goes to background. So I downloaded google sample project of camera2Basic. And onPause() of the Camera2BasicFragment, commented closeCamera() and stopBackgroundThread().

Now just open QREader demo app then this app created. Now just click overview button that displays apps in "cascade" and click the demo app and it will crash with the RuntimeException.

nisrulz commented 6 years ago

@LainCoubrt I will look into it. The develop branch has some updates which are yet to be released. It also has a onError listener. If it works for you, I would suggest you check out the module and use it for the time being. Be aware it has not been tested which is why it has not been released. I need to test and put in a lot of fixes before releasing it.

However if it doesn't fix it, I will need to look into the issue and provide you a fix whenever I can make it available. Thank you for providing the steps to reproduce the issue.