import React, { useEffect, useRef } from "react"; interface VideoWrapperProps { width?: string | number; height?: string | number; className?: string; handleNextStep: () => void; // Function to handle the next step setImage: (imageDataUrl: string) => void; // Function to set the image data URL } // VideoWrapper component const VideoWrapper: React.FC = ({ width, height, className, handleNextStep, setImage, }) => { const videoRef = useRef(null); const canvasRef = useRef(null); useEffect(() => { const startCamera = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ video: true, }); if (videoRef.current) { videoRef.current.srcObject = stream; } } catch (error) { console.error("Erreur lors de l'accès à la caméra :", error); } }; startCamera(); return () => { if (videoRef.current) { const stream = videoRef.current.srcObject as MediaStream; if (stream) { const tracks = stream.getTracks(); tracks.forEach((track) => track.stop()); } } }; }, []); const captureImage = async () => { const canvas = canvasRef.current; const video = videoRef.current; if (canvas && video) { const context = canvas.getContext("2d"); if (context) { canvas.width = video.videoWidth; // Width of the video canvas.height = video.videoHeight; // Height of the video context.drawImage(video, 0, 0, canvas.width, canvas.height); // Draw the video on the canvas return canvas.toDataURL("image/png"); // Return the image as a Data URL } } }; return ( <>
); }; export default VideoWrapper;