xref: /expo/packages/expo-camera/src/utils/props.ts (revision 8285c032)
1import { Platform } from 'expo-modules-core';
2
3import {
4  CameraNativeProps,
5  CameraType,
6  FlashMode,
7  AutoFocus,
8  WhiteBalance,
9  CameraProps,
10} from '../Camera.types';
11import CameraManager from '../ExponentCameraManager';
12
13// Values under keys from this object will be transformed to native options
14export const ConversionTables: {
15  type: Record<keyof typeof CameraType, CameraNativeProps['type']>;
16  flashMode: Record<keyof typeof FlashMode, CameraNativeProps['flashMode']>;
17  autoFocus: Record<keyof typeof AutoFocus, CameraNativeProps['autoFocus']>;
18  whiteBalance: Record<keyof typeof WhiteBalance, CameraNativeProps['whiteBalance']>;
19} = {
20  type: CameraManager.Type,
21  flashMode: CameraManager.FlashMode,
22  autoFocus: CameraManager.AutoFocus,
23  whiteBalance: CameraManager.WhiteBalance,
24};
25
26export function convertNativeProps(props?: CameraProps): CameraNativeProps {
27  if (!props || typeof props !== 'object') {
28    return {};
29  }
30
31  const nativeProps: CameraNativeProps = {};
32
33  for (const [key, value] of Object.entries(props)) {
34    if (typeof value === 'string' && ConversionTables[key]) {
35      nativeProps[key] = ConversionTables[key][value];
36    } else {
37      nativeProps[key] = value;
38    }
39  }
40
41  return nativeProps;
42}
43
44export function ensureNativeProps(props?: CameraProps): CameraNativeProps {
45  const newProps = convertNativeProps(props);
46
47  if (newProps.onBarCodeScanned) {
48    newProps.barCodeScannerEnabled = true;
49  }
50
51  if (newProps.onFacesDetected) {
52    newProps.faceDetectorEnabled = true;
53  }
54
55  if (Platform.OS !== 'android') {
56    delete newProps.ratio;
57    delete newProps.useCamera2Api;
58  }
59
60  if (Platform.OS !== 'web') {
61    delete newProps.poster;
62  }
63
64  return newProps;
65}
66