getUserMedia lock focus/exposure
I am using
navigator.getUserMedia with constraints to access the user’s webcam, using the feed as the source of an HTML
<video> and then copying its stream to
<canvas> context. I’m doing all this so I can take a snapshot at intervals.
What I would like to do is, once the page starts taking snapshots, lock the getUserMedia camera’s focus/exposure, so that in between snapshot intervals the environment can change without the light balance changing or the camera refocusing.
Does anyone know if this is possible on the JS side?
Flagged as duplicate of: Take photo when the camera is automatically focused
Firsly – though perhaps bad practice here – I will link to why MediaCapture might be blurry or grainy than expected:
Why the difference in native camera resolution -vs- getUserMedia on iPad / iOS?
In short: MediaCapture does a lot of transformations on the media source which may cause blury or grainy images.
To solve this use ImageCapture:
The ImageCapture API enables control over camera features such as
zoom, brightness, contrast, ISO and white balance. Best of all, Image
Capture allows you to access the full resolution capabilities of any
available device camera or webcam. Previous techniques for taking
photos on the Web have used video snapshots, which are lower
resolution than that available for still images.
To solve your problem:
You can solve this via UX and a zoom slider. Below is information on how to achieve this with ImageCapture (still images). MediaCapture (video feed) does not allow this functionality. You could use MediaCapture and have a button such as “Manual Mode” and allow a user to pick the correct zoom to take the photo.
You could also “emulate” a camera by having an update loop doing n ImageCaptures per update and have a zoom slider.
And here is an example on how to use it/polyfill: https://github.com/GoogleChromeLabs/imagecapture-polyfill
Makesure you use the latest getUserMedia polyfills which handle crossplatform support: https://www.npmjs.com/package/webrtc-adapter
Hope this helps
We had the same problem some time ago. It only happens on some devices… but even devices from the same brand had different behaviours. Something in the browser / OS version / driver combination were broken. In some devices focus was locked and in others didn’t. We look over all the API, we tried dozens of variations in the initialization code but finally we concluded there was no apparent solution.
We added a button to restart all in order to mitigate the problem somehow…