All files / hooks useExpoCamera.ts

38.88% Statements 35/90
32.14% Branches 9/28
90% Functions 18/20
33.33% Lines 27/81

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                        38x   38x                 38x 12x 12x 12x             38x 2x 2x 2x                         38x 2x             38x 2x               38x 1x             38x   5x 5x 3x         5x     38x 1x           38x 1x           38x                                                                                                                                                                                                                                                                                                       38x                          
import { useCallback, useEffect, useRef, useState } from 'react';
import { Dimensions } from 'react-native';
import { Camera, CameraView, PermissionStatus } from 'expo-camera';
import * as ImageManipulator from 'expo-image-manipulator';
 
import type { CameraState, ExpoBarcodeResult } from '@/src/types/camera';
import { ScanType } from '@/src/types/camera';
 
/**
 * Expo 相机功能的自定义 Hook
 */
export function useExpoCamera(initialScanType: ScanType = ScanType.BARCODE) {
  const cameraRef = useRef<CameraView>(null);
 
  const [cameraState, setCameraState] = useState<CameraState>({
    hasPermission: null,
    isReady: false,
    facing: 'back',
    scanType: initialScanType,
    scannedBarcode: null,
  });
 
  // 组件挂载时检查现有权限
  useEffect(() => {
    (async () => {
      const { status } = await Camera.getCameraPermissionsAsync();
      setCameraState((prev) => ({
        ...prev,
        hasPermission: status === PermissionStatus.GRANTED,
      }));
    })();
  }, []);
 
  const requestPermission = useCallback(async () => {
    try {
      const { status } = await Camera.requestCameraPermissionsAsync();
      setCameraState((prev) => ({
        ...prev,
        hasPermission: status === PermissionStatus.GRANTED,
      }));
    } catch (error) {
      console.error('请求相机权限失败:', error);
      setCameraState((prev) => ({
        ...prev,
        hasPermission: false,
      }));
    }
  }, []);
 
  const toggleFacing = useCallback(() => {
    setCameraState((prev) => ({
      ...prev,
      facing: prev.facing === 'back' ? 'front' : 'back',
    }));
  }, []);
 
  // 新增:切换扫描模式 (OCR <-> Barcode)
  const toggleScanType = useCallback(() => {
    setCameraState((prev) => ({
      ...prev,
      scanType: prev.scanType === ScanType.BARCODE ? ScanType.OCR : ScanType.BARCODE,
      scannedBarcode: null, // 切换模式时重置扫描结果
    }));
  }, []);
 
  // 设置具体的扫描模式
  const setScanType = useCallback((type: ScanType) => {
    setCameraState((prev) => ({
      ...prev,
      scanType: type,
      scannedBarcode: null,
    }));
  }, []);
 
  const handleBarCodeScanned = useCallback((result: ExpoBarcodeResult) => {
    // 如果已经在处理中,或者是OCR模式,忽略
    setCameraState((prev) => {
      if (prev.scannedBarcode || prev.scanType !== ScanType.BARCODE) return prev;
      return {
        ...prev,
        scannedBarcode: result.data,
      };
    });
    console.log('扫描结果:', result.data);
  }, []);
 
  const onCameraReady = useCallback(() => {
    setCameraState((prev) => ({
      ...prev,
      isReady: true,
    }));
  }, []);
 
  const resetBarcodeScan = useCallback(() => {
    setCameraState((prev) => ({
      ...prev,
      scannedBarcode: null,
    }));
  }, []);
 
  const takePicture = useCallback(
    async (options?: {
      quality?: number;
      cropToScanFrame?: boolean;
      zoom?: number;
      frameLayout?: { x: number; y: number; width: number; height: number };
    }) => {
      if (!cameraRef.current || !cameraState.isReady) {
        console.warn('相机未准备好');
        return null;
      }
 
      try {
        // 先拍摄完整照片
        const photo = await cameraRef.current.takePictureAsync({
          quality: options?.quality || 0.8,
          base64: false,
          skipProcessing: false,
        });
 
        // 如果需要裁剪到扫描框
        if (options?.cropToScanFrame && photo && options?.frameLayout) {
          console.log('\n========== 开始裁剪计算(企业级方案 v2)==========');
          console.log('📸 原始照片:', photo.width, 'x', photo.height);
 
          const frameLayout = options.frameLayout;
          console.log('🎯 扫描框位置(相对于相机视图):', {
            x: frameLayout.x.toFixed(0),
            y: frameLayout.y.toFixed(0),
            w: frameLayout.width.toFixed(0),
            h: frameLayout.height.toFixed(0),
          });
 
          const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
          console.log('📱 屏幕尺寸:', screenWidth, 'x', screenHeight);
 
          // 计算照片和屏幕的宽高比
          const photoAspectRatio = photo.width / photo.height;
          const screenAspectRatio = screenWidth / screenHeight;
 
          console.log('📐 宽高比:', {
            photo: photoAspectRatio.toFixed(3),
            screen: screenAspectRatio.toFixed(3),
          });
 
          // 相机使用 'cover' 模式,计算实际显示的照片区域
          let visiblePhotoWidth: number, visiblePhotoHeight: number;
          let photoOffsetX = 0,
            photoOffsetY = 0;
 
          if (photoAspectRatio > screenAspectRatio) {
            // 照片更宽,上下会被裁掉
            visiblePhotoHeight = photo.height;
            visiblePhotoWidth = photo.height * screenAspectRatio;
            photoOffsetX = (photo.width - visiblePhotoWidth) / 2;
          } else {
            // 照片更高,左右会被裁掉
            visiblePhotoWidth = photo.width;
            visiblePhotoHeight = photo.width / screenAspectRatio;
            photoOffsetY = (photo.height - visiblePhotoHeight) / 2;
          }
 
          console.log('👁️ 可见照片区域:', {
            width: visiblePhotoWidth.toFixed(0),
            height: visiblePhotoHeight.toFixed(0),
            offsetX: photoOffsetX.toFixed(0),
            offsetY: photoOffsetY.toFixed(0),
          });
 
          // **关键:扫描框位置已经是相对于相机视图的**
          // 现在我们需要将相机视图的坐标系映射到照片坐标系
          // 相机视图显示的就是 visiblePhoto 区域
          const scaleX = visiblePhotoWidth / screenWidth;
          const scaleY = visiblePhotoHeight / screenHeight;
 
          console.log('🔄 缩放比例:', {
            scaleX: scaleX.toFixed(3),
            scaleY: scaleY.toFixed(3),
          });
 
          // 将屏幕框坐标转换为照片坐标
          let photoFrameX = frameLayout.x * scaleX;
          let photoFrameY = frameLayout.y * scaleY;
          const photoFrameWidth = frameLayout.width * scaleX;
          const photoFrameHeight = frameLayout.height * scaleY;
 
          // 加上偏移量,得到完整照片中的坐标
          photoFrameX += photoOffsetX;
          photoFrameY += photoOffsetY;
 
          console.log('📷 照片框(照片坐标):', {
            x: photoFrameX.toFixed(0),
            y: photoFrameY.toFixed(0),
            w: photoFrameWidth.toFixed(0),
            h: photoFrameHeight.toFixed(0),
          });
 
          // 确保在照片范围内
          const cropX = Math.max(0, photoFrameX);
          const cropY = Math.max(0, photoFrameY);
          let cropWidth = photoFrameWidth;
          let cropHeight = photoFrameHeight;
 
          // 如果超出边界,调整宽高
          if (cropX + cropWidth > photo.width) {
            cropWidth = photo.width - cropX;
          }
          if (cropY + cropHeight > photo.height) {
            cropHeight = photo.height - cropY;
          }
 
          console.log('✂️ 最终裁剪区域:', {
            x: cropX.toFixed(0),
            y: cropY.toFixed(0),
            w: cropWidth.toFixed(0),
            h: cropHeight.toFixed(0),
          });
 
          const cropConfig: ImageManipulator.Action = {
            crop: {
              originX: Math.round(cropX),
              originY: Math.round(cropY),
              width: Math.round(cropWidth),
              height: Math.round(cropHeight),
            },
          };
 
          // 执行裁剪
          const croppedImage = await ImageManipulator.manipulateAsync(photo.uri, [cropConfig], {
            compress: options?.quality || 0.8,
            format: ImageManipulator.SaveFormat.JPEG,
          });
 
          console.log('✅ 裁剪完成:', croppedImage.width, 'x', croppedImage.height);
          console.log('========== 裁剪计算结束 ==========\n');
 
          return { ...photo, uri: croppedImage.uri };
        }
 
        return photo;
      } catch (error) {
        console.error('拍照失败:', error);
        return null;
      }
    },
    [cameraState.isReady, cameraState.scanType]
  );
 
  return {
    state: cameraState,
    cameraRef,
    takePicture,
    toggleFacing,
    toggleScanType, // 导出切换方法
    setScanType, // 导出设置方法
    onCameraReady,
    handleBarCodeScanned,
    requestPermission,
    resetBarcodeScan,
  };
}