Windows (phone) 8.1 Using the camera

I am creating a universal Windows application. I want the user to be able to upload the image, and the user should be able to take it in place and send it. It works for me using api MediaCapture. However, I can only use one camera, for example, if my phone has front and rear cameras, only the front camera is used. How can I switch the camera that is in use?

I read something about using something like this:

private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired) { DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)) .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); return deviceID; } 

However, this always returns null for me, since the device identifier is always zero.

Alternatively, is it possible to control an application that takes a snapshot and returns the captured image to my application? I found the following, but it does not work for Windows Universal applications: http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx

+5
source share
1 answer

Here's how I do it:

Initialization Part First

 // First need to find all webcams DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All) // Then I do a query to find the front webcam DeviceInformation frontWebcam = (from webcam in webcamList where webcam.EnclosureLocation != null && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front select webcam).FirstOrDefault(); // Same for the back webcam DeviceInformation backWebcam = (from webcam in webcamList where webcam.EnclosureLocation != null && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back select webcam).FirstOrDefault(); // Then you need to initialize your MediaCapture newCapture = new MediaCapture(); await newCapture.InitializeAsync(new MediaCaptureInitializationSettings { // Choose the webcam you want VideoDeviceId = backWebcam.Id, AudioDeviceId = "", StreamingCaptureMode = StreamingCaptureMode.Video, PhotoCaptureSource = PhotoCaptureSource.VideoPreview }); // Set the source of the CaptureElement to your MediaCapture // (In my XAML I called the CaptureElement *Capture*) Capture.Source = newCapture; // Start the preview await newCapture.StartPreviewAsync(); 

Second take a picture

 //Set the path of the picture you are going to take StorageFolder folder = ApplicationData.Current.LocalFolder; var picPath = "\\Pictures\\newPic.jpg"; StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName); ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); //Capture your picture into the given storage file await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile); 

This should solve your problem.

+4
source

Source: https://habr.com/ru/post/1203777/


All Articles