淘先锋技术网

首页 1 2 3 4 5 6 7

基于火狐和谷歌浏览器的html5调用摄像头需要浏览器版本比较高,此代码基于两个浏览器都是40版本。手机版本谷歌浏览器在安卓4.0的33版本不支持,手机火狐浏览器支持,并且可以选择调用前后摄像头

调用代码

Snap Photo

var canvas = document.getElementById("canvas");

var context = canvas.getContext("2d");

var video = document.getElementById("video");

$(document).ready(function () {

videoObj = { "video": true };

var errBack = function (error) {

console.log("Video capture error: ", error.code);

};

if (navigator.getUserMedia) { // Standard

navigator.getUserMedia(videoObj, function (stream) {

video.src = stream;

video.play();

}, errBack);

} else if (navigator.webkitGetUserMedia) { // WebKit-prefixed

navigator.webkitGetUserMedia(videoObj, function (stream) {

video.src = window.webkitURL.createObjectURL(stream);

video.play();

}, errBack);

}

else if (navigator.mozGetUserMedia) { // Firefox-prefixed

navigator.mozGetUserMedia(videoObj, function (stream) {

video.src = window.URL.createObjectURL(stream);

video.play();

}, errBack);

};

$("#snap").on("click", function () {

context.drawImage(video, 0, 0, 640, 480);

});

});