5
votes

I am creating a Windows Universal application. I want to the user to be able to upload a picture and the user should have the option of taking one on the spot and sending that. I have this working using the MediaCapture api. However I can only seem to use one camera, so for example if my phone has a front and a back camera only the front camera is used. How would I be able to switch the camera that is in use?

I had read something somewhere 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 deviceID is always null.

Alternatively is there the option of giving control to an application that takes the picture and returns the taken picture to my application? I have found the following, but it doesn't work for Windows Universal apps: http://msdn.microsoft.com/en-us/library/windows/apps/hh394006(v=vs.105).aspx

1
Can you try to run in debug mode the line: var devices = (await DeviceInformation.FindAllAsync(DeviceClass.All)).ToList();, then check what devices are returned? Can you find the cameras there?Romasz

1 Answers

4
votes

Here is how I would do it:

First the initialization part

// 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();

Secondly take the 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);

That should solve your problem.