Download desktop/src/hooks/useCameraDevices.ts from chenbhao/codev: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/chenbhao/codev/resolve/e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/hooks/useCameraDevices.ts
- Command line
-
hf download hf://chenbhao/codev@e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/hooks/useCameraDevices.ts
-
curl -L -o useCameraDevices.ts https://huggingface.co/chenbhao/codev/resolve/e40073cc959cd4729bf4009662bca374c5c4e266/desktop/src/hooks/useCameraDevices.ts
1.73 kB
| import { useState, useCallback } from 'react' | |
| export function useCameraDevices() { | |
| const [devices, setDevices] = useState<MediaDeviceInfo[]>([]) | |
| const [activeDeviceId, setActiveDeviceId] = useState<string | null>(null) | |
| const enumerateCameras = useCallback(async () => { | |
| try { | |
| const allDevices = await navigator.mediaDevices.enumerateDevices() | |
| const videoInputs = allDevices.filter((d) => d.kind === 'videoinput') | |
| setDevices(videoInputs) | |
| return videoInputs | |
| } catch { | |
| return [] | |
| } | |
| }, []) | |
| const switchCamera = useCallback( | |
| async (deviceId: string): Promise<MediaStream | null> => { | |
| try { | |
| const stream = await navigator.mediaDevices.getUserMedia({ | |
| video: { | |
| deviceId: { exact: deviceId }, | |
| width: { ideal: 640 }, | |
| height: { ideal: 480 }, | |
| }, | |
| audio: false, | |
| }) | |
| setActiveDeviceId(deviceId) | |
| return stream | |
| } catch (err) { | |
| console.error('Failed to switch camera:', err) | |
| return null | |
| } | |
| }, | |
| [], | |
| ) | |
| const getStreamForFacingMode = useCallback( | |
| async (facingMode: 'user' | 'environment'): Promise<MediaStream | null> => { | |
| try { | |
| const stream = await navigator.mediaDevices.getUserMedia({ | |
| video: { | |
| facingMode, | |
| width: { ideal: 640 }, | |
| height: { ideal: 480 }, | |
| }, | |
| audio: false, | |
| }) | |
| return stream | |
| } catch (err) { | |
| console.error('Failed to get camera with facing mode:', err) | |
| return null | |
| } | |
| }, | |
| [], | |
| ) | |
| return { | |
| devices, | |
| activeDeviceId, | |
| enumerateCameras, | |
| switchCamera, | |
| getStreamForFacingMode, | |
| } | |
| } | |