Image Capture API
The takePhoto() method of the
ImageCapture interface takes a single exposure using the video capture
device sourcing a MediaStreamTrack and returns a Promise
that resolves with a Blob containing the data.
Syntax
takePhoto()
takePhoto(photoSettings)
Parameters
photoSettings- : An object that sets options for the photo to be taken. The available options are:
fillLightMode- : The flash setting of the capture device, one of
"auto","off", or"flash".
- : The flash setting of the capture device, one of
imageHeight- : The desired image height as an integer. The user agent selects the closest height value to this setting if it only supports discrete heights.
imageWidth- : The desired image width as an integer. The user agent selects the closest width value to this setting if it only supports discrete widths.
redEyeReduction- : A boolean indicating whether the red-eye reduction should be used if it is available.
- : An object that sets options for the photo to be taken. The available options are:
Return value
A Promise that resolves with a Blob.
Exceptions
InvalidStateErrorDOMException- : Thrown if
readyStateproperty of theMediaStreamTrackpassing in the constructor is notlive.
- : Thrown if
UnknownErrorDOMException- : Thrown if the operation can't complete for any reason.
Examples
This example is extracted from this Simple Image Capture demo. It shows how to use the Promise returned by
takePhoto() to copy the returned Blob to an
img element. For simplicity it does not show how to instantiate the
ImageCapture object.
let takePhotoButton = document.querySelector("button#takePhoto");
let canvas = document.querySelector("canvas");
takePhotoButton.onclick = takePhoto;
function takePhoto() {
imageCapture
.takePhoto()
.then((blob) => {
console.log("Took photo:", blob);
img.classList.remove("hidden");
img.src = URL.createObjectURL(blob);
})
.catch((error) => {
console.error("takePhoto() error: ", error);
});
}
Specifications
Browser compatibility
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
