On my web, I need users can upload photos. But to make sure that the images are not blurry, I'd like to automatically detect that the camera is auto-focused or find a way to help focus. How can I take a photo automatically when the camera focuses correctly? Any idea??
My code:
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
var options = {video: true};
var videoWidth, videoHeight;
var video = document.getElementById('my-webcam');
var canvascam = document.getElementById('canvas-cam');
var onFail = function(e) {
alert('Failed to get camera');
alert(e);
};
var onSuccess = function(stream) {
if(navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var url = window.URL || window.webkitURL;
video.src = url.createObjectURL(stream);
}
setTimeout(function(){
setInterval(updateCanvas,30);
//Take photo when camera is focused
},1000);
};
navigator.getUserMedia(options, onSuccess, onFail);
function updateCanvas(){
canvascam.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="A JavaScript Computer Vision Library">
<title>JSFeat - JavaScript Computer Vision Library. Detect Edges</title>
</head>
<body>
<video id="my-webcam" autoplay ></video>
<canvas id="canvas-cam" width="480px" height="640px"></canvas>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</body>
</html>