Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❓ Getting a Black screen when using camera #2291

Closed
3 of 4 tasks
FazilMuhammed opened this issue Dec 15, 2023 · 39 comments · Fixed by #2339 · May be fixed by #2299
Closed
3 of 4 tasks

❓ Getting a Black screen when using camera #2291

FazilMuhammed opened this issue Dec 15, 2023 · 39 comments · Fixed by #2339 · May be fixed by #2299
Labels
💭 question Further information is requested

Comments

@FazilMuhammed
Copy link

Question

{isFocused && (
<Camera
style={StyleSheet.absoluteFill}
// style={{width: 500, height: 500}}
device={device}
codeScanner={codeScanner}
isActive={isActive}
lowLightBoost={true}
key={${resetKey}}
onInitialized={onInitialized}
onError={error => {
if (
error.message.includes(
'CameraDevice was already closed',
)
) {
setResetKey(prev => prev + 1);
}
}}
/>
)}

What I tried

I need scan QR code I done these steps

./android/gradlew clean
rm -rf android/.gradle android/.idea android/app/build android/build
rm -rf package-lock.json yarn.lock node_modules
yarn # or npm I

still remains black screen

VisionCamera Version

"react-native-vision-camera": "^3.6.17",

Additional information

@FazilMuhammed FazilMuhammed added the 💭 question Further information is requested label Dec 15, 2023
@jslok
Copy link

jslok commented Dec 15, 2023

3.6.17 gives me a black screen.
3.6.16 works.

@Gruak
Copy link

Gruak commented Dec 15, 2023

When I wrap the Camera component with SafeAreaView then it works (no black screen). Weird.

@duhuajin
Copy link

same here

@AlexherDev69
Copy link

same here, but then it works when I flip the camera

When I wrap the Camera component with SafeAreaView then it works (no black screen). Weird.

That worked for me too, thanks

@M4Tdev
Copy link

M4Tdev commented Dec 15, 2023

I have the same issue, and I found out that when I turn on the flash it somehow refreshes/activates the camera itself and the image from it shows up.

@android-imdad
Copy link

@mrousavy I did some digging, it seems the even though we set the Camera as "isActive" the camera module is showing isActive=false in the logs

Successfully updated CameraSession Configuration! isActive: false

Turning on flash actually refreshes the camera view and removes the black screen to show the preview.

@jaimeneto85
Copy link

When I wrap the Camera component with SafeAreaView then it works (no black screen). Weird.

I had the same problema on versions 3.6.17 and 3.6.16 as well.

but when I tried. to wrap with SafeAreaView:

<SafeAreaView style={StyleSheet.absoluteFill}>
          <Camera
            ref={ref}
            video
            format={format}
            device={devices}
            isActive={true}
            style={StyleSheet.absoluteFill}
          />
        </SafeAreaView>
        

OBS: without absolutFill the screen turn on White. Weird!

@basitmir
Copy link

Same issue on 3.6.16
Sometimes it works with no black screen but sometimes it shows black screen
@mrousavy

@FazilMuhammed FazilMuhammed changed the title ❓ Getting a Black screen when useing camera ❓ Getting a Black screen when using camera Dec 16, 2023
@FazilMuhammed

This comment was marked as spam.

@android-imdad
Copy link

When I wrap the Camera component with SafeAreaView then it works (no black screen). Weird.

I had the same problema on versions 3.6.17 and 3.6.16 as well.

but when I tried. to wrap with SafeAreaView:

<SafeAreaView style={StyleSheet.absoluteFill}>
          <Camera
            ref={ref}
            video
            format={format}
            device={devices}
            isActive={true}
            style={StyleSheet.absoluteFill}
          />
        </SafeAreaView>
        

OBS: without absolutFill the screen turn on White. Weird!

I am on version 3.5.1 because of the crash issue and this is still a persistent issue.

@android-imdad
Copy link

When I wrap the Camera component with SafeAreaView then it works (no black screen). Weird.

I had the same problema on versions 3.6.17 and 3.6.16 as well.

but when I tried. to wrap with SafeAreaView:

<SafeAreaView style={StyleSheet.absoluteFill}>
          <Camera
            ref={ref}
            video
            format={format}
            device={devices}
            isActive={true}
            style={StyleSheet.absoluteFill}
          />
        </SafeAreaView>
        

OBS: without absolutFill the screen turn on White. Weird!

It seems this also does not work as expected, sometimes it loads the camera and other times its just a black screen

@android-imdad
Copy link

Found a temporary fix for this issue, You need to update line number 42 in CameraConfiguration.Kt to
var isActive: Boolean = true
and line number 208 in CameraView.kt to
config.isActive = true;

This seems to fix the issue for now, not sure if there will be any unintended consequences but seems to be working fine.

@jaimeneto85
Copy link

