0

I am trying to run a basic example of react-native-vision-camera and getting the error device is null. I checked the app permissions through android phone and camera permissions are granted. Here is my current code:


import React,{useEffect, useState} from 'react';
import { Text, View, Linking} from 'react-native';
import {useCameraDevices, Camera} from 'react-native-vision-camera';


const App = () => {

  const devices = useCameraDevices();
  const device = devices.back;

React.useEffect( () => {
  requestCameraPermission();
}, [])


const requestCameraPermission = React.useCallback( async () => {

const permission = await Camera.requestCameraPermission();

if (permission == 'denied')
{
console.log("Permission not granted");  
await Linking.openSettings();
}

  }, [])


  function renderCamera()
  {
    if(device == null)
    {
      console.log("device is null");
      
      return (<View><Text>Camera not working</Text></View>)
    }
    else 
    {
      <View>
        <Camera
       
        device={device}
        isActive={true}
        enableZoomGesture
        />
      </View>
    }
  }
  
    return(
  <View>
    
    {renderCamera()}
  </View>
    )
  }  
    
 export default App;

Osman Khalid
  • 778
  • 1
  • 7
  • 22
  • Have you done this guide: https://mrousavy.com/react-native-vision-camera/docs/guides/#requesting-permissions ? – darshilsakhiya Jan 04 '23 at 10:44
  • yes, actually when I console.log() the device object, it gives me a lot of information: {"devices": ["wide-angle-camera"], "formats": [{"autoFocusSystem": "none", "colorSpaces": [Array], "fieldOfView": 68.76868048838452, "frameRateRanges": [Array], .... continued, which means the device object is not null – Osman Khalid Jan 04 '23 at 11:11
  • @OsmanKhalid what is your device? how many cameras do you have? Have you tried `devices .['wide-angle-camera']` – Kirill Novikov Jan 05 '23 at 11:19

0 Answers0