Upgrade Example to RN 0.64 (#83)
* ReactLogger: Also log function * Run SwiftFormat & SwiftLint in example project * Upgrade to RN 0.64 1/2 * Update lockfiles * Upgrade a few packages * index.tsx -> index.js * Upgrade docusaurus * Fix line length violation * Update CameraView.swift * Update gradle plugin * Fix example to prefer higher res cameras * Remove unused log line * Update App.tsx
This commit is contained in:
45
example/src/hooks/useCameraDevice.ts
Normal file
45
example/src/hooks/useCameraDevice.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { Camera, CameraDevice, sortDevices } from 'react-native-vision-camera';
|
||||
|
||||
/**
|
||||
* A custom hook that's just like `useCameraDevices` from VisionCamera, but ignores `'telephoto-camera'` devices since those often have poor quality.
|
||||
*/
|
||||
export const useCameraDevice = (): {
|
||||
front: CameraDevice | undefined;
|
||||
back: CameraDevice | undefined;
|
||||
} => {
|
||||
const [backDevice, setBackDevice] = useState<CameraDevice>();
|
||||
const [frontDevice, setFrontDevice] = useState<CameraDevice>();
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
const loadDevice = async (): Promise<void> => {
|
||||
const devices = await Camera.getAvailableCameraDevices();
|
||||
if (!isMounted) return;
|
||||
|
||||
// use any device
|
||||
const filtered = devices.filter((d) => !d.devices.includes('telephoto-camera'));
|
||||
const sorted = filtered.sort(sortDevices);
|
||||
const back = sorted.find((d) => d.position === 'back');
|
||||
const front = sorted.find((d) => d.position === 'front');
|
||||
setBackDevice(back);
|
||||
setFrontDevice(front);
|
||||
|
||||
console.log(`Devices: ${sorted.map((d) => d.name).join(', ')}`);
|
||||
};
|
||||
loadDevice();
|
||||
|
||||
return () => {
|
||||
isMounted = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
back: backDevice,
|
||||
front: frontDevice,
|
||||
}),
|
||||
[backDevice, frontDevice],
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user