2024-01-29 20:01:45 -07:00
|
|
|
import React, { useCallback, useRef, useState } from 'react'
|
|
|
|
import { StyleSheet, Text, View } from 'react-native'
|
2024-01-30 15:08:46 -07:00
|
|
|
import { Camera, useCameraPermission, useCameraDevice, useCameraFormat, PhotoFile, VideoFile, CameraRuntimeError } from 'react-native-vision-camera'
|
2024-01-29 20:01:45 -07:00
|
|
|
import { RecordingButton } from './capture-button'
|
2024-01-30 15:08:46 -07:00
|
|
|
import { useIsForeground } from './is-foreground'
|
2024-01-31 16:55:33 -07:00
|
|
|
import { useIsFocused } from '@react-navigation/native'
|
|
|
|
|
2024-01-29 20:01:45 -07:00
|
|
|
|
|
|
|
export default function CameraScreen(): React.ReactElement {
|
|
|
|
const camera = useRef<Camera>(null)
|
|
|
|
const { hasPermission, requestPermission } = useCameraPermission()
|
|
|
|
const [isCameraInitialized, setIsCameraInitialized] = useState(false)
|
2024-01-30 01:15:23 -07:00
|
|
|
|
2024-01-30 15:08:46 -07:00
|
|
|
const isForeground = useIsForeground()
|
2024-01-31 16:55:33 -07:00
|
|
|
const isFocused = useIsFocused();
|
|
|
|
const isActive = isForeground && isFocused
|
2024-01-30 15:08:46 -07:00
|
|
|
|
|
|
|
const onError = useCallback((error: CameraRuntimeError) => {
|
|
|
|
console.error(error)
|
|
|
|
}, [])
|
2024-01-29 20:01:45 -07:00
|
|
|
|
|
|
|
const onInitialized = useCallback(() => {
|
|
|
|
console.log('Camera initialized!')
|
|
|
|
setIsCameraInitialized(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const onMediaCaptured = useCallback(
|
|
|
|
(media: PhotoFile | VideoFile) => {
|
|
|
|
console.log(`Media captured! ${JSON.stringify(media)}`)
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
requestPermission()
|
|
|
|
// Error handling in case they refuse to give permission
|
|
|
|
}
|
|
|
|
|
|
|
|
const device = useCameraDevice('back')
|
2024-01-30 15:08:46 -07:00
|
|
|
const format = useCameraFormat(device, [
|
|
|
|
{ videoResolution: { width: 3048, height: 2160 } },
|
|
|
|
{ fps: 60 }
|
|
|
|
])
|
2024-01-29 20:01:45 -07:00
|
|
|
|
|
|
|
if (device === null) {
|
|
|
|
return <Text>Camera not available. Does user have permissions: {hasPermission}</Text>
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
hasPermission && (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Camera
|
|
|
|
ref={camera}
|
|
|
|
style={StyleSheet.absoluteFill}
|
|
|
|
device={device}
|
2024-01-30 15:08:46 -07:00
|
|
|
format={format}
|
2024-01-29 20:01:45 -07:00
|
|
|
onInitialized={onInitialized}
|
2024-01-30 15:08:46 -07:00
|
|
|
onError={onError}
|
2024-01-29 20:01:45 -07:00
|
|
|
video={true}
|
2024-01-30 13:55:52 -07:00
|
|
|
photo={false}
|
2024-01-30 15:08:46 -07:00
|
|
|
orientation='portrait' // TODO: #60
|
|
|
|
isActive={isActive}
|
2024-01-29 20:01:45 -07:00
|
|
|
/>
|
|
|
|
<RecordingButton
|
|
|
|
style={styles.captureButton}
|
|
|
|
camera={camera}
|
|
|
|
onMediaCaptured={onMediaCaptured}
|
|
|
|
enabled={isCameraInitialized}
|
|
|
|
/>
|
|
|
|
</View>
|
2024-01-31 16:55:33 -07:00
|
|
|
|
2024-01-29 20:01:45 -07:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: 'black',
|
|
|
|
},
|
|
|
|
captureButton: {
|
|
|
|
position: 'absolute',
|
|
|
|
alignSelf: 'center',
|
2024-01-30 15:08:46 -07:00
|
|
|
bottom: 20, // Should come from SafeAreaProvider
|
2024-01-29 20:01:45 -07:00
|
|
|
},
|
|
|
|
})
|