journeyapps / zxing-android-embedded

Barcode scanner library for Android, based on the ZXing decoder
https://journeyapps.com/
Apache License 2.0
5.77k stars 1.27k forks source link

Fail to connect to camera service #358

Closed ckenergy closed 5 years ago

ckenergy commented 6 years ago

Description of the problem:

in ScannerActivity start a NewActivity, in NewActivity onResume use camera = Camera.open(); got Fail to connect to camera service error; i think in CameraInstance.close() method use cameraThread.enqueue(closer) is delay NewActivity.onResume() , so i call Camera.open() the camera is not release; when i use closer.run() instead cameraThread.enqueue(closer), it work

Which library version are you using? E.g. 3.2.0. 3.5.0 Which phone/tablet are you using, and which Android version does it run? (e.g. Samsung Galaxy S5, Android 5.0) android 4.4

leohan1992 commented 6 years ago

I got the same error

01-01 00:30:37.378 5908-5943/com.sunvua.android.createtagutil E/CameraInstance: Failed to open camera
                                                                                java.lang.RuntimeException: Fail to connect to camera service
                                                                                    at android.hardware.Camera.<init>(Camera.java:654)
                                                                                    at android.hardware.Camera.open(Camera.java:495)
                                                                                    at com.google.zxing.client.android.camera.open.OpenCameraInterface.open(OpenCameraInterface.java:83)
                                                                                    at com.journeyapps.barcodescanner.camera.CameraManager.open(CameraManager.java:138)
                                                                                    at com.journeyapps.barcodescanner.camera.CameraInstance$3.run(CameraInstance.java:190)
                                                                                    at android.os.Handler.handleCallback(Handler.java:815)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                    at android.os.Looper.loop(Looper.java:194)
                                                                                    at android.os.HandlerThread.run(HandlerThread.java:61)

library version 3.5.0
android 5.0 on an company pda

InfraRedApple commented 6 years ago

Same error here:

E/Camera: Error 2
D/CameraInstance: Starting preview
E/CameraInstance: Failed to start preview
          java.io.IOException: setPreviewTexture failed
              at android.hardware.Camera.setPreviewSurface(Native Method)
              at android.hardware.Camera.setPreviewDisplay(Camera.java:652)
              at com.journeyapps.barcodescanner.camera.CameraSurface.setPreview(CameraSurface.java:43)
              at com.journeyapps.barcodescanner.camera.CameraManager.setPreviewDisplay(CameraManager.java:170)
              at com.journeyapps.barcodescanner.camera.CameraInstance$5.run(CameraInstance.java:219)
              at android.os.Handler.handleCallback(Handler.java:751)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:154)
              at android.os.HandlerThread.run(HandlerThread.java:61)

Started the scanner via:

 if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

            IntentIntegrator integrator = new IntentIntegrator(this);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("USE VOLUME KEYS FOR FLASHLIGHT");
            integrator.setCameraId(0);  // Use a specific camera of the device
            integrator.setBeepEnabled(true);
            integrator.setBarcodeImageEnabled(true);
            integrator.initiateScan();

        }

Additionally this will pop up too:

E/Camera: Error 2
W/AutoFocusManager: Unexpected exception while focusing
                    java.lang.RuntimeException: autoFocus failed
                        at android.hardware.Camera.native_autoFocus(Native Method)
                        at android.hardware.Camera.autoFocus(Camera.java:1297)
                        at com.journeyapps.barcodescanner.camera.AutoFocusManager.focus(AutoFocusManager.java:104)
                        at com.journeyapps.barcodescanner.camera.AutoFocusManager.access$100(AutoFocusManager.java:31)
                        at com.journeyapps.barcodescanner.camera.AutoFocusManager$1.handleMessage(AutoFocusManager.java:57)
                        at android.os.Handler.dispatchMessage(Handler.java:98)
                        at android.os.Looper.loop(Looper.java:154)
                        at android.os.HandlerThread.run(HandlerThread.java:61)

Just before the warning popped up, here is the camera's configuration:

