這是一項新技術。 Yoy必須有Firefox/Chrome/Opera瀏覽器並且必須更新。那麼,試試這個:
function showCamera() { var streaming = false,
video = window.content.document.createElement("video"),
cover = window.content.document.createElement("div"),
canvas = window.content.document.createElement("canvas"),
photo = window.content.document.createElement("img"),
startbutton = window.content.document.createElement("button"),
width = 320,
height = 0;
photo.src = "http://placekitten.com/g/320/261"; window.content.document.body.insertBefore(video, window.content.document.body.firstChild);
var navigator = window.navigator;
navigator.getMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia(
{
video: true,
audio: false
},
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL.createObjectURL(stream);
}
video.play();
},
function(err) {
console.log("An error occured! " + err);
} );
video.addEventListener('canplay', function(ev){
if (!streaming) {
height = video.videoHeight/(video.videoWidth/width);
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
streaming = true;
} }, false);
function takepicture() {
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').drawImage(video, 0, 0, width, height);
var data = canvas.toDataURL('image/png');
photo.setAttribute('src', data); }
startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault(); }, false); }
showCamera();
如果你的瀏覽器是Firefox和仍然沒有工作,去about:config中,並設置/添加一個布爾變量與稱得上是真正的價值media.navigator.enabled
來源: https://developer.mozilla.org/en-US/docs/WebRTC/Taking_webcam_photos
P/D:我在Greasemonkey腳本中使用了這段代碼,它工作正常。我在原始代碼的第一行做了一些改變。
您是否還修改了對'getUserMedia()'的調用以回退到瀏覽器特定版本?如果您將所嘗試的確切代碼粘貼到您的問題中,您將獲得更多幫助。 – robertc
看看編輯,這是給我的錯誤 –