This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The MediaStream Image Capture API is an API for capturing images or videos from a photographic device. In addition to capturing data, it also allows you to retrieve information about device capabilities such as image size, red-eye reduction and whether or not there is a flash and what they are currently set to. Conversely, the API allows the capabilities to be configured within the constraints what the device allows.
The process of retrieving an image or video stream happens as described below. The example code is adapted from Chrome's Image Capture examples.
First, get a reference to a device by calling MediaDevices.getUserMedia()
. The example below simply says give me whatever video device is available, though the getUserMedia()
method allows more specific capabilities to be requested. This method returns a Promise
that resolves with a MediaStream
object.
navigator.mediaDevices.getUserMedia({ video: true }) .then(mediaStream => { // Do something with the stream. })
Next, isolate the visual part of the media stream. Do this by calling MediaStream.getVideoTracks()
. This returns an array of MediaStreamTrack
objects. The code below assumes that the first item in the MediaStreamTrack
array is the one to use. You can use the properties of the MediaStreamTrack
objects to select the one you need.
const track = mediaStream.getVideoTracks()[0];
At this point, you might want to configure the device capabilities before capturing an image. You can do this by calling applyConstraints()
on the track object before doing anything else.
let zoom = document.querySelector('#zoom'); const capabilities = track.getCapabilities() // Check whether zoom is supported or not. if (!capabilities.zoom) { return; }track.applyConstraints({advanced : [{zoom: zoom.value}] });
Finally, pass the MediaStreamTrack
object to the ImageCapture.ImageCapture()
constructor. Though a MediaStream
holds several types of tracks and provides multiple methods for retrieving them, the ImageCapture constructor will throw a DOMException
of type NotSupportedError
if MediaStreamTrack.kind
is not "video"
.
let imageCapture = new ImageCapture(track);
ImageCapture
MediaStreamTrack
.PhotoCapabilities
PhotoCapabilities
object by calling ImageCapture.getPhotoCapabilities()
.Specification | Status | Comment |
---|---|---|
MediaStream Image Capture | Working Draft | Initial definition. |
ImageCapture
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 59 | ? | ? | ? | 46 | ? |
ImageCapture() constructor
|
59 | ? | ? | ? | 46 | ? |
getPhotoCapabilities
|
59 | ? | ? | ? | 46 | ? |
getPhotoSettings
|
61 | ? | ? | ? | 46 | ? |
grabFrame
|
59 | ? | ? | ? | 46 | ? |
takePhoto
|
60
|
? | ? | ? | 47
|
? |
track
|
59 | ? | ? | ? | 46 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 59 | 59 | ? | ? | 46 | ? | 7.0 |
ImageCapture() constructor
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
getPhotoCapabilities
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
getPhotoSettings
|
61 | 61 | ? | ? | 46 | ? | ? |
grabFrame
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
takePhoto
|
60
|
60
|
? | ? | 46 | ? | 7.0 |
track
|
59 | 59 | ? | ? | 46 | ? | 7.0 |
PhotoCapabilities
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 59 | ? | ? | ? | 46 | ? |
fillLightMode
|
59 | ? | ? | ? | 46 | ? |
imageHeight
|
59 | ? | ? | ? | 46 | ? |
imageWidth
|
59 | ? | ? | ? | 46 | ? |
redEyeReduction
|
59 | ? | ? | ? | 46 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 59 | 59 | ? | ? | 46 | ? | ? |
fillLightMode
|
59 | 59 | ? | ? | 46 | ? | ? |
imageHeight
|
59 | 59 | ? | ? | 46 | ? | ? |
imageWidth
|
59 | 59 | ? | ? | 46 | ? | ? |
redEyeReduction
|
59 | 59 | ? | ? | 46 | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Image_Capture_API