Found a temporary fix for this issue, You need to update line number 42 in CameraConfiguration.Kt to var isActive: Boolean = true and line number 208 in CameraView.kt to config.isActive = true;

This seems to fix the issue for now, not sure if there will be any unintended consequences but seems to be working fine.

the config.isActive = true; for me make no sense, bacause config.isActive check isActive && isAttachedToWindow. Changing config.isActive = isActive it's enough. The problem is isAttachedToWindow is changing the state. start with true (correctly) but change for false (this is the reason that works wrappign the code witn "SafeAreaView". I did a PR (#2299) right now to change this line that I said.

@android-imdad
Copy link

Found a temporary fix for this issue, You need to update line number 42 in CameraConfiguration.Kt to var isActive: Boolean = true and line number 208 in CameraView.kt to config.isActive = true;
This seems to fix the issue for now, not sure if there will be any unintended consequences but seems to be working fine.

the config.isActive = true; for me make no sense, bacause config.isActive check isActive && isAttachedToWindow. Changing config.isActive = isActive it's enough. The problem is isAttachedToWindow is changing the state. start with true (correctly) but change for false (this is the reason that works wrappign the code witn "SafeAreaView". I did a PR (#2299) right now to change this line that I said.

Even though this fixes the camera preview, barcode or QR code scanning does not work as code scanner is being turned into null for some reason. I added a temporary fix for it by adding the scanning parameters manually in line number 348 in CameraSession.kt

`// val codeScanner = configuration.codeScanner as? CameraConfiguration.Output.Enabled<CameraConfiguration.CodeScanner>

var scannerOptions : List<CodeType> = listOf(CodeType.CODE_39,CodeType.CODE_128,CodeType.QR,CodeType.EAN_13);
// CodeScanner Output
val codeScanner = CameraConfiguration.Output.Enabled.create(
  CameraConfiguration.CodeScanner(scannerOptions)
)`

@FazilMuhammed
Copy link
Author

FazilMuhammed commented Dec 18, 2023

@android-imdad when I replace from.

// val codeScanner = configuration.codeScanner as? CameraConfiguration.Output.Enabled<CameraConfiguration.CodeScanner>

to

var scannerOptions : List = listOf(CodeType.CODE_39,CodeType.CODE_128,CodeType.QR,CodeType.EAN_13);
// CodeScanner Output
val codeScanner = CameraConfiguration.Output.Enabled.create(
CameraConfiguration.CodeScanner(scannerOptions)
)

getting error

node_modules/react-native-vision-camera/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt: (361, 97): Unresolved reference: CodeType

@android-imdad
Copy link

@android-imdad when I replace from.

// val codeScanner = configuration.codeScanner as? CameraConfiguration.Output.Enabled<CameraConfiguration.CodeScanner>

to

var scannerOptions : List = listOf(CodeType.CODE_39,CodeType.CODE_128,CodeType.QR,CodeType.EAN_13); // CodeScanner Output val codeScanner = CameraConfiguration.Output.Enabled.create( CameraConfiguration.CodeScanner(scannerOptions) )

getting error

node_modules/react-native-vision-camera/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt: (361, 97): Unresolved reference: CodeType

use the following import

import com.mrousavy.camera.types.CodeType

