Javascript 如何更改imageCapture.takePhoto()中的大小?

Javascript 如何更改imageCapture.takePhoto()中的大小?,javascript,getusermedia,navigator,Javascript,Getusermedia,Navigator,我需要从imageCapture.takePhoto()更改照片的大小,但它不起作用。我需要将大小更改为320高x240宽,但结果是640高x480宽 在这里您可以找到文档 在使用ImageCapture.takePhoto(photoSettings)的photoSettings参数之前,似乎需要检查ImageCapture的光电容 为此,您将调用ImageCapture的方法,然后检查设置为imageHeight和imageWidth的范围 const capture = new Image

我需要从imageCapture.takePhoto()更改照片的大小,但它不起作用。我需要将大小更改为320高x240宽,但结果是640高x480宽

在这里您可以找到文档


在使用ImageCapture.takePhoto(photoSettings)的photoSettings参数之前,似乎需要检查ImageCapture的光电容

为此,您将调用ImageCapture的方法,然后检查设置为
imageHeight
imageWidth
的范围

const capture = new ImageCapture(track);
const { imageWidth, imageHeight } = await capture.getPhotoCapabilities();
const width = setInRange(required_width, imageWidth);
const height = setInRange(required_height, imageHeight);
const photoSettings = (width && height) ? {
  imageWidth: width,
  imageHeight: height
} : null;
const pic = await capture.takePhoto(photoSettings);

function setInRange(value, range) {
  if(!range) return NaN;
  let x = Math.min(range.max, Math.max(range.min, value));
  x = Math.round(x / range.step) * range.step; // take `step` into account
  return x;
}


但很可能您所需的宽度和高度不在这些介质设置范围内,因此您可能需要自己在画布上调整此图像的大小。

Ps:navigator.getUserMedia不推荐使用。使用navigator.mediaDevices.getUserMedia。谢谢,这非常重要。
const capture = new ImageCapture(track);
const { imageWidth, imageHeight } = await capture.getPhotoCapabilities();
const width = setInRange(required_width, imageWidth);
const height = setInRange(required_height, imageHeight);
const photoSettings = (width && height) ? {
  imageWidth: width,
  imageHeight: height
} : null;
const pic = await capture.takePhoto(photoSettings);

function setInRange(value, range) {
  if(!range) return NaN;
  let x = Math.min(range.max, Math.max(range.min, value));
  x = Math.round(x / range.step) * range.step; // take `step` into account
  return x;
}