Created
May 24, 2018 08:04
-
-
Save softberries/4ae169f9c5090f93fe189c67a9fddbf6 to your computer and use it in GitHub Desktop.
open the camera in a browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------------- | |
// GET USER MEDIA CODE | |
//-------------------- | |
navigator.getUserMedia = ( navigator.getUserMedia || | |
navigator.webkitGetUserMedia || | |
navigator.mozGetUserMedia || | |
navigator.msGetUserMedia); | |
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() { | |
webcamStream.stop(); | |
} | |
//--------------------- | |
// TAKE A SNAPSHOT CODE | |
//--------------------- | |
var canvas, ctx; | |
function init() { | |
// Get the canvas and obtain a context for | |
// drawing in it | |
canvas = document.getElementById("myCanvas"); | |
ctx = canvas.getContext('2d'); | |
startWebcam(); | |
} | |
function snapshot() { | |
// Draws current image from the video element into the canvas | |
ctx.drawImage(video, 0,0, canvas.width, canvas.height); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment