react-native-vision-camera/src/hooks/useCameraDevices.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-02-20 09:05:02 -07:00
import { useEffect, useState } from 'react';
2021-02-22 01:43:47 -07:00
import type { CameraPosition } from 'src/CameraPosition';
2021-02-20 15:20:28 -07:00
import { sortDevices } from 'src/utils/FormatFilter';
2021-02-20 09:05:02 -07:00
import { Camera } from '../Camera';
import { CameraDevice, LogicalCameraDeviceType, parsePhysicalDeviceTypes, PhysicalCameraDeviceType } from '../CameraDevice';
2021-02-22 01:43:47 -07:00
type CameraDevices = {
[key in CameraPosition]: CameraDevice | undefined;
};
const DefaultCameraDevices: CameraDevices = {
back: undefined,
external: undefined,
front: undefined,
unspecified: undefined,
};
2021-02-20 15:20:28 -07:00
/**
* Gets the best available `CameraDevice`. Devices with more cameras are preferred.
*
2021-02-20 15:23:38 -07:00
* @returns The best matching `CameraDevice`.
2021-02-20 15:20:28 -07:00
* @throws `CameraRuntimeError` if no device was found.
* @example
2021-02-20 15:23:38 -07:00
* const device = useCameraDevice()
2021-02-20 15:20:28 -07:00
* // ...
* return <Camera device={device} />
*/
2021-02-22 01:43:47 -07:00
export function useCameraDevices(): CameraDevices;
2021-02-20 15:20:28 -07:00
/**
* Gets a `CameraDevice` for the requested device type.
*
2021-02-20 15:23:38 -07:00
* @returns A `CameraDevice` for the requested device type.
* @throws `CameraRuntimeError` if no device was found.
2021-02-20 15:20:28 -07:00
* @example
* const device = useCameraDevice('wide-angle-camera')
* // ...
* return <Camera device={device} />
*/
2021-02-22 01:43:47 -07:00
export function useCameraDevices(deviceType: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices;
2021-02-20 15:20:28 -07:00
2021-02-22 01:43:47 -07:00
export function useCameraDevices(deviceType?: PhysicalCameraDeviceType | LogicalCameraDeviceType): CameraDevices {
const [cameraDevices, setCameraDevices] = useState<CameraDevices>(DefaultCameraDevices);
2021-02-20 09:05:02 -07:00
useEffect(() => {
2021-02-20 15:20:28 -07:00
let isMounted = true;
2021-02-20 09:05:02 -07:00
const loadDevice = async (): Promise<void> => {
2021-02-22 01:43:47 -07:00
let devices = await Camera.getAvailableCameraDevices();
2021-02-20 15:20:28 -07:00
if (!isMounted) return;
2021-02-22 01:43:47 -07:00
devices = devices.sort(sortDevices);
if (deviceType != null) {
devices = devices.filter((d) => {
2021-02-20 15:20:28 -07:00
const parsedType = parsePhysicalDeviceTypes(d.devices);
return parsedType === deviceType;
});
}
2021-02-22 01:43:47 -07:00
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'),
});
2021-02-20 09:05:02 -07:00
};
loadDevice();
2021-02-20 15:20:28 -07:00
return () => {
isMounted = false;
};
2021-02-20 09:05:02 -07:00
}, [deviceType]);
2021-02-22 01:43:47 -07:00
return cameraDevices;
2021-02-20 15:20:28 -07:00
}