How can I capture an image via the user's webcam using getUserMedia?

I want to make a program on the web which will capture an image via the user's webcam.

I am using the getUserMedia Web API. Here is my code, but it does not work. How can I change it to capture the webcam image?

<div id="container">
    <video autoplay="true" id="videoElement">



There is the JS:

var video = document.querySelector("#videoElement");

navigator.getUserMedia, elem = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;


if (navigator.getUserMedia) {
    navigator.getUserMedia({video: true}, handleVideo, videoError);

function handleVideo(stream) {
    video.src = window.URL.createObjectURL(stream);

function videoError(e) {
    // do something


You can use this working sample

 <!DOCTYPE html>
  <body onload="init();">
    <h1>Take a snapshot of the current video stream</h1>
   Click on the Start WebCam button.
    <button onclick="startWebcam();">Start WebCam</button>
    <button onclick="stopWebcam();">Stop WebCam</button> 
       <button onclick="snapshot();">Take Snapshot</button> 
    <video onclick="snapshot(this);" width=400 height=400 id="video" controls autoplay></video>

        Screenshots : <p>
      <canvas  id="myCanvas" width="400" height="350"></canvas>  
          navigator.getUserMedia = ( navigator.getUserMedia ||
                             navigator.webkitGetUserMedia ||
                             navigator.mozGetUserMedia ||

      var video;
      var webcamStream;

      function startWebcam() {
        if (navigator.getUserMedia) {
           navigator.getUserMedia (

              // constraints
                 video: true,
                 audio: false

              // successCallback
              function(localMediaStream) {
                  video = document.querySelector('video');
                 video.src = window.URL.createObjectURL(localMediaStream);
                 webcamStream = localMediaStream;

              // errorCallback
              function(err) {
                 console.log("The following error occured: " + err);
        } else {
           console.log("getUserMedia not supported");

      function stopWebcam() {
      var canvas, ctx;

      function init() {
        // Get the canvas and obtain a context for
        // drawing in it
        canvas = document.getElementById("myCanvas");
        ctx = canvas.getContext('2d');

      function snapshot() {
         // Draws current image from the video element into the canvas
        ctx.drawImage(video, 0,0, canvas.width, canvas.height);


There have been updates to the getUserMedia api that now expose takePhoto and grabFrame. The grabFramemethod is less exciting because it does what we have been doing all along and just returning the next video frame from the stream but takePhoto interupts the stream to use the cameras "highest available photographic camera resolution" to capture a compressed image blob. More details here: google devs

var stream, imageCapture; 

function getMediaStream()
    window.navigator.mediaDevices.getUserMedia({video: true})
        stream = mediaStream; 
        let mediaStreamTrack = mediaStream.getVideoTracks()[0];
        imageCapture = new ImageCapture(mediaStreamTrack);

function error(error)
    console.error('error:', error); 

function takePhoto(img)
    const img = img || document.querySelector('img');

    .then(blob => {
        let url = window.URL.createObjectURL(blob);
        img.src = url;

/* just call */ 

/* and when you want to capture an image */ 

getUserMedia is not supported by IE11, take a look on this link:

So if you are still wanted to use the core property of getUserMedia then you need to use polyfill. Take a look on this polyfilllink:

Need Your Help

LINQ Guid toString()

c# linq linq-to-entities

Hi this seems like it should work,

Input into database using PHP and Javascript

javascript php jquery

I am new to javascript and am trying to use the following code to input a value into the database using php and javascript.