Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Get image directly from video stream as data url

How would I go directly from video capture to a data url in javascript? I want to display the image to the user as a resized version, but keep the full size image available. So, how would I do this?

var PhotoBooth = {
    onMediaStream: function(stream) {
        PhotoBooth.canvas = $('canvas')[0];
        PhotoBooth.context = PhotoBooth.canvas.getContext('2d');

        PhotoBooth.localVideo = $('video')[0];
        PhotoBooth.localVideo.src = window.URL.createObjectURL(stream);
    },
    noStream: function() {
        console.log('FAIL TO GET WEBCAM ACCESS');
    }
};

getUserMedia(
    {video: true},
    PhotoBooth.onMediaStream,
    PhotoBooth.noStream
);

This is how I am currently saving the image for upload:

PhotoBooth.context.drawImage(PhotoBooth.localVideo, 0, 0, 200, 150);
$('#preview').show();

Then I retrive the saved image like so:

var dataUrl = PhotoBooth.canvas.toDataURL();

I'd like to keep the canvas the same size it is by default, but keep the actual data. Basically, I want the canvas to show a re-sized version, but maintain the full size version.

like image 520
HSchmale Avatar asked Mar 06 '16 22:03

HSchmale


People also ask

How to to get image from stream in Javascript?

src = window. URL. createObjectURL(stream); }, noStream: function() { console. log('FAIL TO GET WEBCAM ACCESS'); } }; getUserMedia( {video: true}, PhotoBooth.

How do I find the URL of an image?

To convert image from an Html page tag to a data URI using javascript, you first need to create a canvas element, set its width and height equal to that of the image, draw the image on it and finally call the toDataURL method on it.

What is data URL in Javascript?

A Data URL is a URI scheme that provides a way to inline data in a document, and it's commonly used to embed images in HTML and CSS.


1 Answers

Here, canvas maintains the original 640x480 snapshot (use https fiddle for Chrome):

var start = () => navigator.mediaDevices.getUserMedia({ video: true })
  .then(stream => video.srcObject = stream)
  .catch(log);

var canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 480;

var snap = () => {
  canvas.getContext('2d').drawImage(video, 0, 0, 640, 480);
  preview.getContext('2d').drawImage(canvas, 0, 0, 160, 120);
}

var log = msg => div.innerHTML += "<br>" + msg;
<button onclick="start()">Start!</button>
<button onclick="snap()">Snap!</button><div id="div"></div>
<video id="video" width="160" height="120" autoplay></video>
<canvas id="preview" width="160" height="120"></canvas>
like image 113
jib Avatar answered Sep 21 '22 12:09

jib