To take a photo using the ImageCapture API, you call the takePhoto() method on an ImageCapture instance, which returns a Promise that resolves with a Blob containing the image data . The source shows that you first need to create an ImageCapture object from a video MediaStreamTrack, typically obtained by calling navigator.mediaDevices.getUserMedia() .
A practical example from the MDN documentation demonstrates this process:
let imageCapture;
// 1. Get the video track from the media stream
navigator.mediaDevices
.getUserMedia({ video: true })
.then((mediaStream) => {
document.querySelector("video").srcObject = mediaStream;
const track = mediaStream.getVideoTracks()[0];
imageCapture = new ImageCapture(track);
})
.catch((error) => console.error(error));
// 2. Take the photo and handle the resulting Blob
function takePhoto() {
imageCapture
.takePhoto()
.then((blob) => {
console.log("Took photo:", blob);
// For example, display it in an <img> element
img.src = URL.createObjectURL(blob);
})
.catch((error) => {
console.error("takePhoto() error: ", error);
});
}The takePhoto() method also accepts an optional photoSettings object to configure the capture. The available options are:
fillLightMode: Sets the flash to"auto","off", or"flash".imageHeight: The desired image height as an integer .imageWidth: The desired image width as an integer .redEyeReduction: A boolean indicating whether to use red-eye reduction if available .
This information is drawn from the "Instance methods," "Examples," and "Parameters" sections of the MDN Web Docs page for the ImageCapture API, specifically referencing the takePhoto() and getPhotoSettings() documentation.