Even after all this the scanner is still crashing, so I switched to react-native-camera-kit (only downside is, I can't get the type of code scanned but works perfectly for my use case

https://github.com/teslamotors/react-native-camera-kit

@feardarkness
Copy link

feardarkness commented Dec 18, 2023

@M4Tdev solution works for now (using flash as a new isActive flag on Android) for both camera and barcode scanner. I've noticed that I had to set an small delay when setting isActive to true in some android devices.

<Camera
  // all other properties
   isActive={isCameraActive}
   flash={isCameraActive}
   />

@Ivanindiani
Copy link

Ivanindiani commented Dec 18, 2023

I have the flash in a state, when changing the state the camera activates correctly.

                `const [flash, setFlash] = useState('on');
                  ......
                  <Camera fps={25}
                    torch={flash === 'on' ? 'on':'off'}
                    onInitialized={() => setTimeout(()=>setFlash('off'),800)}
                    style={[StyleSheet.absoluteFill, styles.cam]}
                    device={device} 
                    isActive={isActive} 
                    codeScanner={codeScanner} 
                    lowLightBoost={false} />`

@fontesrp
Copy link

Initializing isActive as false and switching it to true a second after the Camera mounts work on my test devices as well:

  const [isActive, setIsActive] = useState(Platform.OS === 'ios')

  useEffect(() => {
    if (Platform.OS === 'ios') {
      return
    }

    if (permissionsGranted && device) {
      const timeout = setTimeout(() => setIsActive(true), 1e3)
      return () => clearTimeout(timeout)
    }

    setIsActive(false)
  }, [device, permissionsGranted])

  return appState === 'active' && isActive

@FazilMuhammed
Copy link
Author

@fontesrp can you share full screen code here. it will help to understand all

@iqorlobanov
Copy link

@fontesrp can you share full screen code here. it will help to understand all

Something like that. I actually had another issue with the camera on Android, but it was also fixed by setting isActive to true after a short delay.

const [isCameraInitialized, setIsCameraInitialized] = useState(false);
const [isActive, setIsActive] = useState(Platform.OS === 'ios');

const isFocused = useIsFocused();
const isForeground = useIsForeground();

useEffect(() => {
      if (Platform.OS === 'ios') {
          return () => {};
      }

      let timeout: NodeJS.Timeout;

      if (isCameraInitialized) {
          timeout = setTimeout(() => setIsActive(true), 150);
      }

      setIsActive(false);
      return () => {
          clearTimeout(timeout);
      };
  }, [isCameraInitialized]);

const onInitialized = useCallback(() => {
    setIsCameraInitialized(true);
}, []);

<Camera
    ...
    isActive={isActive && isFocused && isForeground && isCameraInitialized}
    onInitialized={onInitialized}
/>

@cu4nt0m
Copy link

cu4nt0m commented Dec 19, 2023

3.5.1 Currently works for me, this is really weird to deal with.

@SungKyuMM
Copy link

SungKyuMM commented Dec 20, 2023

setTimeout solved this issue.
However, since I need to use the camera frequently, 1 second is too annoying...

@iqorlobanov
Copy link

setTimeout solved this issue.

However, since I need to use the camera frequently, 1 second is too annoying...

You can reduce the timeout to 150ms. This is enough to avoid the problem

@SungKyuMM
Copy link

When I tested with 500ms on S23, it didn't work, so I restored it to 1 second.

@iqorlobanov
Copy link

iqorlobanov commented Dec 20, 2023

When I tested with 500ms in S23, it didn't work, so I restored it to 1 second.

Try setting a timeout after the camera is initialized (onInitialized() method)

#2291 (comment)

@SungKyuMM
Copy link

Try setting a timeout after the camera is initialized (onInitialized() method)

#2291 (comment)

When I used onInitialized, solved it. thank you! :).

@erwanlpfr
Copy link
Contributor

I tried almost every workaround here and this always appeared on random devices.
I downgraded to the previous version : "react-native-vision-camera": "^3.6.16".

@andreasaker
Copy link

Encountered the same issue where "Camera" doesn't appear to register "isActive," resulting in a black screen.
After almost ripping my only hair strand i saw this thread and downgraded to "3.6.16". Now it works flawlessly again, thank you all. 👍
Downgraded from "3.6.17" to "3.6.16".

@amilaupendra
Copy link

Downgraded from "3.6.17" to "3.6.16". It works.

@BLOCKMATERIAL
Copy link

Downgraded from "3.6.17" to "3.6.16" also works.

ping, need hot-fix

phone: Google Pixel 4

@mrousavy
Copy link
Owner

mrousavy commented Jan 5, 2024

Hey! Thanks for reporting this issue.

I've been working the past days on making sure the Camera lifecycle is safely handled and released when needed to make sure this crash doesn't occur anymore.

I just created a PR for this - can you please this to see if that fixes the issue for you? #2339

If this fixes your issue, please consider 💖 sponsoring me on GitHub 💖 to support me / thank me for building VisionCamera and continuously improving it.

If this does not fix your issue, please clone the repo, check out the branch fix/blackscreen (the PR above), run the Example app, reproduce your issue there and share the adb logcat logs with me (upload to pastebin or gist) so I can investigate this further.

Thank you! 🙏

@aberic1992
Copy link

aberic1992 commented Jan 25, 2024

I just wanna add, this is not fixed in the latest 2.x.x version.
I solved this by rerendering camera component by changing key prop on every focus.

@mrousavy
Copy link
Owner

I just wanna add, this is not fixed in the latest 2.x.x version.

Yes, because main branch is V3 since almost half a year

@aberic1992
Copy link

aberic1992 commented Jan 25, 2024

@mrousavy I understand, but we just couldn't easily update to the latest version, and we needed solution asap. Maybe someone else is in a similar situation...

@ErAmanDhiman
Copy link

@mrousavy , Why we needs SetTimeOut for set camera active , The camera screen remains black , if we do not use settimeout function and not works less than One Second delay , Please Have a look Once .
carbon

@tomerh2001
Copy link

On 3.8.2 this is still an issue

@genaroNF
Copy link

genaroNF commented Apr 7, 2024

Hello, not sure if what I am experiencing is realted to this error but I created an app using the example in the readme and got this problem where everything is black except for sources of light like a lightbulb I am adding a screenshot down below. Any idea what could be happening? Here is the code I am using

Screenshot_2024-04-07-17-12-06-032_com osohormigueroapp

@genaroNF
Copy link

genaroNF commented Apr 7, 2024

I want to clarify that my camera is working just fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💭 question Further information is requested
Projects
None yet