WebCam Recording
by Bacher
HTML
<video id="videoEl" width="320" height="240"></video>
JavaScript
function setWebCam(video, doneFn) {
function _detectVideoSignal() {
var canvas = document.createElement('canvas'),
ctx, res = false;
try {
ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, 1, 1);
res = ctx.getImageData(0, 0, 1, 1).data[4] != 255;
} catch (e) {}
return res;
}
var navigator = window.navigator;
var getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
getMedia.call(navigator, {
video: true
}, function (stream) {
var pid;
var URL = window.URL || window.webkitURL || window.mozURL;
pid = setInterval(function () {
if (_detectVideoSignal()) {
clearTimeout(pid);
doneFn();
}
}, 100);
video.src = URL.createObjectURL(stream);
video.play();
}, function () {});
}
setWebCam(videoEl, function () {
document.body.style.backgroundColor = 'red';
});