import { useState, useRef, useEffect, useCallback } from 'react'; import { Camera, Check, RotateCcw, SwitchCamera } from 'lucide-react'; interface CameraCaptureProps { isOpen: boolean; onCapture: (base64Image: string) => void; onClose: () => void; } export default function CameraCapture({ isOpen, onCapture, onClose }: CameraCaptureProps) { const videoRef = useRef(null); const canvasRef = useRef(null); const overlayRef = useRef(null); const streamRef = useRef(null); const animFrameRef = useRef(0); const detectorRef = useRef(null); const [cameraReady, setCameraReady] = useState(false); const [cameraError, setCameraError] = useState(false); const [captured, setCaptured] = useState(null); const [hasFaceDetector, setHasFaceDetector] = useState(false); const [facingMode, setFacingMode] = useState<'user' | 'environment'>('user'); const startCamera = useCallback(async () => { try { stopCamera(); const stream = await navigator.mediaDevices.getUserMedia({ video: { width: 640, height: 480, facingMode }, }); streamRef.current = stream; if (videoRef.current) videoRef.current.srcObject = stream; setCameraReady(true); setCameraError(false); } catch { setCameraError(true); } }, [facingMode]); const toggleFacing = useCallback(() => { setFacingMode((m) => (m === 'user' ? 'environment' : 'user')); }, []); const stopCamera = useCallback(() => { cancelAnimationFrame(animFrameRef.current); if (streamRef.current) { streamRef.current.getTracks().forEach((t) => t.stop()); streamRef.current = null; } setCameraReady(false); }, []); const drawGuide = useCallback(() => { const overlay = overlayRef.current; const video = videoRef.current; if (!overlay || !video) return; overlay.width = video.videoWidth || 640; overlay.height = video.videoHeight || 480; const ctx = overlay.getContext('2d'); if (!ctx) return; ctx.clearRect(0, 0, overlay.width, overlay.height); const w = overlay.width * 0.5; const h = overlay.height * 0.55; const x = (overlay.width - w) / 2; const y = (overlay.height - h) / 2 - overlay.height * 0.05; ctx.strokeStyle = '#22c55e'; ctx.lineWidth = 3; ctx.setLineDash([8, 6]); ctx.beginPath(); ctx.roundRect(x, y, w, h, 12); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = 'rgba(34, 197, 94, 0.6)'; ctx.font = '13px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('Coloque su rostro aquí', overlay.width / 2, overlay.height - 20); }, []); const startFaceDetection = useCallback(async () => { if (!('FaceDetector' in window)) { drawGuide(); return; } setHasFaceDetector(true); try { detectorRef.current = new (window as any).FaceDetector({ maxDetectedFaces: 3, fastMode: true, }); } catch { drawGuide(); return; } const detect = async () => { const video = videoRef.current; const overlay = overlayRef.current; if (!video || !overlay || !detectorRef.current) { animFrameRef.current = requestAnimationFrame(detect); return; } try { const faces = await detectorRef.current.detect(video); const ctx = overlay.getContext('2d'); if (!ctx) return; overlay.width = video.videoWidth || 640; overlay.height = video.videoHeight || 480; ctx.clearRect(0, 0, overlay.width, overlay.height); if (faces.length > 0) { for (const face of faces) { const b = face.boundingBox; ctx.strokeStyle = '#22c55e'; ctx.lineWidth = 3; ctx.setLineDash([]); ctx.beginPath(); ctx.roundRect(b.x, b.y, b.width, b.height, 8); ctx.stroke(); } } else { drawGuide(); } } catch { drawGuide(); } animFrameRef.current = requestAnimationFrame(detect); }; animFrameRef.current = requestAnimationFrame(detect); }, [drawGuide]); useEffect(() => { if (!isOpen) return; startCamera(); return () => { stopCamera(); detectorRef.current = null; }; }, [isOpen, startCamera, stopCamera]); useEffect(() => { if (cameraReady) { const t = setTimeout(() => startFaceDetection(), 300); return () => clearTimeout(t); } }, [cameraReady, startFaceDetection]); const handleCapture = () => { if (!videoRef.current || !canvasRef.current) return; const video = videoRef.current; const canvas = canvasRef.current; canvas.width = video.videoWidth; canvas.height = video.videoHeight; const ctx = canvas.getContext('2d'); if (!ctx) return; ctx.drawImage(video, 0, 0); const base64 = canvas.toDataURL('image/jpeg', 0.8); setCaptured(base64); stopCamera(); }; const handleAccept = () => { if (captured) onCapture(captured); }; const handleRetry = () => { setCaptured(null); startCamera(); }; const handleClose = () => { setCaptured(null); stopCamera(); onClose(); }; if (!isOpen) return null; return (
e.stopPropagation()}>

Capturar Rostro

{cameraError ? (

No se pudo acceder a la cámara

) : captured ? (
Rostro capturado
) : (
)}
); }