dlazaro66 / QRCodeReaderView

Modification of ZXING Barcode Scanner project for easy Android QR-Code detection and AR purposes
1.9k stars 492 forks source link

Failed to connect to camera service #24

Closed Kyson closed 8 years ago

Kyson commented 9 years ago

hi, i write codes as following,same to your demo

  1. find QRCodeReaderView in xml
  2. add listener
  3. onresume startpreview and onpause stop it

i have two activity,both need to read qrcode,and when i jump first activity to another ,a problem occurs:Failed to connect to camera service.

please help.thanks a lot

Philip-Nunoo commented 8 years ago

@Kyson noticed with android M you'd need to turn on camera permission with the device

nielsswinkels commented 8 years ago

I'm getting this sometimes as well, and also other apps that use the camera can't connect to it then. So far the only solution is rebooting the phone. I think we should do mydecoderview.getCameraManager().getCamera().release(); in onPause() like it says in the Android documentation: https://developer.android.com/reference/android/hardware/Camera.html

But I haven't figured out yet how to open the camera again in onResume().

janskod commented 8 years ago

Got the same problem on Nexus 5 and S3 Mini. I also guess its because a release is not called in the pause func. In my scenario i solved it by always calling finish() in on pause, which will call the destroySurface method. Inside the destroySurface method the release is called. But this only works if you want the QR Activity always be closed if paused or so. A more clever solution would be nice.

awadeyar commented 8 years ago

Hi,

In Android Marshmellow 6.x version the library gives the error and app crashes.

\ FATAL EXCEPTION: main Process: com.jnj.pave.stage, PID: 15782 java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:518) at android.hardware.Camera.open(Camera.java:360) at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:57) at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:77) at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:110) at android.view.SurfaceView.updateWindow(SurfaceView.java:583) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5466) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)**

Anybody has solution to this issue?

milanPansuriya commented 8 years ago

Hey, My App crashes in many Devices with the Error below: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.native_setup(Native Method) at android.hardware.Camera.<init>(Camera.java:407) at android.hardware.Camera.open(Camera.java:325) at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:57) at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:78) at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:90) at android.view.SurfaceView.updateWindow(SurfaceView.java:572) at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:232) at android.view.View.dispatchWindowVisibilityChanged(View.java:8113) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1257) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1020) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5762) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:771) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:757) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:149) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method) Your help will be appreciable.

awadeyar commented 8 years ago

Hi milanPansuriya are you running the app on Android 6.XX? If yes, it is because of the camera permission issue of Android 6.XX version.

You can use this code in your activity on create method.

int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.M){ if (ContextCompat.checkSelfPermission(PaveMainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(PaveMainActivity.this,
                    Manifest.permission.CAMERA)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

                ActivityCompat.requestPermissions(PaveMainActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        1);

            } else {

                // No explanation needed, we can request the permission.
                //Toast.makeText(getActivity(),"else",Toast.LENGTH_LONG).show();
                ActivityCompat.requestPermissions(PaveMainActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        1);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }

    } else{
        // do something for phones running an SDK before lollipop
    }

Use this code outside the on create method.

@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {
                System.exit(0);
                finish();
                finishAffinity();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
dlazaro66 commented 8 years ago

If you're running Android 5.0 or 6.0, this is not a QRCodeReaderView issue since you need to check the Camera permission in runtime before using the view (please check: http://developer.android.com/intl/training/permissions/index.html)

Moctar-Haiz commented 6 years ago

I came up with the same problem and I'm sharing how I fixed it. It may help some people.

First, check your Android version. If it is running on Android 6.0 and higher (API level 23+), then you need to :

  1. Declare a permission in the app manifest. Make sure to insert the permission above the application tag.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.snazzyapp">

**<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />**

<application ...>
    ...
</application>

  1. Then, request that the user approve each permission at runtime

    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // here, Permission is not granted ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.CAMERA}, 50); }

For more information, have a look at the API documentation here