All files / components camera-view.tsx

100% Statements 2/2
100% Branches 4/4
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                                    6x                                                                                                             1x                                                                                                                              
import { StyleSheet, TouchableOpacity, View } from 'react-native';
import { CameraView } from 'expo-camera';
 
import { LottieAnimation } from '@/src/components/LottieAnimation';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import type { CameraFacing } from '@/src/types/camera';
 
interface CameraViewComponentProps {
  cameraRef: React.RefObject<CameraView | null>;
  facing: CameraFacing;
  onCapture: () => void;
  onToggleFacing: () => void;
  onClose?: () => void;
  onCameraReady?: () => void;
  onPickFromLibrary?: () => void;
}
 
export function CameraViewComponent({
  cameraRef,
  facing,
  onCapture,
  onToggleFacing,
  onClose,
  onCameraReady,
  onPickFromLibrary,
}: CameraViewComponentProps) {
  return (
    <View style={styles.container}>
      <LottieAnimation
        source={require('@/assets/animations/orange_cat_peeping.json')} // json文件路径
        width={200}
        height={200}
      />
      <CameraView
        ref={cameraRef}
        style={styles.camera}
        facing={facing}
        onCameraReady={onCameraReady}
      >
        <View style={styles.topBar}>
          {onClose && (
            <TouchableOpacity style={styles.iconButton} onPress={onClose} activeOpacity={0.7}>
              <IconSymbol name="xmark" size={28} color="#fff" />
            </TouchableOpacity>
          )}
        </View>
 
        <View style={styles.bottomBar}>
          <TouchableOpacity
            style={styles.iconButton}
            // left button should open photo library when provided
            onPress={onPickFromLibrary ?? onToggleFacing}
            activeOpacity={0.7}
            testID="library-button"
          >
            <IconSymbol name="photo.fill.on.rectangle.fill" size={32} color="#fff" />
          </TouchableOpacity>
 
          <TouchableOpacity
            style={styles.captureButton}
            onPress={onCapture}
            activeOpacity={0.8}
            testID="capture-button"
          >
            <View style={styles.captureButtonInner} />
          </TouchableOpacity>
 
          <TouchableOpacity
            style={styles.iconButton}
            onPress={onToggleFacing}
            activeOpacity={0.7}
            testID="flip-button"
          >
            <IconSymbol name="arrow.triangle.2.circlepath.camera" size={32} color="#fff" />
          </TouchableOpacity>
        </View>
      </CameraView>
    </View>
  );
}
 
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
 
  camera: {
    flex: 0.7,
    backgroundColor: '#000',
    marginTop: '40%',
    width: '90%',
    height: '60%',
    marginLeft: 'auto',
    marginRight: 'auto',
    borderRadius: 50,
    borderWidth: 10,
    borderColor: '#FEBE98',
  },
 
  topBar: {
    flexDirection: 'row',
    justifyContent: 'flex-end',
    padding: '7%',
  },
 
  bottomBar: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    paddingHorizontal: '8%',
    paddingBottom: '7%',
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
  },
 
  iconButton: {
    width: 50,
    height: 50,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'rgba(0, 0, 0, 0.3)',
    borderRadius: 25,
  },
 
  captureButton: {
    width: 70,
    height: 70,
    borderRadius: 35,
    backgroundColor: '#fff',
    justifyContent: 'center',
    alignItems: 'center',
    borderWidth: 4,
    borderColor: 'rgba(255, 255, 255, 0.3)',
  },
 
  captureButtonInner: {
    width: 60,
    height: 60,
    borderRadius: 30,
    backgroundColor: '#fff',
  },
});