In the previous post you have learned about the 5 simple steps to getting started with Camera in Windows Store App. By following those steps you should be able to get preview of video from the default camera from the device. When we developed apps for Tablets and most of the tablets has multiple cameras ( mainly front and rear ), it is important to give the preference to the users to choose the camera. Using WinRT Api, it is quite easy to deal with this scenarios.
First of all get the list of all camera devices by calling the FindAllAsync
method of DeviceInformation
class.
var Videodevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
FindAllAsync
() method will return list of available camera information from the device where your app is running. Once you have list of cameras, you can pass the Panel
Position ( Front / Back ) to get access of proper camera.
var rearCamera= Videodevices.FirstOrDefault(item => item .EnclosureLocation != null && item .EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
or
var frontCamera= Videodevices.FirstOrDefault(item => item .EnclosureLocation != null && item .EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
rearCamera
or frontCamera
will be null
if the specified camera is not found. EnclosureLocation class used to find the physical location of a device in its enclosure.
In the next step, you have to initialize the camera ( Step 3 of 5 simple steps to getting started with Camera in Windows Store App. ) with the respective camera id. You can use the using following code block.
private async void TurnOnCamera(DeviceInformation selectedCamera) { try { MediaCapture mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = selectedCamera.Id }); captureElement.Source= mediaCapture; await mediaCapture.StartPreviewAsync(); } catch { // Handle the exception . } }