Create a gist now

Instantly share code, notes, and snippets.

Embed
Take a picture with getUserMedia
<html>
<body>
<video id="v" width="300" height="300"></video>
<input id="b" type="button" disabled="true" value="Take Picture"></input>
<canvas id="c" style="display:none;" width="300" height="300"></canvas>
</body>
<script>
navigator.getUserMedia({video: true}, function(stream) {
var video = document.getElementById("v");
var canvas = document.getElementById("c");
var button = document.getElementById("b");
video.src = stream;
button.disabled = false;
button.onclick = function() {
canvas.getContext("2d").drawImage(video, 0, 0, 300, 300, 0, 0, 300, 300);
var img = canvas.toDataURL("image/png");
alert("done");
};
}, function(err) { alert("there was an error " + err)});
</script>
</html>
@Jackbennett

This comment has been minimized.

Show comment
Hide comment
@Jackbennett

Jackbennett Jan 9, 2017

Commenting this old gist as it's a good google rank. Line 13 needs to be the following in order the stream the video preview.

      video.srcObject = stream;
      video.play();

Commenting this old gist as it's a good google rank. Line 13 needs to be the following in order the stream the video preview.

      video.srcObject = stream;
      video.play();
@TobiasSeitz

This comment has been minimized.

Show comment
Hide comment
@TobiasSeitz

TobiasSeitz Jan 29, 2017

I ran into some problems when I tried this gist on Chrome > v55:

  1. The navigator.getUserMedia() method is deprecated in favor of navigator.mediaDevices.getUserMedia() (https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia, no more prefixes!).
  2. As noted by @Jackbennett, video.src = stream fails and should be replaced with a valid URL, that we can generate like this:
video.src = window.URL.createObjectURL(stream);
  1. I needed to dynamically create and resize the canvas to take the full picture of the webcam.

Here's a working example (tested on Chrome 55, Firefox 51):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML5 Media Device Access</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    video, img {
      max-width:100%;
    }
  </style>
</head>
<body>

<video autoplay></video>

<script>
  (function() {
    'use strict';
    var video = document.querySelector('video')
      , canvas;

    /**
     *  generates a still frame image from the stream in the <video>
     *  appends the image to the <body>
     */
    function takeSnapshot() {
      var img = document.querySelector('img') || document.createElement('img');
      var context;
      var width = video.offsetWidth
        , height = video.offsetHeight;

      canvas = canvas || document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;

      context = canvas.getContext('2d');
      context.drawImage(video, 0, 0, width, height);

      img.src = canvas.toDataURL('image/png');
      document.body.appendChild(img);
    }

    // use MediaDevices API
    // docs: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
    if (navigator.mediaDevices) {
      // access the web cam
      navigator.mediaDevices.getUserMedia({video: true})
      // permission granted:
        .then(function(stream) {
          video.src = window.URL.createObjectURL(stream);
          video.addEventListener('click', takeSnapshot);
        })
        // permission denied:
        .catch(function(error) {
          document.body.textContent = 'Could not access the camera. Error: ' + error.name;
        });
    }
  })();

</script>
</body>
</html>

I ran into some problems when I tried this gist on Chrome > v55:

  1. The navigator.getUserMedia() method is deprecated in favor of navigator.mediaDevices.getUserMedia() (https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia, no more prefixes!).
  2. As noted by @Jackbennett, video.src = stream fails and should be replaced with a valid URL, that we can generate like this:
video.src = window.URL.createObjectURL(stream);
  1. I needed to dynamically create and resize the canvas to take the full picture of the webcam.

Here's a working example (tested on Chrome 55, Firefox 51):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>HTML5 Media Device Access</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    video, img {
      max-width:100%;
    }
  </style>
</head>
<body>

<video autoplay></video>

<script>
  (function() {
    'use strict';
    var video = document.querySelector('video')
      , canvas;

    /**
     *  generates a still frame image from the stream in the <video>
     *  appends the image to the <body>
     */
    function takeSnapshot() {
      var img = document.querySelector('img') || document.createElement('img');
      var context;
      var width = video.offsetWidth
        , height = video.offsetHeight;

      canvas = canvas || document.createElement('canvas');
      canvas.width = width;
      canvas.height = height;

      context = canvas.getContext('2d');
      context.drawImage(video, 0, 0, width, height);

      img.src = canvas.toDataURL('image/png');
      document.body.appendChild(img);
    }

    // use MediaDevices API
    // docs: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
    if (navigator.mediaDevices) {
      // access the web cam
      navigator.mediaDevices.getUserMedia({video: true})
      // permission granted:
        .then(function(stream) {
          video.src = window.URL.createObjectURL(stream);
          video.addEventListener('click', takeSnapshot);
        })
        // permission denied:
        .catch(function(error) {
          document.body.textContent = 'Could not access the camera. Error: ' + error.name;
        });
    }
  })();

</script>
</body>
</html>
@jan-ivar

This comment has been minimized.

Show comment
Hide comment
@jan-ivar

jan-ivar Feb 19, 2017

@TobiasSeitz Please replace video.src = window.URL.createObjectURL(stream) with video.srcObject = stream, as createObjectURL with a stream as argument is being deprecated.

jan-ivar commented Feb 19, 2017

@TobiasSeitz Please replace video.src = window.URL.createObjectURL(stream) with video.srcObject = stream, as createObjectURL with a stream as argument is being deprecated.

@kikaGalhardo

This comment has been minimized.

Show comment
Hide comment
@kikaGalhardo

kikaGalhardo Oct 20, 2017

thank you so much help me a lot

thank you so much help me a lot

@Vuurvlieg

This comment has been minimized.

Show comment
Hide comment
@Vuurvlieg

Vuurvlieg Nov 18, 2017

@TobiasSeitz @jan-ivar Thank you so much, this will help me a lot!

@TobiasSeitz @jan-ivar Thank you so much, this will help me a lot!

@Magistern

This comment has been minimized.

Show comment
Hide comment
@Magistern

Magistern Apr 10, 2018

Do you know how to make this work on iOS 11.3?
I know Instagram.com makes it work somehow.

Do you know how to make this work on iOS 11.3?
I know Instagram.com makes it work somehow.

@kimpossible6

This comment has been minimized.

Show comment
Hide comment
@kimpossible6

kimpossible6 Apr 14, 2018

thanks so much!!

thanks so much!!

@barcenitas

This comment has been minimized.

Show comment
Hide comment
@barcenitas

barcenitas May 6, 2018

I love you

I love you

@TudorGruian

This comment has been minimized.

Show comment
Hide comment
@TudorGruian

TudorGruian May 15, 2018

Thank you so much.Really apreciate it

Thank you so much.Really apreciate it

@ubonsoft

This comment has been minimized.

Show comment
Hide comment
@ubonsoft

ubonsoft Jun 5, 2018

So GOOOOOOD

ubonsoft commented Jun 5, 2018

So GOOOOOOD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment