1

I have developed windows 8.1 store app, it need to be capture photo by using back camera and post.

MediaCaptureInitializationSettings _captureSettings = new var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    foreach (var device in devices)
    {
    if (device.EnclosureLocation != null && device.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back)
                        {
                            deviceId = device.Id;
                            break;
                        }
                    }
  if (!string.IsNullOrEmpty(deviceId))
                    {
                        _captureSettings.AudioDeviceId = "";
                        _captureSettings.VideoDeviceId = deviceId;
                        _captureSettings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo;
                        _captureSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
                    }
captureManager = new MediaCapture();
await captureManager.InitializeAsync(_captureSettings);
                    await captureManager.ClearEffectsAsync(MediaStreamType.Photo);
                    capturePreview1.Source = captureManager;
                    await captureManager.StartPreviewAsync();
</code>

Here i am getting two devices but that devices EnclosureLocation is null, so i can't find which one is front and back camera.

so have decided to get second device from list 

<code>
deviceId = devices[1].Id;
</code>

but it throws an error like "The current capture source does not have an independent photo stream."
in the line of initializing MediaCapture

<code>

await captureManager.InitializeAsync(_captureSettings);

</code>

i have tried in windows surface pro 2 and acer devices. Please advise. Thanks in advance.

1 Answers1

3

Try to organise your code better, you got two equals signs on the same line and your code is not well formated so it's hard to read.

To use Camera in Windows 8.1 stop app I use this code :

// First need to find all webcams
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)

// 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
var captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
    // Choose the webcam you want (backWebcam or frontWebcam)
    VideoDeviceId = backWebcam.Id,
    AudioDeviceId = "",
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});

// Set the source of the CaptureElement to your MediaCapture
capturePreview1.Source = captureManager;

// Start the preview
await captureManager.StartPreviewAsync();

This way it's easier to read. The code is not very different, MediaCaptureInitializationSettings is not the same.

This code works for me on Surface 2 RT and Nokia 635 so it should work for you.

Edit:

Seems it works on devices with Windows RT but on full windows 8.1 devices it's always null. Msdn says that:

If no enclosure location information is available, the property will be null

so what you can do is first try to see if you find a backwebcam and if it's null take the last one;

DeviceInformation backWebcam = (from webcam in webcamList
 where webcam.EnclosureLocation != null 
 && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
 select webcam).FirstOrDefault();

if (backWebcam == null)
{
   backWebcam = webcamList.Last();
}

But you are not sure the last one in the collection is the back one, so you should add a button to let the user switch camera

If you change camera,

await captureManager.StopPreviewAsync();

await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
    {
        // Choose an other webcam 
        VideoDeviceId = //id of the new webcam,
        AudioDeviceId = "",
        StreamingCaptureMode = StreamingCaptureMode.Video,
        PhotoCaptureSource = PhotoCaptureSource.VideoPreview
    });

 await captureManager.StartPreviewAsync();

this way you can be sure the user can choose the right camera even is you programmaticaly cannot tell which one is which

Aurelien Souchet
  • 721
  • 5
  • 11
  • 1
    Thank you so much for your response. I have tried your code, i can see two devices in 'webcamList', but the value of webcam.EnclosureLocation all devices is null. so i can't find the back camera. – Saranraj Palanisamy Oct 11 '14 at 04:40