-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebcam-capture.tsx
More file actions
72 lines (66 loc) · 2.08 KB
/
webcam-capture.tsx
File metadata and controls
72 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { CameraIcon, RotateCcwIcon, XIcon } from "lucide-react";
import { forwardRef, useState } from "react";
import Webcam from "react-webcam";
import { toast } from "sonner";
import { Button } from "../ui/button";
const WebcamCapture = forwardRef<
Webcam,
{ capturePhoto: () => void; onCancel: () => void }
>((props, ref) => {
const [videoConstraints, setVideoConstraints] = useState({
facingMode: "environment",
});
const rotateCamera = () => {
setVideoConstraints((prev) => ({
...prev,
facingMode: prev.facingMode === "environment" ? "user" : "environment",
}));
};
const handleCameraError = (error: string | DOMException) => {
console.error("Camera error:", error);
toast.error(
"Camera access failed. Please check permissions or use file upload instead."
);
props.onCancel();
};
return (
<div className="fixed inset-0 w-full h-full z-50 flex items-center justify-center bg-muted">
<Webcam
audio={false}
ref={ref}
screenshotFormat="image/jpeg"
className="border-2 border-gray-300 mx-auto w-full"
videoConstraints={videoConstraints}
onUserMediaError={handleCameraError}
/>
<div className="absolute bottom-2 gap-4 left-[50%] translate-x-[-50%] flex items-center justify-center">
<Button
type="button"
onClick={props.capturePhoto}
variant="outline"
className="w-10 h-10 p-2 rounded-full"
>
<CameraIcon className="w-full h-full" />
</Button>
<Button
type="button"
onClick={rotateCamera}
variant="outline"
className="w-10 h-10 p-2 rounded-full"
>
<RotateCcwIcon className="w-full h-full" />
</Button>
</div>
<Button
type="button"
onClick={props.onCancel}
variant="outline"
className="absolute top-2 right-2 w-10 h-10 p-2 rounded-full"
>
<XIcon className="w-full h-full" />
</Button>
</div>
);
});
WebcamCapture.displayName = "WebcamCapture";
export default WebcamCapture;