E/Camera: Error 2
 D/CameraInstance: Configuring camera
 I/CameraManager: Camera Display Orientation: 0
 I/CameraManager: Initial camera parameters: antibanding=auto;antibanding-values=auto,60hz;auto-exposure-lock-supported=true;auto-whitebalance-lock-supported=true;burstshot-fps-values=(5,5);constant-growth-rate-zoom-supported=true;effect=none;effect-values=none,mono,negative,sepia;exposure-compensation=0;exposure-compensation-step=0.5;flash-mode=off;flash-mode-values=off,auto,on,torch;focal-length=3.300000;focus-areas=(0,0,0,0,0);focus-distances=0.15,1.20,Infinity;focus-mode=auto;focus-mode-values=auto,infinity,macro,fixed;horizontal-view-angle=56;iso=auto;iso-values=auto,100,200,400;jpeg-quality=96;jpeg-thumbnail-height=120;jpeg-thumbnail-quality=100;jpeg-thumbnail-size-values=160x120,160x160,160x90,0x0;jpeg-thumbnail-width=160;max-exposure-compensation=4;max-num-detected-faces-hw=0;max-num-detected-faces-sw=3;max-num-focus-areas=1;max-num-metering-areas=0;max-zoom=30;min-exposure-compensation=-4;picture-format=jpeg;picture-format-values=jpeg;picture-size=2576x1932;picture-size-values=2576x1932,2560x1440,2560x1920,2560x1536,2048x1536,2048x1152,1920x1920,1920x1080,1600x1200,1280x960,1280x720,1024x768,640x480,320x240;preferred-preview-size-for-video=1280x720;preview-format=yuv420sp;preview-format-values=yuv420sp,yuv420p;preview-fps-range=15000,30000;preview-fps-range-values=(7000,7000),(15000,15000),(15000,30000),(30000,30000);preview-frame-rate=30;preview-frame-rate-values=7,15,30;preview-size=1280x720;preview-size-values=1280x720,1280x960,1024x768,720x720,720x480,640x480,352x288,320x240,176x144;scene-mode=auto;scene-mode-values=auto,portrait,landscape,night,beach,sunset,fireworks,sports,party,candlelight;smooth-zoom-supported=false;vertical-view-angle=41;video-frame-format=yuv420sp;video-size=1280x720;video-size-values=1280x720,720x480,640x480,320x240,176x144;video-snapshot-supported=true;video-stabilization-supported=false;whitebalance=auto;whitebalance-values=auto,incandescent,fluorescent,daylight,cloudy-daylight;zoom=0;zoom-ratios=100,104,109,114,120,125,131,138,144,151,158,166,174,182,190,200,209,219,229,240,251,263,276,289,303,317,332,348,364,381,400;zoom-supported=true
 I/CameraConfiguration: Requesting focus mode value from among: [auto]
 I/CameraConfiguration: Supported focus mode values: [auto, infinity, macro, fixed]
 I/CameraConfiguration: Can set focus mode to: auto
 I/CameraConfiguration: Focus mode already set to auto
 I/CameraConfiguration: Requesting flash mode value from among: [off]
 I/CameraConfiguration: Supported flash mode values: [off, auto, on, torch]
 I/CameraConfiguration: Can set flash mode to: off
 I/CameraConfiguration: Flash mode already set to off
 I/PreviewScalingStrategy: Viewfinder size: 1280x720
 I/PreviewScalingStrategy: Preview in order of preference: [1280x720, 1280x960, 1024x768, 720x480, 640x480, 720x720, 352x288, 320x240, 176x144]
 I/CameraManager: Final camera parameters: antibanding=auto;antibanding-values=auto,60hz;auto-exposure-lock-supported=true;auto-whitebalance-lock-supported=true;burstshot-fps-values=(5,5);constant-growth-rate-zoom-supported=true;effect=none;effect-values=none,mono,negative,sepia;exposure-compensation=0;exposure-compensation-step=0.5;flash-mode=off;flash-mode-values=off,auto,on,torch;focal-length=3.300000;focus-areas=(0,0,0,0,0);focus-distances=0.15,1.20,Infinity;focus-mode=auto;focus-mode-values=auto,infinity,macro,fixed;horizontal-view-angle=56;iso=auto;iso-values=auto,100,200,400;jpeg-quality=96;jpeg-thumbnail-height=120;jpeg-thumbnail-quality=100;jpeg-thumbnail-size-values=160x120,160x160,160x90,0x0;jpeg-thumbnail-width=160;max-exposure-compensation=4;max-num-detected-faces-hw=0;max-num-detected-faces-sw=3;max-num-focus-areas=1;max-num-metering-areas=0;max-zoom=30;min-exposure-compensation=-4;picture-format=jpeg;picture-format-values=jpeg;picture-size=2576x1932;picture-size-values=2576x1932,2560x1440,2560x1920,2560x1536,2048x1536,2048x1152,1920x1920,1920x1080,1600x1200,1280x960,1280x720,1024x768,640x480,320x240;preferred-preview-size-for-video=1280x720;preview-format=yuv420sp;preview-format-values=yuv420sp,yuv420p;preview-fps-range=15000,30000;preview-fps-range-values=(7000,7000),(15000,15000),(15000,30000),(30000,30000);preview-frame-rate=30;preview-frame-rate-values=7,15,30;preview-size-values=1280x720,1280x960,1024x768,720x720,720x480,640x480,352x288,320x240,176x144;scene-mode=auto;scene-mode-values=auto,portrait,landscape,night,beach,sunset,fireworks,sports,party,candlelight;smooth-zoom-supported=false;vertical-view-angle=41;video-frame-format=yuv420sp;video-size=1280x720;video-size-values=1280x720,720x480,640x480,320x240,176x144;video-snapshot-supported=true;video-stabilization-supported=false;whitebalance=auto;whitebalance-values=auto,incandescent,fluorescent,daylight,cloudy-daylight;zoom=0;zoom-ratios=100,104,109,114,120,125,131,138,144,151,158,166,174,182,190,200,209,219,229,240,251,263,276,289,303,317,332,348,364,381,400;zoom-supported=true;preview-size=1280x720
 I/CenterCropStrategy: Preview: 1280x720; Scaled: 1280x720; Want: 1280x720
 I/CameraPreview: Starting preview
 D/CameraInstance: Starting preview
 I/AutoFocusManager: Current focus mode 'auto'; use auto focus? true
 D/CameraPreview: pause()
 D/CameraInstance: Closing camera
 D/SensorManager: unregisterListener ::   
 D/CameraPreview: resume()
 D/CameraInstance: Opening camera
 D/SensorManager: registerListener :: 0, 200000, 0,  
E/Camera: Error 2

Permission to camera exists.

Library v 3.5 Samsung Galaxy Amp Prime: Android v 7.1.1

rkistner commented 5 years ago

For the original issue: By design, the camera management is performed in a background thread, so the camera may still be in use by the time the Activity is closed.

I recommend retrying opening the camera for a second if two.

For the other comments, please open a new issue if you still have trouble with the latest version.