react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

🐛 Black screen flashes for a second before camera appears

Open vladimir-vasilev opened this issue 2 years ago • 5 comments

What were you trying to do?

I want to use back Camera

Reproduceable Code

const [hasPermission, setHasPermission] = useState(false);
  const devices = useCameraDevices();
  const device = devices.back;

  useEffect(() => {
    (async () => {
      const status = await Camera.requestCameraPermission();
      setHasPermission(status === 'authorized');
    })();
  }, []);

  return device && hasPermission
    ? <Camera style={StyleSheet.absoluteFill} device={device} isActive={true} />
    : null;

What happened instead?

Black screen flashes for a second before camera appears. Reproduces on many devices and simulators, on iOS and android.

"react-native": "0.68.2"

Relevant log output

No response

Device

iPhone 11 (iOS 15.0), Samsung Galaxy S20 FE (Android 12)

VisionCamera Version

2.13.0

Additional information

vladimir-vasilev avatar Jul 19 '22 12:07 vladimir-vasilev

I don't understand, where exactly is the problem here?

mrousavy avatar Jul 19 '22 13:07 mrousavy

when the camera appears, the first thing I see is the black screen. I expect to see the camera image at once, not the black screen

vladimir-vasilev avatar Jul 19 '22 14:07 vladimir-vasilev

You are rendering null and the request for camera permission is in a useEffect so it will render initially without displaying the camera component and then (if it has the permission) render the camera component. A simple way to get around this would be to check the permission before navigating to the screen where you have the camera in the first place or just display a permission place holder rather than null?

thomas-coldwell avatar Jul 19 '22 14:07 thomas-coldwell

yes, but I see the issue in Camera component, when it's rendered. I tried with different screen as well.

vladimir-vasilev avatar Jul 19 '22 14:07 vladimir-vasilev

Having the same issue, short flickers (two) of black that actually also overflow the parent container, which looks kinda ugly (even if the camera is given props for dimensions like 100px that are easily within the bounds of the parent containerl).

After the actual camera image is rendered everything looks as expected.

Edit: It seems like calling VisionCamera.requestCameraPermission() onLayout causes one of the two flickers, even when nothing is done with the result of the promise (so no state update). Not sure if that helps.

rmyr avatar Aug 11 '22 10:08 rmyr

This is happening because the status of hasPermission when opening the screen is false, it is still querying the permission request.

I'm having the same problem and I'm trying to solve for the permission to be accessed when the app starts.

mssoliveira avatar Dec 22 '22 12:12 mssoliveira

I faced the same issue and resolved it by checking the permission status before asking permission.

if (await Camera.getCameraPermissionStatus() !== 'authorized') {
    if (await Camera.requestCameraPermission() === 'denied') {
        setSettingsModalOpen(true)
    }
}

kashandanish avatar May 16 '23 12:05 kashandanish

Closing as this is a stale issue - this might have been fixed with the full rewrite in VisionCamera V3 (🥳) - if not, please create a new issue.

mrousavy avatar Sep 30 '23 09:09 mrousavy