简体   繁体   中英

How to display 2 web camera preview in UWP?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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