summaryrefslogtreecommitdiff
path: root/app/lib/camera/component/camera.tsx
blob: b398616fb143497b5e880570b7816b623a9945a0 (plain)
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
73
74
75
76
77
78
79
import { CameraSharp } from "@mui/icons-material";
import { Button, IconButton } from "@mui/material";
import Image from "next/image";
import React, { useRef } from "react";
import Webcam from "react-webcam";

interface WebcamCaptureProps {
  image: string | null;
  setImage: (image: string | null) => void;
  isWebcamVisible: boolean;
  setIsWebcamVisible: (isVisible: boolean) => void;
}

const WebcamCapture: React.FC<WebcamCaptureProps> = ({
  image,
  setImage,
  isWebcamVisible,
  setIsWebcamVisible,
}) => {
  const webcamRef = useRef<Webcam>(null);

  // Mengambil foto dari webcam
  const capture = () => {
    const image = webcamRef.current?.getScreenshot();
    setIsWebcamVisible(false);
    setImage(image || null);
  };

  const takePicture = () => {
    setImage(null);
    setIsWebcamVisible(true);
  };

  // Mengatur ukuran webcam
  const videoConstraints = {
    width: 500,
    height: 480,
    facingMode:  {
      exact: "environment" // untuk kamera belakang
    },
  };

  return (
    <div className="items-center">
      {!isWebcamVisible && (
        <Button variant="text" size="large" onClick={() => takePicture()}>
          Ambil Foto Surat Jalan
        </Button>
      )}
    
      {isWebcamVisible && (
        <div>
          <Webcam
            audio={false}
            ref={webcamRef}
            screenshotFormat="image/jpeg"
            videoConstraints={videoConstraints}
          />
          <IconButton aria-label="camera" size="large" onClick={() => capture()}>
            <CameraSharp fontSize="inherit" />
          </IconButton>
        </div>
      )}
      {image && (
        <div>
          <Image
            src={image}
            alt="Captured"
            width={500}
            height={480}
            unoptimized 
          />
        </div>
      )}
    </div>
  );
};

export default WebcamCapture;