ideacreation / react-native-barcodescanner

A barcode scanner component for react native - not maintained anymore - use react-native-camera
MIT License
537 stars 162 forks source link

Camera shows black viewfinder after device has been in "sleep mode" or "screen timeout" #51

Open unipheye opened 7 years ago

unipheye commented 7 years ago

Hi,

The camera doesn't wake up / work after the device has been in sleep mode or the screen was in screen timeout mode.

Test Device

How to replicate the issue:

  1. Open app that uses the react-native-barcodescanner and go to the view that uses the camera
  2. Navigate away from the view to any view that doesn't use the camera
  3. Let the device go to sleep or in screen timeout mode
  4. Unlock screen and go to view which uses the camera

In all of my apps the viewfinder is black after walking through the previous steps

What I found so far:

09-01 22:56:55.833: E/CameraPreview(15121): java.lang.RuntimeException: Camera is being used after Camera.release() was called 09-01 22:56:55.833: E/CameraPreview(15121): java.lang.RuntimeException: Camera is being used after Camera.release() was called 09-01 22:56:55.833: E/CameraPreview(15121): at android.hardware.Camera.native_cancelAutoFocus(Native Method) 09-01 22:56:55.833: E/CameraPreview(15121): at android.hardware.Camera.cancelAutoFocus(Camera.java:1348) 09-01 22:56:55.833: E/CameraPreview(15121): at com.eguma.barcodescanner.CameraPreview.stopCameraPreview(CameraPreview.java:89) 09-01 22:56:55.833: E/CameraPreview(15121): at com.eguma.barcodescanner.CameraPreview.stopCamera(CameraPreview.java:52) 09-01 22:56:55.833: E/CameraPreview(15121): at com.eguma.barcodescanner.BarcodeScannerView.onPause(BarcodeScannerView.java:43) 09-01 22:56:55.833: E/CameraPreview(15121): at com.eguma.barcodescanner.BarcodeScannerManager.onHostPause(BarcodeScannerManager.java:58) 09-01 22:56:55.833: E/CameraPreview(15121): at com.facebook.react.bridge.ReactContext.onHostPause(ReactContext.java:179) 09-01 22:56:55.833: E/CameraPreview(15121): at com.facebook.react.XReactInstanceManagerImpl.moveToBeforeResumeLifecycleState(XReactInstanceManagerImpl.java:573) 09-01 22:56:55.833: E/CameraPreview(15121): at com.facebook.react.XReactInstanceManagerImpl.onHostPause(XReactInstanceManagerImpl.java:489) 09-01 22:56:55.833: E/CameraPreview(15121): at com.facebook.react.ReactActivity.onPause(ReactActivity.java:120) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.Activity.performPause(Activity.java:6765) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1338) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4418) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4391) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4366) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread.access$1200(ActivityThread.java:219) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1746) 09-01 22:56:55.833: E/CameraPreview(15121): at android.os.Handler.dispatchMessage(Handler.java:102) 09-01 22:56:55.833: E/CameraPreview(15121): at android.os.Looper.loop(Looper.java:145) 09-01 22:56:55.833: E/CameraPreview(15121): at android.app.ActivityThread.main(ActivityThread.java:6939) 09-01 22:56:55.833: E/CameraPreview(15121): at java.lang.reflect.Method.invoke(Native Method) 09-01 22:56:55.833: E/CameraPreview(15121): at java.lang.reflect.Method.invoke(Method.java:372) 09-01 22:56:55.833: E/CameraPreview(15121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 09-01 22:56:55.833: E/CameraPreview(15121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Please help! I really need this to work.

Thanks.

Regards, Gawein

AugustoL commented 7 years ago

Hi @unipheye , any update on this? having the same issue here :(

unipheye commented 7 years ago

Hi @AugustoL, I haven't found a solution yet. Though I have a app where this issue doesn't seem to occur. I am going to take a deep dive today. I will post all findings later.

unipheye commented 7 years ago

This issue doesn't seem to be the same issue as #47 "pop to the Previous scene in android 4.x black scene in screen"

unipheye commented 7 years ago

Small update:

I have figured out where the "Camera is being used after Camera.release() was called" came from. In the source of react-native-barcodescanner, when the device goes into sleep mode, two functions are being called after the camera is already released: "stopCameraPreview()" and "mCameraManager.releaseCamera()". This triggered the error. Just to get the error away I have made the following modifications to the source.

CameraPreview.java

Add a global variable private boolean mReleased = false;

Alter stopCamera function

public void stopCamera() {
        if(!mReleased){
            stopCameraPreview();
            mCameraManager.releaseCamera();
            mReleased = true;
        }
    }

Modify startCameraPreview() by adding the following line between the try block mReleased = false;

Unfortunately this does not solve the issue with the black screen. It somehow seems as if the preview cannot be started after the device when into sleep mode. In the android device monitor I get the following error:

SprdCameraHardware(177) void android::SprdCameraHardware::receivePreviewFrame(camera_frame_type*): displayOneFrame not successful!

I can't find anything sensible about this error. So I am currently stuck at finding a solution ...

getreup commented 7 years ago

Hi!

I have fixed this and made a pr here: https://github.com/ideacreation/react-native-barcodescanner/pull/59

The issue is that this library does not handle multiple camera instances properly, and react's handling of the module instances shouldn't really be depended on. So, all I did was make the camera static.

unipheye commented 7 years ago

Hi @getreup,

Thanks for responding to this issue but unfortunately I still get a black screen and the same error: SprdCameraHardware(177) void android::SprdCameraHardware::receivePreviewFrame(camera_frame_type*): displayOneFrame not successful!

But you did experience the same issues and now they are gone? Could you give me some more info on that?

Thanks

getreup commented 7 years ago

Hi @unipheye

I found that the mCamera instance was being created multiple times. If you put a break point at new Camera(), then you might see the same. The issue happens when the second instance is being used instead of the first. So, I figured making it static would be an easy hack to get this going, especially since the library doesn't handle multiple instances of mCamera anyways, so it kinda makes sense.

igor-bv commented 7 years ago

Hi @getreup ! making mCamera static didn't help me. if you have any other ideas - please post them. thanks in advance! Android 4.4.2 RN 0.34.1 Scanner 3.1.1

getreup commented 7 years ago

@bo4a10 Hey can you try debugging and seeing if there are multiple instances of "new Camera()" being created? That would be my best advice.

alex44jzy commented 7 years ago

Hi @getreup I have tried your method, but it also doesn't work for me. If you have any progress I will try again. This performance bug makes me very despaired.

getreup commented 7 years ago

That sucks ... the only thing I did is in the PR. But I would just suggest throwing breakpoints wherever "new Camera()" is being called and make sure you're never really recreating it twice.

jiaminglu commented 7 years ago

I experienced this issue on my Meizu MX5, fixed it by making CameraManager a singleton. Please check whether pr #61 solved your problem @bo4a10

wv1124 commented 7 years ago

@jiaminglu thanks.

igor-bv commented 7 years ago

Hi @jiaminglu! Yes, your PR fixed problem as for me. Nice work. Thank you!

kingorange2309 commented 7 years ago

hi @jiaminglu ,thank you! Issue has fixed. However, i have another performace problem. When i exit the app and open it again each view runs not smoothly. But when i go to the view which uses the camera, besides first open this view is not smoothly, the app runs smoothly everywhere.

WaydeWang commented 7 years ago

@jiaminglu it works, thank you!

unipheye commented 7 years ago

@jiaminglu it really works. This bug was a real thorn in my side. Thank you!

tnortman commented 7 years ago

@jiaminglu Thank you, that totally worked!

ozuit commented 7 years ago

@jiaminglu Thank you very much! You are wonderful 👍