Capture Image using WP.1 store app

0

I am developing an Windows Store App in WP8.1. I need to take photo through camera and save it and use it for showing but it throws exception everytime. My code is--

async private void capturePhoto_Tapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                MediaCapture mediaCapture = new MediaCapture();

                StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
                ImageEncodingProperties img = new ImageEncodingProperties();
                await mediaCapture.CapturePhotoToStorageFileAsync(img, sf);
            }
            catch
            {

            }
        }

It throws an exception Object must be initailized and if I use InitalizeAsync it shows System.Exception and message is Text related to the Exception could not be found.If I use this code

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);
            var captureManager = new MediaCapture();
            await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.Photo,
                AudioDeviceId = string.Empty,
                VideoDeviceId = cameraID.Id
            });
            StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
            ImageEncodingProperties img = new ImageEncodingProperties();
            await captureManager.CapturePhotoToStorageFileAsync(img, sf);
        }

I found the error ------ The requested attribute was not found. (Exception from HRESULT: 0xC00D36E6)-- Can anybody help?

Link to my project named Camera

c#
camera
windows-phone-8.1
asked on Stack Overflow Feb 4, 2015 by (unknown user) • edited Feb 5, 2015 by (unknown user)

1 Answer

0

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();
}
answered on Stack Overflow Feb 5, 2015 by Romasz • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0