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

标签 javascript getusermedia navigator

我需要从 imageCapture.takePhoto() 更改照片的尺寸,但它不起作用。我需要将尺寸更改为 320 高 X 240 宽,但结果是 640 高 X 480 宽。

在这里您可以找到文档。 https://developer.mozilla.org/en-US/docs/Web/API/ImageCapture/takePhoto

navigator.getUserMedia(
{
 video:true,
 audio:true
},
function(localMediaStream) {
 const track = localMediaStream.getVideoTracks()[0];
 let imageCapture = new ImageCapture(track);
 imageCapture.takePhoto(
   {imageHeight:320,imageWidth:240}).then(function(blob) {
    //do somethingh with blob (photo)
 }).catch(function(error) {
  console.log(error);
 });
},
function(err) {
  console.log(err);
});

最佳答案

您似乎需要先检查 ImageCapture 的 PhotoCapability,然后才能使用 ImageCapture.takePhoto(photoSettings)photoSettings 参数.

为此,您需要调用 getPhotoCapabilities() ImageCapture 方法,然后检查设置为 imageHeightimageWidth 的范围。

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;
}

https://jsfiddle.net/bLrvyet5/

但很可能您所需的宽度和高度不在这些MediaSettingsRange中,因此您可能必须在 Canvas 上自行调整此图像的大小。

关于javascript - 如何更改 imageCapture.takePhoto() 中的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226802/

相关文章:

javascript - polymer - 应用程序存储 : Remove entry and update storage

javascript - 在 PhantomJS 和 Python 中伪造 Flash 插件信息

dart - Flutter:Offstage with Navigator inside not getting focus

javascript - 如何使用 JavaScript 将一个 div 的内容克隆到另一个 div 中?

javascript - 使用 jquery 或 javascript 解析 Json 对象

javascript - 为什么 getFloatFrequencyData 返回的值为负数?

javascript - 如何保存带有webkit-filter效果的html5视频元素?

javascript - WebRTC 减少录制视频的大小

python - Anaconda Navigator 应用程序未启动应用程序

javascript - Webpack 加载字体