我需要从 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 方法,然后检查设置为 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;
}
https://jsfiddle.net/bLrvyet5/
但很可能您所需的宽度和高度不在这些MediaSettingsRange中,因此您可能必须在 Canvas 上自行调整此图像的大小。
关于javascript - 如何更改 imageCapture.takePhoto() 中的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226802/