import React, { useState, useRef } from 'react'; import { Animated, ImageURISource, SafeAreaView, StatusBar, StyleSheet, TouchableOpacity, View } from 'react-native'; import { Camera, CameraApi, CameraType, Orientation } from 'react-native-camera-kit'; import loc from '../loc'; import { Icon } from '@rneui/base'; interface CameraScreenProps { onCancelButtonPress: () => void; showImagePickerButton?: boolean; showFilePickerButton?: boolean; onImagePickerButtonPress?: () => void; onFilePickerButtonPress?: () => void; torchOnImage?: ImageURISource; torchOffImage?: ImageURISource; onReadCode?: (event: any) => void; cameraFlipImage?: ImageURISource; } const CameraScreen: React.FC = ({ onCancelButtonPress, showImagePickerButton, showFilePickerButton, onImagePickerButtonPress, onFilePickerButtonPress, torchOnImage, torchOffImage, onReadCode, cameraFlipImage, }) => { const cameraRef = useRef(null); const [torchMode, setTorchMode] = useState(false); const [cameraType, setCameraType] = useState(CameraType.Back); const [zoom, setZoom] = useState(); const [orientationAnim] = useState(new Animated.Value(3)); const onSwitchCameraPressed = () => { const direction = cameraType === CameraType.Back ? CameraType.Front : CameraType.Back; setCameraType(direction); setZoom(1); // When changing camera type, reset to default zoom for that camera }; const onSetTorch = () => { setTorchMode(!torchMode); }; // Counter-rotate the icons to indicate the actual orientation of the captured photo. // For this example, it'll behave incorrectly since UI orientation is allowed (and already-counter rotates the entire screen) // For real phone apps, lock your UI orientation using a library like 'react-native-orientation-locker' const rotateUi = true; const uiRotation = orientationAnim.interpolate({ inputRange: [1, 4], outputRange: ['180deg', '-90deg'], }); const uiRotationStyle = rotateUi ? { transform: [{ rotate: uiRotation }] } : undefined; function rotateUiTo(rotationValue: number) { Animated.timing(orientationAnim, { toValue: rotationValue, useNativeDriver: true, duration: 200, isInteraction: false, }).start(); } return ( ); }; export default CameraScreen; const styles = StyleSheet.create({ screen: { height: '100%', backgroundColor: '#000000', }, topButtons: { margin: 10, zIndex: 10, flexDirection: 'row', justifyContent: 'space-between', }, topButton: { backgroundColor: '#222', width: 44, height: 44, borderRadius: 22, justifyContent: 'center', alignItems: 'center', }, topButtonImg: { margin: 10, width: 24, height: 24, }, cameraContainer: { justifyContent: 'center', flex: 1, }, cameraPreview: { width: '100%', height: '100%', }, bottomButtons: { margin: 10, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, backTextStyle: { padding: 10, color: 'white', fontSize: 20, }, rightButtonsContainer: { flexDirection: 'row', alignItems: 'center', }, bottomButton: { backgroundColor: '#222', width: 44, height: 44, borderRadius: 22, justifyContent: 'center', alignItems: 'center', marginLeft: 10, }, spacing: { marginLeft: 20, }, });