I am trying to access the webcam stream using HTML5 and JavaScript, using the technique described in this blog: https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm
It is based on the navigator.getUserMedia method.
The thing is, it just kinda works. Sometimes it does, sometimes it doesn't. Even this example webpage also does not work anymore if I reload the page.
What can be missing, and how do I even debug this? What is a flawless way to access the webcam in the browser? I am not even worried about compatibility with other browsers, give me a sure way that only works in Chrome and I'll take it!
Is creating a java applet or something a better alternative??...
EDIT here is my code:
<html>
<head>
<meta charset="utf-8">
<title>Display Webcam Stream</title>
<style>
#container {
margin: 0px auto;
width: 640px;
height: 480px;
border: 10px #333 solid;
}
#videoElement {
width: 640px;
height: 480px;
background-color: #666;
}
</style>
</head>
<body>
<div>
<a onClick="snapshot()">snap!</a>
</div>
<br/>
<div id="container">
<div>
<video autoplay="true" id="videoElement"></video>
</div>
</div>
<div>
<img width=320 height=240 src="" id="saida">
</div>
<canvas style="display:none;"></canvas>
<script>
var video = document.querySelector('#videoElement');
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;
<!--video.addEventListener('click', snapshot, false);-->
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: false, video: true}, handleVideo, videoError);
}
function handleVideo(stream) {
video.src = window.URL.createObjectURL(stream);
localMediaStream = stream
}
function videoError(e) {
console.log('foi malz');
}
function snapshot() {
if (localMediaStream) {
canvas.width = video.clientWidth;
canvas.height = video.clientHeight;
ctx.drawImage(video, 0, 0);
document.querySelector('#saida').src = canvas.toDataURL('image/webp');
}
}
</script>
</body>
</html>
EDIT2: This is apparently some bug in Chrome, in my machine. I reported it, let's see what happens. Probably will never be fixed. It works perfectly in Firefox anyway.