No title

 <!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Take Photo</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            text-align: center;

            background-color: #f4f4f4;

            margin: 0;

            padding: 0;

        }


        #camera, #canvas {

            display: none;

            margin-top: 20px;

            border-radius: 10px;

            overflow: hidden;

        }


        #take-photo-btn {

            display: inline-block;

            padding: 15px 30px;

            margin-top: 50px;

            background-color: #007bff;

            color: white;

            font-size: 1.2em;

            border: none;

            border-radius: 5px;

            cursor: pointer;

            transition: background-color 0.3s;

        }


        #take-photo-btn:hover {

            background-color: #0056b3;

        }


        #canvas {

            margin-top: 20px;

        }

    </style>

</head>

<body>

    <h1>Take a Photo</h1>


    <!-- Button to start the process and take a photo -->

    <button id="take-photo-btn">Take Photo</button>


    <!-- Video element for live camera view -->

    <video id="camera" width="640" height="480" autoplay></video>


    <!-- Canvas element to display the captured image -->

    <canvas id="canvas" width="640" height="480"></canvas>


    <script>

        document.getElementById('take-photo-btn').addEventListener('click', function() {

            // Get the video element and canvas

            const video = document.getElementById('camera');

            const canvas = document.getElementById('canvas');

            const context = canvas.getContext('2d');


            // Function to start the camera and capture a photo

            function startCameraAndTakePhoto() {

                navigator.mediaDevices.getUserMedia({ video: true })

                    .then(function(stream) {

                        video.srcObject = stream;

                        video.style.display = 'block';

                        

                        // Delay to ensure camera starts before capturing the photo

                        setTimeout(function() {

                            context.drawImage(video, 0, 0, canvas.width, canvas.height);

                            canvas.style.display = 'block'; // Show the captured image

                            video.style.display = 'none'; // Hide the video after capturing

                            stream.getTracks().forEach(track => track.stop()); // Stop the camera

                        }, 500);

                    })

                    .catch(function(err) {

                        console.error("Error accessing the camera: " + err);

                    });

            }


            // Call the function to start the camera and take a photo

            startCameraAndTakePhoto();

        });

    </script>

</body>

</html>


Post a Comment