18

Well, my question is simple:
How do I capture pictures with a Windows Store App for Windows Phone 8.1, using the camera?
The samples on MSDN use Windows.Media.Capture.CameraCaptureUI, which is not usable on Windows Phone, or are for Silverlight.
I can't find any doc or sample specifically for Windows Phone app using Windows Runtime.
If someone knows, or even have the doc for this, I would be glad.

Romasz
  • 29,662
  • 13
  • 79
  • 154
GlorfSf
  • 370
  • 1
  • 2
  • 12

3 Answers3

48

In WP8.1 Runtime (also in Silverlight) you can use MediaCapture. In short:

// First you will need to initialize MediaCapture
Windows.Media.Capture.MediaCapture  takePhotoManager = new Windows.Media.Capture.MediaCapture();
await takePhotoManager.InitializeAsync();

If you need a preview you can use a CaptureElement:

// In XAML: 
<CaptureElement x:Name="PhotoPreview"/>

Then in the code behind you can start/stop previewing like this:

// start previewing
PhotoPreview.Source = takePhotoManager;
await takePhotoManager.StartPreviewAsync();
// to stop it
await takePhotoManager.StopPreviewAsync();

Finally to take a Photo you can for example take it directly to a file CapturePhotoToStorageFileAsync or to a Stream CapturePhotoToStreamAsync:

ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();

// a file to save a photo
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
        "Photo.jpg", CreationCollisionOption.ReplaceExisting);

await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file);

If you want to capture video then here is more information.

Also don't forget to add Webcam in Capabilities of your manifest file, and Front/Rear Camera in Requirements.


In case you need to choose a Camera (fornt/back), you will need to get the Camera Id and then initialize MediaCapture with desired settings:

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

    if (deviceID != null) return deviceID;
    else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired));
}

async private void InitCamera_Click(object sender, RoutedEventArgs e)
{
    var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
    captureManager = new MediaCapture();
    await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.Photo,
            AudioDeviceId = string.Empty,
            VideoDeviceId = cameraID.Id                    
        });
}
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • @user3627550 I've added choosing the camera device (front/back). – Romasz May 16 '14 at 04:56
  • I have implemented everything like you have but when I want to take a photo the preview is flipped 180 degrees but looks ok when I hold the phone in landscape mode. Any ideas how to fix it? – Ivan Crojach Karačić Jul 27 '14 at 13:50
  • 3
    @IvanCrojachKaračić Once, I've written a [blog post](http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/), take a look at it. The default preview is rotated, in `StartPreviewBtn_Click` you will find: `captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);` - this should do the job. – Romasz Jul 27 '14 at 14:27
  • also add captureManager.SetPreviewMirroring(false); – user3290180 Aug 28 '15 at 09:50
  • @user3290180 This depends on user's needs. – Romasz Aug 28 '15 at 10:03
  • SetPreviewRotation must be called after InitializeAsync. – Jensen Nov 05 '15 at 18:01
  • @Jensen It seems to be logic - first initialize camera, before setting/modifying it's properties. – Romasz Nov 05 '15 at 18:05
  • @Romasz , for this example I could marry you. THANKS! – Yogurtu Apr 05 '16 at 18:34
  • @Yogurtu I'm glad you have found it useful. SO - a matrimonial service, thankfully it's a very rare case when somebody wants to merry me for a post :) – Romasz Apr 05 '16 at 19:45
3

In universal Windows Phone 8.1 (WinRT) apps it is no longer possible to jump directly into the built-in camera app and receive a callback when a picture was taken.

To do that you have to implement Windows.Media.Capture.MediaCapture as described above. There used to be CameraCatureUI but it is not available in WinRT apps for Windows Phone 8.1.

However there is a "workaround". You can use Windows.Storage.Pickers.FileOpenPicker and configure it to pick images. Now the picker will have a camera-button. The user can click the camera-button and the built-in camera app will open. Once the user has taken a picture you will receive a callback in your app. The FileOpenPicker callback is a little annoying to implement, but it works. If you can live with the usability implications this might be a valid approach.

There was a session on this topic during Microsofts build-Conference in 2014. You can watch the session online with this link.

RhodanV5500
  • 1,087
  • 12
  • 16
0

You can take the approach on this link. Everything is explained very nicely.

Just use the PhotoCamera class and don't forget to enable camera usage in your app manifest

Ivan Crojach Karačić
  • 1,911
  • 2
  • 24
  • 44
  • Well, thank you, but the point is that i need to complete a windows store app. The windows 8.1 apps use the windows runtime and this API is very different from the silverlight apps. And, of course, the complete API is not implemented for windows phone : some class very useful and simple that you can use for Windows 8.1 won't be usable for windows phone, and i can't find the others solutions. – GlorfSf May 12 '14 at 10:28