简体   繁体   English

如何在iOS上使用getUserMedia更改尺寸?

[英]How to change dimensions with getUserMedia on iOS?

Since Apple's iOS 11 webRTC and getUserMedia introduction, I'm able to get the camera's input to the <video> element, but can't use conventional methods to set its dimensions ( height and width ). 自Apple的iOS 11 webRTCgetUserMedia介绍以来,我能够将相机的输入提供给<video>元素,但不能使用传统方法来设置其尺寸( heightwidth )。

This works: 这有效:

var constraints = {
    audio: false,
    video: { facingMode: 'user' }
};

navigator.mediaDevices.getUserMedia(constraints).then(
    function success(stream) {
         video.srcObject = stream;
    }
);

This doesn't work ( only on iOS), and no image is shown: 这不起作用( 在iOS上),并且不显示图像:

var constraints = {
    audio: false,
    video: { facingMode: 'user', width: 306, height: 180 }
};

navigator.mediaDevices.getUserMedia(constraints).then(
    function success(stream) {
         video.srcObject = stream;
    }
);

The error message from catch : 来自catch的错误消息:

Invalid constraint 约束无效


Failed attempts: setting mandatory , ideal , minHeight etc. 尝试失败:设置mandatoryidealminHeight等。


Is this a bug? 这是一个错误吗? Are there any known alternatives? 有没有已知的替代品?

This looks like a bug in Safari. 这看起来像Safari中的一个错误。 Bare values like width: 306 are supposed to mean ideal , but Safari appears to treat them like exact , and fails with an error unless it exactly matches a driver resolution offered by the user's camera. width: 306这样的裸值应该是ideal ,但Safari似乎将它们视为exact一样,并且失败并出现错误,除非它与用户相机提供的驱动程序分辨率完全匹配。

Except for that, Safari appears to work like other browsers like Firefox and Edge, where the goal is to find best-match native resolutions offered by the camera. 除此之外,Safari似乎像其他浏览器一样工作,如Firefox和Edge,其目标是找到相机提供的最佳匹配原生分辨率。

So stick to ranges until Safari fixes this. 所以坚持到范围,直到Safari修复此问题。

navigator.mediaDevices.getUserMedia({video: {width: {min: 320, max: 640}}})

...or use common values with a fallback strategy: ...或使用回退策略的常用值:

try {
  await navigator.mediaDevices.getUserMedia({video: {width: 320}})
} catch(e) {
  await navigator.mediaDevices.getUserMedia({video: {width: 640}})
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM