Please provide the following:
- SDK Version: 42.0.0
- Platforms(Android/iOS/web/all): Android
I’d like to set ratio
and pictureSize
after Camera is ready in order to select a good picture size. Setting the ratio works fine, but when altering pictureSize
in handleCameraReady
, I’ll always get an error “Camera is not running” when trying to call camera functions. Is there anything I’m doing wrong?
Thanks for your help,
Florian
Excerpt from my code:
const PREFERRED_RATIOS = ['16:9', '4:3']
const cameraRef = useRef()
// initial values
const [selectedRatio, setSelectedRatio] = useState('4:3')
const [selectedPictureSize, setSelectedPictureSize] = useState('640x480')
const handleCameraReady = async () => {
if (Platform.OS === 'android') {
const supportedRatios = await cameraRef.current?.getSupportedRatiosAsync()
const selectedRatio = PREFERRED_RATIOS.find(pr => supportedRatios.includes(pr)) ?? supportedRatios[0]
const availablePictureSizes = await cameraRef.current?.getAvailablePictureSizesAsync(selectedRatio)
setSelectedRatio(selectedRatio)
setSelectedPictureSize(availablePictureSizes[availablePictureSizes.length - 1])
}
setIsCameraReady(true)
}
<Camera
onCameraReady={handleCameraReady}
pictureSize={selectedPictureSize}
ratio={selectedRatio}
ref={cameraRef}
style={styles.camera}
type={Camera.Constants.Type.back}
useCamera2Api
>
// ...
</Camera>