import { useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Download } from "lucide-react"; export default function WebcamImageSaver() { const videoRef = useRef(null); const canvasRef = useRef(null); const [imageUrl, setImageUrl] = useState(""); const startCamera = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ video: true }); if (videoRef.current) { videoRef.current.srcObject = stream; } } catch (error) { console.error("Error accessing webcam:", error); } }; const captureImage = () => { if (!videoRef.current || !canvasRef.current) return; const context = canvasRef.current.getContext("2d"); context.drawImage(videoRef.current, 0, 0, 640, 480); const imageDataUrl = canvasRef.current.toDataURL("image/png"); setImageUrl(imageDataUrl); }; const saveImage = () => { if (!imageUrl) return; const link = document.createElement("a"); link.href = imageUrl; link.download = "webcam_image.png"; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; return (