railbird-gql/component/video/camera.tsx

141 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-01-31 22:45:00 -07:00
import React, { useCallback, useRef, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import {
Camera,
useCameraPermission,
useCameraDevice,
useCameraFormat,
PhotoFile,
VideoFile,
CameraRuntimeError,
Orientation,
2024-02-01 00:58:23 -07:00
// @ts-ignore
2024-01-31 22:45:00 -07:00
} from "react-native-vision-camera";
import { RecordingButton } from "./capture-button";
import { useIsForeground } from "./is-foreground";
import { useIsFocused } from '@react-navigation/native'
export default function CameraScreen(): React.ReactElement {
2024-01-31 22:45:00 -07:00
const camera = useRef<Camera>(null);
const { hasPermission, requestPermission } = useCameraPermission();
const [isCameraInitialized, setIsCameraInitialized] = useState<boolean>(false);
const isForeground = useIsForeground();
const isFocused = useIsFocused();
const isActive = isForeground && isFocused;
const onError = useCallback((error: CameraRuntimeError) => {
2024-01-31 22:45:00 -07:00
console.error(error);
}, []);
const onInitialized = useCallback(() => {
2024-01-31 22:45:00 -07:00
console.log("Camera initialized!");
setIsCameraInitialized(true);
}, []);
2024-01-31 22:45:00 -07:00
const onMediaCaptured = useCallback((media: PhotoFile | VideoFile) => {
console.log(`Media captured! ${JSON.stringify(media)}`);
}, []);
if (!hasPermission) {
2024-01-31 22:45:00 -07:00
requestPermission();
// Error handling in case they refuse to give permission
}
2024-01-31 22:45:00 -07:00
const device = useCameraDevice("back");
const format = useCameraFormat(device, [
{ videoResolution: { width: 3048, height: 2160 } },
2024-01-31 22:45:00 -07:00
{ fps: 60 },
]); // this sets as a target
//Orientation detection
2024-01-31 22:45:00 -07:00
const [orientation, setOrientation] = useState<Orientation>("portrait");
const toggleOrientation = () => {
2024-01-31 22:45:00 -07:00
setOrientation(
(currentOrientation) =>
currentOrientation === "landscape-left" ? "portrait" : "landscape-left", // Can adjust this and the type to match what we want
);
};
if (device === null) {
console.log(device)
2024-01-31 22:45:00 -07:00
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}
format={format}
onInitialized={onInitialized}
onError={onError}
video={true}
orientation={orientation} // TODO: #60
isActive={isActive}
/>
<RecordingButton
2024-01-31 22:45:00 -07:00
style={[
styles.captureButton,
orientation === "portrait" ? styles.portrait : styles.landscape,
]}
camera={camera}
onMediaCaptured={onMediaCaptured}
enabled={isCameraInitialized}
/>
2024-01-31 22:45:00 -07:00
<View
style={[
styles.button,
orientation === "portrait"
? styles.togglePortrait
: styles.toggleLandscape,
]}
>
<Button
title="Toggle Orientation"
onPress={toggleOrientation}
color="#841584"
accessibilityLabel="Toggle camera orientation"
/>
</View>
</View>
)
2024-01-31 22:45:00 -07:00
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
2024-01-31 22:45:00 -07:00
backgroundColor: "black",
},
captureButton: {
2024-01-31 22:45:00 -07:00
position: "absolute",
alignSelf: "center",
},
button: {
2024-01-31 22:45:00 -07:00
position: "absolute",
alignSelf: "center",
},
togglePortrait: {
2024-01-30 18:05:43 -07:00
bottom: 110, // needs refined
},
toggleLandscape: {
2024-01-31 22:45:00 -07:00
transform: [{ rotate: "90deg" }],
bottom: "43%", // Should come from SafeAreaProvider, hardcoded right now, should roughly appear above the button
left: 50, // needs refined
},
portrait: {
2024-01-31 22:45:00 -07:00
bottom: 20, // needs refined
},
landscape: {
2024-01-31 22:45:00 -07:00
bottom: "40%", // Should come from SafeAreaProvider
left: 20, // needs refined
},
2024-01-31 22:45:00 -07:00
});