The API with getUserMedia
is very useful to have a video stream inside the browser/inside a HTML5 page, see for example https://www.html5rocks.com/en/tutorials/getusermedia/intro/ or https://simpl.info/getusermedia/sources/.
From this API, how to trigger an autofocus of the (smartphone) camera when doing a tap in the video display?
This is indeed the usual behaviour in nearly all smartphone camera apps.
I haven't found anything about "focus" in the API documentation MediaDevices.getUserMedia()
For example, you may need to use the allow attribute on any <iframe> that uses getUserMedia(), and pages that use getUserMedia() will eventually need to supply the Feature-Policy header. The two permissions that apply to getUserMedia() are camera and microphone.
To use getUserMedia() in an installable app (for example, a Firefox OS app), you need to specify one or both of the following fields inside your manifest file: See permission: audio-capture and permission: video-capture for more information.
Be aware that this feature may cease to work at any time. The deprecated Navigator.getUserMedia () method prompts the user for permission to use up to one video input device (such as a camera or shared screen) and up to one audio input device (such as a microphone) as the source for a MediaStream .
getUserMedia () is a powerful feature which can only be used in secure contexts; in insecure contexts, navigator.mediaDevices is undefined, preventing access to getUserMedia (). A secure context is, in short, a page loaded using HTTPS or the file:/// URL scheme, or a page loaded from localhost.
This is impossible right now but it may be possible in the near future. You should take a look at MediaStream Image Capture Working Draft. This spec describes advance options for .applyConstraints
, initially introduced in Media Capture and Streams.
How it works according to spec? First, you need to get the capabilities by using .getCapabilities
. If the browser can manage focus, then you can set focusMode: "auto" | "manual"
and focusDistance: number
for current constraints.
Focus mode describes the focus setting of the capture device (e.g. auto or manual).
Focus distance is a numeric camera setting that controls the focus distance of the lens. The setting usually represents distance in meters to the optimal focus distance.
Unfortunately, you can't control the focus at the moment, but you can check the code that may work in the future.
Chrome issue about support for focusDistance | Chrome Platform Status | MediaCapture Image Implementation Status.
CodeSandbox Demo
navigator.mediaDevices
.getUserMedia({ video: true })
.then(gotMedia)
.catch(err => console.error("getUserMedia() failed: ", err));
function gotMedia(mediastream) {
const video = document.querySelector("video");
video.srcObject = mediastream;
const track = mediastream.getVideoTracks()[0];
const capabilities = track.getCapabilities();
// Check whether focus distance is supported or not.
if (!capabilities.focusDistance) {
return;
}
// Map focus distance to a slider element.
const input = document.querySelector('input[type="range"]');
input.min = capabilities.focusDistance.min;
input.max = capabilities.focusDistance.max;
input.step = capabilities.focusDistance.step;
input.value = track.getSettings().focusDistance;
input.oninput = function(event) {
track.applyConstraints({
advanced: [{
focusMode: "manual",
focusDistance: event.target.value
}]
});
};
input.hidden = false;
}
<video autoplay></video>
<input type="range" hidden />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With