Created
July 5, 2016 09:42
-
-
Save rndme/a540f817df792653c962d53c1fc82709 to your computer and use it in GitHub Desktop.
This file contains 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
function getSnapshot(callback) { | |
"use strict"; | |
callback = callback || Boolean; | |
var canvas = document.createElement("canvas"), | |
ctx = canvas.getContext("2d"), | |
webkit = false, | |
moz = false, | |
v = null, | |
W = 800, | |
H = 600, | |
n = navigator, | |
dataBuffer = [], | |
err = Boolean, | |
v = document.createElement("video"); | |
canvas.height = v.height = H; | |
canvas.width = v.width = W; | |
var payload = { | |
video: { | |
facingMode: "environment", | |
width: W, | |
height: H, | |
framerate: 10, | |
}, | |
audio: false | |
}; | |
var meth = n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia; | |
meth.call(n, payload, success, err); | |
function dumpCanvas() { | |
var imgurl = canvas.toDataURL("image/jpeg", 0.5); | |
if(imgurl.length < 12000) { | |
return setTimeout(updateCanvas, 50); | |
} else { | |
updateCanvas.stop(); | |
} | |
callback(canvas.toDataURL("image/jpeg", 0.5)); | |
} | |
function updateCanvas() { | |
ctx.drawImage(v, 0, 0, W, H); | |
setTimeout(dumpCanvas, 25); | |
} | |
function success(stream) { | |
updateCanvas.stop = function() { | |
stream.getTracks()[0].stop(); | |
}; | |
v.src = URL.createObjectURL(stream); | |
setTimeout(updateCanvas, 89); | |
} | |
} | |
////////////////////////////////// | |
function getSnapshot(e){function f(){if(12e3>a.toDataURL("image/jpeg",.5).length)return setTimeout(c,50);c.stop(),e(a.toDataURL("image/jpeg",.5))}function c(){g.drawImage(b,0,0,800,600),setTimeout(f,25)}e=e||Boolean;var a=document.createElement("canvas"),g=a.getContext("2d"),b=null,d=navigator,h=Boolean,b=document.createElement("video");a.height=b.height=600,a.width=b.width=800,(d.getUserMedia||d.webkitGetUserMedia||d.mozGetUserMedia).call(d,{video:{facingMode:"environment",width:800,height:600,framerate:10},audio:!1},function(a){c.stop=function(){a.getTracks()[0].stop()},b.src=URL.createObjectURL(a),setTimeout(c,89)},h)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment