我正在研究“ mediaDevices
”以尝试使用 javascript 捕获网络摄像头。下面是我的代码。
'use strict';
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const snap = document.getElementById("snap");
const errorMsgElement = document.querySelector('span#errorMsg');
const constraints = {
audio: true,
video: {
width: 1280, height: 720
}
};
// Access webcam
async function init() {
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
handleSuccess(stream);
} catch (e) {
//console.error('navigator.getUserMedia error:', e);
errorMsgElement.innerHTML = e.toString();
}
}
// Success
function handleSuccess(stream) {
//console.log('getUserMedia() got stream:', stream);
window.stream = stream;
video.srcObject = stream;
}
// Load init
init();
我已经在 google chrome 中启用了网络摄像头设置,但我有 ' Cannot set property 'innerHTML' of null
' 代替。我是javascript新手,任何建议或指导将不胜感激,谢谢。