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.
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
});
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With