Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display 2 web camera preview in UWP?

Tags:

c#

webcam

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?

like image 844
Kay Avatar asked Mar 07 '23 16:03

Kay


1 Answers

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.

like image 198
Martin Zikmund Avatar answered Mar 14 '23 19:03

Martin Zikmund