/** * Verifica se webcam está disponível */ export async function validarWebcamDisponivel(): Promise { if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { return false; } try { const devices = await navigator.mediaDevices.enumerateDevices(); return devices.some((device) => device.kind === 'videoinput'); } catch { return false; } } /** * Captura imagem da webcam */ export async function capturarWebcam(): Promise { if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { return null; } let stream: MediaStream | null = null; try { // Solicitar acesso à webcam stream = await navigator.mediaDevices.getUserMedia({ video: { width: { ideal: 1280 }, height: { ideal: 720 }, facingMode: 'user', }, }); // Criar elemento de vídeo temporário const video = document.createElement('video'); video.srcObject = stream; video.play(); // Aguardar vídeo estar pronto await new Promise((resolve, reject) => { video.onloadedmetadata = () => { video.width = video.videoWidth; video.height = video.videoHeight; resolve(); }; video.onerror = reject; setTimeout(() => reject(new Error('Timeout ao carregar vídeo')), 5000); }); // Capturar frame const canvas = document.createElement('canvas'); canvas.width = video.videoWidth; canvas.height = video.videoHeight; const ctx = canvas.getContext('2d'); if (!ctx) { throw new Error('Não foi possível obter contexto do canvas'); } ctx.drawImage(video, 0, 0); // Converter para blob return await new Promise((resolve) => { canvas.toBlob( (blob) => { resolve(blob); }, 'image/jpeg', 0.9 ); }); } catch (error) { console.error('Erro ao capturar webcam:', error); return null; } finally { // Parar stream if (stream) { stream.getTracks().forEach((track) => track.stop()); } } } /** * Captura imagem da webcam com preview */ export async function capturarWebcamComPreview( videoElement: HTMLVideoElement, canvasElement: HTMLCanvasElement ): Promise { if (typeof navigator === 'undefined' || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { return null; } let stream: MediaStream | null = null; try { // Solicitar acesso à webcam stream = await navigator.mediaDevices.getUserMedia({ video: { width: { ideal: 1280 }, height: { ideal: 720 }, facingMode: 'user', }, }); videoElement.srcObject = stream; await videoElement.play(); // Aguardar vídeo estar pronto await new Promise((resolve, reject) => { videoElement.onloadedmetadata = () => { canvasElement.width = videoElement.videoWidth; canvasElement.height = videoElement.videoHeight; resolve(); }; videoElement.onerror = reject; setTimeout(() => reject(new Error('Timeout ao carregar vídeo')), 5000); }); // Capturar frame const ctx = canvasElement.getContext('2d'); if (!ctx) { throw new Error('Não foi possível obter contexto do canvas'); } ctx.drawImage(videoElement, 0, 0); // Converter para blob return await new Promise((resolve) => { canvasElement.toBlob( (blob) => { resolve(blob); }, 'image/jpeg', 0.9 ); }); } catch (error) { console.error('Erro ao capturar webcam:', error); return null; } finally { // Parar stream if (stream) { stream.getTracks().forEach((track) => track.stop()); } } }