skip to Main Content

I want to Automatically take a picture from a webcam source, as soon as webcam access is granted from the browser, then save it to files, with no user input.

I have got the picture capturing sorted but i cannot:

A) Get it to automatically take the picture

B) Save the picture

The idea is that then there will be input for user email, then picture file will be renamed to their email.

This is what I have got so far:

<html>
<head>
  
</head>
<body>



<video id="player" controls autoplay></video>
<button id="capture">Capture</button>
<canvas id="canvas" width="320" height="240"></canvas>
<script>

  const player = document.getElementById('player');
  const canvas = document.getElementById('canvas');
  const context = canvas.getContext('2d');
  const captureButton = document.getElementById('capture');

  const constraints = {
    video: true,
  };

  captureButton.addEventListener('click', () => {
    // Draw the video frame to the canvas.
    context.drawImage(player, 0, 0, canvas.width, canvas.height);
  });

  // Attach the video stream to the video element and autoplay.
  navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
    player.srcObject = stream;
  });
</script>

</html>

So i just need to:
A) Get it to automatically take the picture
B) Save the picture

2

Answers


  1. You can call an object’s event listener just like a normal function.

    In your case you’d just call captureButton.onclick() in your script when you want the picture to be taken.

    For downloading files, I’d recommend using download.js (https://github.com/rndme/download). You can download the source code but you can also get it through some CDNs if you prefer. As far as I know, download.js only downloads to the "downloads" directory (at least on Windows, not sure about Apple)

    Login or Signup to reply.
  2. According to what you’ve written, there might be some ethical concerns for actually taking a picture without the user’s consent, even if the user allows access to the camera, that doesn’t mean you can take a picture of it immediately.

    There might be other issues, for instance, even if the camera is immediately available, doesn’t mean that the first image stream has a good picture frame, meaning you might want to wait sometime until the frame is ready.

    Anyway, I’ve added another button so you can take as many pictures as you want, until you get one that satisfies you.
    The first photo is always automatic, but you can override it by clicking in the Take another Photo Button.
    Here is a full working code:

    <video id="player" controls autoplay></video>
    <canvas id="canvas" width="640" height="480" ></canvas>
    
    <!-- Input for user email -->
    <input type="text" id="userEmail" placeholder="Enter your email">
    <button onclick="captureImage()">Take another Photo</button>
    <button onclick="downloadImage()">Download</button>
    
    <script>
        const player = document.getElementById('player');
        const canvas = document.getElementById('canvas');
        const context = canvas.getContext('2d');
        const userEmailInput = document.getElementById('userEmail');
    
        const constraints = {
            video: true,
        };
    
        function captureImage() {
            context.drawImage(player, 0, 0, canvas.width, canvas.height);
        }
    
        function downloadImage() {
            const email = userEmailInput.value;
            if (!email) {
                alert('Please enter your email first.');
                return;
            }
    
            const link = document.createElement('a');
            canvas.toBlob(function(blob) {
                link.href = URL.createObjectURL(blob);
                link.download = email + '.png'; // naming the downloaded file with email
                link.click();
            }, 'image/png');
        }
    
        navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
            player.srcObject = stream;
            // Once the webcam stream is loaded, capture the image
            player.onloadedmetadata = function(e) {
                captureImage();
            };
        });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search