Photo Capture on Windows Store App for Windows Phone

Photo capture on Windows Store App for Windows Phone

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

picture capture by using universal app windows phone 8.1 winrt

In Windows 8 apps, the equivalent to CameraCaptureTask is CameraCaptureUI. Unfortunately it is not available for Windows Phone 8.1. So your only option is to use MediaCapture. Check this post for details: Photo capture on Windows Store App for Windows Phone

There is also a helper class that you can use as an alternative: CameraCaptureUI for Windows Phone. However it's very elementary and lacks customization.

Photo capture with Windows Phone 8.1 and JavaScript

I finally know what the difference is. One link helped me a lot: MediaCapture.StartPreviewAsync is not available in javascript? There is a link mentioned pointing to a sample source code: Media capture using capture device sample. The most important part:

mediaCapture = new Windows.Media.Capture.MediaCapture();
mediaCapture.initializeAsync().done(function(result){
var video = this.document.getElementById("PhotoPreview");
video.src = URL.createObjectURL(mediaCapture, { oneTimeOnly: true });
video.play();
})

Thanks to everyone reading my question.

Capture screen of Windows store App

You can't capture screen programatically. See the below threads from MSDN.

Programmatically take snap shot Windows RT.

How to capture screen in Metro app?

Capture Image using WP.1 store app

I think there are couple of problems you are facing:

  • you shouldn't declare MediaCapture in local scope
  • you should preview your photo before taking it
  • you should always Dispose() the MediaCapture - I think this may be the biggest problem, the first time you initialize it will work fine, but when you stop debugging you don't dispose the capture manager - in this case further initialization will fail unless you restart the phone. MediaCapture needs to be handeled carefully
  • also watch out not to fire Tapped event multiple times

I've tried such code and works just fine:

private bool initialized = false;
private MediaCapture captureManager;
async private void capturePhoto_Tapped(object sender, TappedRoutedEventArgs e)
{
if (initialized) return;
try
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
// StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
// ImageEncodingProperties img = new ImageEncodingProperties();
// img = ImageEncodingProperties.CreateJpeg();
// await captureManager.CapturePhotoToStorageFileAsync(img, sf);
}
catch (Exception ex) { (new MessageDialog("An error occured")).ShowAsync(); }
}

private void OffButton_Click(object sender, RoutedEventArgs e)
{
captureManager.Dispose();
}

How to use existing camera app(not create one using MediaCapture) and take picture in windows phone 8.1(WinRT)?

The app needs to use the MediaCapture API.

Windows Phone Runtime apps cannot directly launch the camera app to capture and return an image. There is no analogous API to CameraCaptureTask (Windows Phone Silverlight) or CameraCaptureUI (Windows Store apps).

can't we just launch the existing camera app using a launchUriAsync()
and get back the path of the file or the file itself in the form of an
argument?

In Windows 10 it would be possible for somebody to write a camera app service that provides this, but not in 8.1.

See the build session App-to-App Communication: Building a Web of Apps

Should we really have to implement our own media capture with
resolution etc?

http://wpdev.uservoice.com is the place to request features for the Windows Dev Platform.

Is there a system Task that can take a photo in the WinRT API?

On Windows 8.1 (not Phone) the Windows.Media.Capture.CameraCaptureUI class is really easy to use--just a couple lines of code, and it has built-in features like camera selection, pixel density, and cropping:

using Windows.Media.Capture; 
using Windows.Storage;

CameraCaptureUI dialog = new CameraCaptureUI();
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);

The file variable will have the captured image. Refer to the Camera Capture UI sample for more details and usage.

Do note that if you're targeting a universal Windows 8.1 app, this API isn't available on that version of Windows Phone and you'll need to write your own capture routine as Aurelien demonstrates.



Related Topics



Leave a reply



Submit