如何在UWP中显示2个网络摄像头预览?(How to display 2 web camera preview in UWP?)

编程入门 行业动态 更新时间:2024-10-18 14:20:47
如何在UWP中显示2个网络摄像头预览?(How to display 2 web camera preview in UWP?)

您好我在UWP中显示了1个网络摄像头预览,这是成功的。

但是现在我想在我的程序中使用2个摄像头预览,或者能够在计算机上连接2个摄像头时在两个摄像头之间进行选择。

当我运行1个网络摄像头预览时,我提到了使用MediaCapture 文档 ,这很好。

但现在我不知道如何显示2个摄像头预览或在摄像头之间选择一个。

这不可能吗?

Hello I displayed 1 webcam preview in UWP and that was a success.

But now I want to use 2 camera's preview on my program or be able to choose between the two cameras while connected 2 cameras on computer.

When I run 1 webcam preview, I referred to documentation on using MediaCapture and it was good.

But now I don't know how to display 2 camera previews or select a one between cameras.

Is it impossible?

最满意答案

对的,这是可能的 :-) 。 当您调用不带参数的InitializeAsync方法时, MediaCapture类将使用默认摄像头,但还有另一个重载允许您指定设备ID

该文档显示了如何发现视频捕获设备:

DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

现在您可以像这样初始化多个MediaCapture实例:

foreach ( var device in devices ) { var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id }; MediaCapture mediaCapture = new MediaCapture(); mediaCapture.InitializeAsync(mediaInitSettings); //do something with the media capture }

当然,当您想要显示多个预览时,您需要拥有多个CaptureElements ,每个都设置为您想要的特定MediaCapture实例。

然而,这种方法非常简单。 要确保支持并发捕获和预览,您必须首先确保仅使用MediaCapture.IsVideoProfileSupported方法查询支持设备配置文件的摄像机,如文档中所示,然后还要检查找到两个摄像头共用的启用并发的配置文件 - MediaCapture.FindConcurrentProfiles ,请参阅docs 。 只有这样你才能安全地创建两个预览,并知道应用程序不会崩溃。

Yes, it is possible :-) . The MediaCapture class takes the default camera when you call the InitializeAsync method without parameters, but there is another overload that allows you to specify the device ID.

The documentation shows how to discover video capture devices:

DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

Now you can initialize multiple MediaCapture instances like this:

foreach ( var device in devices ) { var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id }; MediaCapture mediaCapture = new MediaCapture(); mediaCapture.InitializeAsync(mediaInitSettings); //do something with the media capture }

Naturally, when you want to display multiple previews, you will need to have multiple CaptureElements, each set to the specific MediaCapture instance you want.

However this approach is quite simplified. To make sure the concurrent capture and preview is supported, you must first ensure to query only cameras that support device profile using MediaCapture.IsVideoProfileSupported method as shown in the documentation and then also check find a concurrency-enabled profile common for both cameras - MediaCapture.FindConcurrentProfiles, see docs. Only then you can safely create the two previews and know the app will not crash.

更多推荐

摄像头,preview,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-17 13:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144935.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:摄像头   如何在   网络   UWP   preview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!