我正在将网络摄像头数据流传送到video
元素,然后将其绘制到canvas
元素上。然后,我在画布上运行人脸检测算法,在脸上绘制正方形。调用context.getImgData()会导致画布“闪烁”
问题是,要做到这一点,我需要调用context.getImgData()
方法。这会导致画布在调用方法时“闪烁”。它瞬间完全变黑,然后恢复正常。它看起来很可怕。我跟着其他一些用网络摄像头动画绘制画布的人的例子,他们也使用这种方法,所以我真的不知道解决它的一种方法。
我在JSfiddle下面有一些示例代码。我在使用Firefox的Mac OS上。有两个按钮。第一个将video
元素的流描绘为canvas
,第二个简单地运行getImgData()
。你可以清楚地看到我描述的问题。有什么想法吗?
<body>
<div>
<button onclick="paintCanvas()">paint</button>
<button onclick="testFunc()">test</button>
</div>
<div>
<canvas id="canvas" width="500" height="375"></canvas>
<video autoplay loop="false" src="media/vid.mp4" type="video/mp4" id="videoElement">
</div>
</body>
<script>
var video = document.querySelector("#videoElement");
var myCanvas = document.getElementById('canvas');
var myContext = myCanvas.getContext('2d');
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
video.srcObject = stream;
// vid2.srcObject = stream;
// document.getElementById('controls').innerHTML = "Switch back to video for player controls";
// document.getElementById('timing').innerHTML = '';
})
.catch(function (err) {
media = 'video';
console.log(err.name);
});
let ch, cw;
function paintCanvas(e) {
console.log('painting canvas')
const v = document.getElementById('videoElement');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
cw = Math.floor(canvas.clientWidth);
ch = Math.floor(canvas.clientHeight);
canvas.width = cw;
canvas.height = ch;
draw(v, context, cw, ch);
}
function draw(v, c, w, h) {
// console.log('drawing')
videoRunning = true;
if (v.paused || v.ended) return false;
c.drawImage(v, 0, 0, w, h);
myVar = setTimeout(draw, 60, v, c, w, h);
}
function testFunc() {
// setInterval(function() {
console.log(video.videoWidth, video.videoHeight)
x = myContext.getImageData(0, 0, video.videoWidth, video.videoHeight);
console.log(x);
// }, 200);
}
</script>
我无法复制。你测试了哪些浏览器和操作系统? – Watilin
对不起应该澄清 - mac os和firefox –