指示getUserMedia使用最佳的可用摄像头分辨率 [英] Instruct getUserMedia to use best available camera resolution

查看:284
本文介绍了指示getUserMedia使用最佳的可用摄像头分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用getUserMedia函数来录制来自网络摄像头的视频.一切正常,除了我只是指定视频时才以640x480分辨率录制:约束条件为true.

I am using the getUserMedia function to record video from a webcam. Everyting works fine, except that it was only recording in 640x480 resolution, when I just specified video: true for the constraint.

如果我将约束设置如下,现在可以在笔记本电脑上获得更好的录制质量:

If I set the constraint as below, I now get better quality recording on my laptop:

var mediaConstraints = {
    audio: true,
    video: {
        width: { min: 1280 },
        height: { min: 720 }
    }
};

但是,如果另一台设备的性能优于1280x720或更低,该怎么办呢?

But what if another device is capable of better than 1280x720, or has even lower capabilities?

我尝试将min设置为非常低,max设置为非常高;但它默认设置为640x480,或完全不提供任何内容.我也尝试设置所需的大小,但是正如预期的那样-我最终得到的是那个值.如果我将期望值设置得很高(因此它将选择最接近的可用值),那么我一无所有.

I tried playing around with setting min to be very low, and max to be very high; but it just defaulted to 640x480, or gave nothing at all. I also tried setting the desired size, but as expected - I just ended up with whatever that value was. If I set desired to be very high (so it would choose the closest available), I ended up with nothing.

问题是,如何设置约束以获取相机可以提供的最大分辨率?

So the question is, how to set the constraints to get whatever the maximum resolution a camera can provide?

推荐答案

请注意, min 的意思是最小允许值",并且由于大多数浏览器默认为640x480,因此低于此值将不会产生太大影响

Note that min means "minimum allowed value", and since most browsers default to 640x480, values lower than that wont have much effect.

根据规格,只要求一个高分辨率,您就应该获得相机可以产生的最接近的分辨率.例如:

According to the spec, just ask for a high resolution, and you should get the closest resolution the camera can produce. E.g.:

var mediaConstraints = {
    audio: true,
    video: { width: 2880, height: 1800 }
};

这可以在Firefox中使用,并且也可以在Edge中使用(我没有尝试过).

This works in Firefox, and may work in Edge too (which I haven't tried).

请参见此其他答案,以详细了解其工作原理.

See this other answer for more on why this works.

Chrome(尽管他们正在研究它)仍依赖 adapter.js polyfill不幸的是,标准约束的语法不正确,并且该polyfill有一些局限性,例如您在此处遇到的局限性.

Chrome (though they are working on it) still relies on the adapter.js polyfill for the standard constraints syntax unfortunately, and that polyfill has some limitations, like the one you are running into here.

如果您使用的是 adapter.js ,则可以在Chrome的网络控制台中查看它本机使用的旧的非标准语法.使用该语法,可以构造一系列依次更宽的最小范围,并使该算法更喜欢较高的值.

If you are using adapter.js, you can look in the web console in Chrome to see the old non-standard syntax that it uses natively. Using that syntax it is possible to construct a series of successively broader min-ranges, and getting the algorithm to prefer higher values that way.

这篇关于指示getUserMedia使用最佳的可用摄像头分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