我正在使用导航器。媒体设备。getUserMedia,从web浏览器中的摄像头设备打开MediaStream。我的应用程序想在WebAssembly中进行一些实时图像处理,为此,我需要直接从摄像机提供实时图像流。
我的解决方案在大多数设备上都运行得很好,但是,我在具有多个后置摄像头的设备上遇到了一个问题,例如GoogleChromeAndroid上的三星Galaxy S10。问题是以下片段:
const constraints = {
audio: false,
video: {
width: { min: 640, ideal: 1280, max: 1920 },
height: { min: 480, ideal: 720, max: 1080 },
facingMode: { ideal: 'environment' },
}
};
const stream = await navigator.mediaDevices.getUserMedia( constraints );
总是打开错误的相机——广角相机不支持自动对焦,提供的图像对我的代码来说太失真了。广角相机适合风景摄影,但对条形码和文本扫描来说很糟糕。
如何使用MediaTrackConstraint选择正确的相机?我也尝试过添加
focusMode: { ideal: 'continuous' }
对于约束(根据MDN文档,这应该是图像轨迹的一个可能约束),但它似乎不起作用。
我也尝试列举了所有设备(从这个SO答案),但我不知道如何正确选择正确的相机。
值得注意的是,以下代码段:
const devices = await navigator.mediaDevices.enumerateDevices();
devices.forEach( ( device: MediaDeviceInfo ) => {
console.log( "Found device: " + JSON.stringify( device ) );
});
生成以下控制台输出:
Found device: {"deviceId":"default","kind":"audioinput","label":"","groupId":"4852f187ff6a41e6d3fb3ba41c4897f46bd8ff153579da6fcb8f485432a32f66"}
Found device: {"deviceId":"86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d","kind":"audioinput","label":"","groupId":"c2cfc78763f7668263b0033c44d0f906ca0f33264ebfa6b96e9846265a21ff09"}
Found device: {"deviceId":"4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c","kind":"audioinput","label":"","groupId":"7a86866423279d7b1e12dbe585a14a677a2f2df4e41ec5d388b6c90f7319e88d"}
Found device: {"deviceId":"b46cd34041256d2cf72ed6e8500f71beb698a01b8c47c7c04801c20c47630978","kind":"videoinput","label":"camera2 1, facing front","groupId":"b1bd1a6ed8a87cd07ca0fa84744ae515b1ab2bed61cc257765c37d3426269af7"}
Found device: {"deviceId":"39d63e8a9764261b73785c90beb58399997a5a4de56b3238fff6676c738331a6","kind":"videoinput","label":"camera2 3, facing front","groupId":"a23c2f0e311c0ca0c80a56a8b5ff7c1f8aa093df4f6ac080b051c1d95f60a94e"}
Found device: {"deviceId":"4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c","kind":"videoinput","label":"camera2 2, facing back","groupId":"9b6b1a429e0db2d5094ddebe205d23309464650d8bcd585b2fe4ae8196b86f1c"}
Found device: {"deviceId":"86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d","kind":"videoinput","label":"camera2 0, facing back","groupId":"0de9556e1253763d7203b3b9e5db313cf89e05dd4bdd4ea0c5aff52d2952cf11"}
Found device: {"deviceId":"default","kind":"audiooutput","label":"","groupId":"default"}
正确的摄像机具有标签“camera2 0,面朝后”,出于某种原因,Chrome始终选择“camera2 2,面朝后”。
当然,我可以根据标签对相机的选择进行硬编码,但这只适用于三星Galaxy S10,我希望我的代码适用于任何具有多个背面摄像头的设备。
我还没有尝试在iPhone 11 Pro上运行我的页面(它有3个背面摄像头),但它在华为Mate 30 Pro(4个背面摄像头)和Oppo Reno 9上运行正常。此外,这个问题似乎与Android上的谷歌浏览器有关。当我在Android版Firefox上打开页面时,浏览器会要求我在关闭摄像头权限对话框后选择应该使用的摄像头,并且只有在多个摄像头满足给定约束的情况下。这很公平,因为它可以选择正确的摄像机来执行扫描。我还没有尝试在三星互联网和Opera浏览器上打开我的页面。
由于谷歌Chrome是Android系统上最受欢迎的网络浏览器,我甚至会对Chrome特定的解决方案感到满意,但当然,最好的答案是无处不在。
在jib的评论之后,我还尝试使用focusDistance
focusDistance: { min: 0.05, ideal: 0.12, max: 0.3 }
但这没用。
我还尝试使用以下片段记录getSetings
和getCapables
的输出:
const devices = await navigator.mediaDevices.enumerateDevices();
let videoDevices: Array< MediaDeviceInfo > = [];
devices.forEach( ( device: MediaDeviceInfo ) => {
if ( device.kind == 'videoinput' ) {
console.log( "Found video device: " + JSON.stringify( device ) );
videoDevices.push( device );
}
});
console.log( '' );
// open every video device and dump its characteristics
for ( let i in videoDevices ) {
const device = videoDevices[ i ];
console.log( "Opening video device " + device.deviceId + " (" + device.label + ")" );
const stream = await navigator.mediaDevices.getUserMedia( { video: { deviceId: { exact: device.deviceId } } } );
stream.getVideoTracks().forEach( track => {
const capabilities = track.getCapabilities();
console.log( "Track capabilities: " + JSON.stringify( capabilities ) );
const settings = track.getSettings();
console.log( "Track settings: " + JSON.stringify( settings ) );
console.log( '' );
}
)
stream.getTracks().forEach( track => track.stop() );
}
输出如下:
Found video device: {"deviceId":"b46cd34041256d2cf72ed6e8500f71beb698a01b8c47c7c04801c20c47630978","kind":"videoinput","label":"camera2 1, facing front","groupId":"500dd57c6795399100a5ca8bf7f0cc4d7ed8b1bcb0877101d1bef7eb74921868"}
Found video device: {"deviceId":"39d63e8a9764261b73785c90beb58399997a5a4de56b3238fff6676c738331a6","kind":"videoinput","label":"camera2 3, facing front","groupId":"245693c8d34be77fe2f15be31b6054a19edb8ea9ed4116d966d2a03695bebebe"}
Found video device: {"deviceId":"4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c","kind":"videoinput","label":"camera2 2, facing back","groupId":"c219595df2c2a430aea7007f64e6ce8fbfa783b038cf53069336361cc07e71af"}
Found video device: {"deviceId":"86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d","kind":"videoinput","label":"camera2 0, facing back","groupId":"96a68b6d6429786317e3b2c4773082604c5ab9a5cdaaf49c481878e40d67e987"}
Opening video device b46cd34041256d2cf72ed6e8500f71beb698a01b8c47c7c04801c20c47630978 (camera2 1, facing front)
Track capabilities: {"aspectRatio":{"max":3216,"min":0.0004528985507246377},"deviceId":"b46cd34041256d2cf72ed6e8500f71beb698a01b8c47c7c04801c20c47630978","facingMode":["user"],"frameRate":{"max":30,"min":0},"groupId":"500dd57c6795399100a5ca8bf7f0cc4d7ed8b1bcb0877101d1bef7eb74921868","height":{"max":2208,"min":1},"resizeMode":["none","crop-and-scale"],"width":{"max":3216,"min":1}}
Track settings: {"aspectRatio":1.3333333333333333,"deviceId":"b46cd34041256d2cf72ed6e8500f71beb698a01b8c47c7c04801c20c47630978","facingMode":"user","frameRate":30,"groupId":"500dd57c6795399100a5ca8bf7f0cc4d7ed8b1bcb0877101d1bef7eb74921868","height":480,"resizeMode":"none","width":640}
Opening video device 39d63e8a9764261b73785c90beb58399997a5a4de56b3238fff6676c738331a6 (camera2 3, facing front)
Track capabilities: {"aspectRatio":{"max":3968,"min":0.0003654970760233918},"deviceId":"39d63e8a9764261b73785c90beb58399997a5a4de56b3238fff6676c738331a6","facingMode":["user"],"frameRate":{"max":30,"min":0},"groupId":"245693c8d34be77fe2f15be31b6054a19edb8ea9ed4116d966d2a03695bebebe","height":{"max":2736,"min":1},"resizeMode":["none","crop-and-scale"],"width":{"max":3968,"min":1}}
Track settings: {"aspectRatio":1.3333333333333333,"deviceId":"39d63e8a9764261b73785c90beb58399997a5a4de56b3238fff6676c738331a6","facingMode":"user","frameRate":30,"groupId":"245693c8d34be77fe2f15be31b6054a19edb8ea9ed4116d966d2a03695bebebe","height":480,"resizeMode":"none","width":640}
Opening video device 4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c (camera2 2, facing back)
Track capabilities: {"aspectRatio":{"max":4608,"min":0.00028935185185185184},"deviceId":"4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c","facingMode":["environment"],"frameRate":{"max":60,"min":0},"groupId":"c219595df2c2a430aea7007f64e6ce8fbfa783b038cf53069336361cc07e71af","height":{"max":3456,"min":1},"resizeMode":["none","crop-and-scale"],"width":{"max":4608,"min":1}}
Track settings: {"aspectRatio":1.3333333333333333,"deviceId":"4d5fecf5a3eee5d41812bb6c34efe6d25342af9448628b006561c7385a22ca6c","facingMode":"environment","frameRate":60,"groupId":"c219595df2c2a430aea7007f64e6ce8fbfa783b038cf53069336361cc07e71af","height":480,"resizeMode":"none","width":640}
Opening video device 86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d (camera2 0, facing back)
Track capabilities: {"aspectRatio":{"max":4032,"min":0.00033068783068783067},"deviceId":"86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d","facingMode":["environment"],"frameRate":{"max":60,"min":0},"groupId":"96a68b6d6429786317e3b2c4773082604c5ab9a5cdaaf49c481878e40d67e987","height":{"max":3024,"min":1},"resizeMode":["none","crop-and-scale"],"width":{"max":4032,"min":1}}
Track settings: {"aspectRatio":1.3333333333333333,"deviceId":"86d4706b0bf160ff12fa75535173edcc68d4fa7ad5e00ec186cb1285ff22869d","facingMode":"environment","frameRate":60,"groupId":"96a68b6d6429786317e3b2c4773082604c5ab9a5cdaaf49c481878e40d67e987","height":480,"resizeMode":"none","width":640}
因此,除了不同的最大可用分辨率外,对于背向的摄像机2 0和背向的摄像机2 2,仍然没有任何区别。
我也尝试过三星互联网浏览器,它的行为与谷歌Chrome相同。
还有其他想法吗(除了遍历所有后置摄像头并选择分辨率最低的摄像头)?
我把所有的摄像机按id排列成这样
js lang-js prettyprint-override"> navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
for(;devices[i];){
if(devices[i].kind == "videoinput"){
that.aCameras.push( [devices[i].deviceId , devices[i].label] )
j++;
}
i++;
}
});
由于这个问题在今天仍然存在,目前检测“非长焦”/“非宽镜头”相机的最佳方法是检查火炬参数。
(因为这是字面上唯一的参数,在某些设备上与“标准”摄像机和其他设备不同。)
我正在这样做:
facingMode:{理想:'环境'},
,找出是否存在torch
torch
焦点距离
。我在这个问题上遇到了几个问题。我正在尝试选择运行Chrome的Android设备上的后置摄像头。 因此,在阅读之后: 然后,因为我用了三个。在这个应用程序中,我将这个ID绑定到Jerome Etienne三分机网络摄像头抓取(https://github.com/jeromeetienne/threex.webar): 然后我不得不修改3X。网络摄像头抓取以这种方式分类(我删除了不相关的部分):
问题内容: 我注意到有不同的bean作用域,例如: 每个的目的是什么?如何为我的bean选择合适的范围? 问题答案: 介绍 它表示bean的范围(生存期)。如果您熟悉基本Servlet Web应用程序的“幕后”工作,这将更容易理解:Servlet如何工作?实例化,会话,共享变量和多线程。 @Request/View/Flow/Session/ApplicationScoped 一个的生存时间只有一
将 package.json 中的 Ionic 版本改为 2.0.0 的时候,我就思考一个问题。这个该死的问题是——我到底要用哪个框架继续工作下去。 刚开始学习前端的时候,SPA(单页面应用)还没有现在这么流行,可以选择的框架也很少。而今天,我随便打开一个技术相关的网站、应用,只需要简单的看几页,就可以看到丰富的前端框架世界 Angular 2、React、Vue.js、Ember.js。 当我还
过去,我一直无法相信:一个新人在三个月里可以学好前端。后来,我信了。因为三个月后,我又是一个前端的新人,我又需要重新入门前端。 前端领域好似也有一个“摩尔定律”。戈登·摩尔提出来:积体电路上可容纳的电晶体(晶体管)数目,约每隔24个月便会增加一倍,后来经常被引用的“18个月”。而对于前端领域来说,每隔 3-6 个月,知识点将增加一倍。 过去一年(即 2016 年)的每三个月(或者半年)里,前端领域
问题内容: 我在以下div中有一个背景图片,但是该图片被截断了: 有没有一种方法可以显示背景图像而不将其剪切掉? 问题答案: 您可以使用])属性来实现此目的,大多数浏览器现在都支持该属性。 缩放背景图像以适合div: 要缩放背景图像以覆盖整个div:
我是硒IDE的新手。据我所知,当打开Selenium IDE时,您会注意到红色的‘记录宏’按钮被切换。这意味着selenium将尝试记录您在浏览器内所做的每一个操作。这是一种有问题的记录方式,因为我们在继续之前隐式地等待操作完成。 如果我只让Selenium记录每一个动作而不指定额外的动作,那么许多测试步骤将会失败,错误消息是:Element not found。我试图添加基于Selenium A