diff --git a/README.md b/README.md index 3657b0d..d6ee2c2 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ npx pod-install ```tsx function App() { - const device = useCameraDevice('wide-angle-camera') + const devices = useCameraDevices('wide-angle-camera') + const device = devices.back return ( */ -export function useCameraDevice(): CameraDevice; +export function useCameraDevices(): CameraDevices; /** * Gets a `CameraDevice` for the requested device type. @@ -26,32 +36,31 @@ export function useCameraDevice(): CameraDevice; * // ... * return */ -export function useCameraDevice(deviceType: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevice | undefined; +export function useCameraDevices(deviceType: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices; -export function useCameraDevice(deviceType?: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevice | undefined { - const [device, setDevice] = useState(); +export function useCameraDevices(deviceType?: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices { + const [cameraDevices, setCameraDevices] = useState(DefaultCameraDevices); useEffect(() => { let isMounted = true; const loadDevice = async (): Promise => { - const devices = await Camera.getAvailableCameraDevices(); + let devices = await Camera.getAvailableCameraDevices(); if (!isMounted) return; - let bestMatch: CameraDevice | undefined; - if (deviceType == null) { - // use any device - const sorted = devices.sort(sortDevices); - bestMatch = sorted[0]; - } else { - // use specified device (type) - bestMatch = devices.find((d) => { + devices = devices.sort(sortDevices); + if (deviceType != null) { + devices = devices.filter((d) => { const parsedType = parsePhysicalDeviceTypes(d.devices); return parsedType === deviceType; }); } - if (bestMatch == null) throw new CameraRuntimeError('device/no-device', 'No Camera device was found!'); - setDevice(bestMatch); + setCameraDevices({ + back: devices.find((d) => d.position === 'back'), + external: devices.find((d) => d.position === 'external'), + front: devices.find((d) => d.position === 'front'), + unspecified: devices.find((d) => d.position === 'unspecified'), + }); }; loadDevice(); @@ -60,5 +69,5 @@ export function useCameraDevice(deviceType?: PhysicalCameraDeviceType | LogicalC }; }, [deviceType]); - return device; + return cameraDevices; } diff --git a/src/index.ts b/src/index.ts index 94e7b61..ae0a2e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,6 @@ export * from './Point'; export * from './Snapshot'; export * from './TemporaryFile'; export * from './VideoFile'; -export * from './hooks/useCameraDevice'; +export * from './hooks/useCameraDevices'; export * from './hooks/useCameraFormat'; export * from './utils/FormatFilter'; diff --git a/src/utils/FormatFilter.ts b/src/utils/FormatFilter.ts index 8ca0866..a34db1c 100644 --- a/src/utils/FormatFilter.ts +++ b/src/utils/FormatFilter.ts @@ -91,8 +91,8 @@ export const sortFormatsByResolution = (left: CameraDeviceFormat, right: CameraD let rightPoints = right.photoHeight * right.photoWidth; if (left.videoHeight != null && left.videoWidth != null && right.videoHeight != null && right.videoWidth != null) { - leftPoints += left.videoWidth * left.videoHeight ?? 0; - rightPoints += right.videoWidth * right.videoHeight ?? 0; + leftPoints += left.videoWidth * left.videoHeight; + rightPoints += right.videoWidth * right.videoHeight; } // "returns a negative value if left is better than one"