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

  • Take a look [at this answer](http://stackoverflow.com/a/23606620/2681948) also ensure that you have declared the needed capabilities. – Romasz Feb 04 '15 at 13:11
  • I tried this but I got an error --------------The text associated with this error code could not be found.--------which I described in my question –  Feb 04 '15 at 13:15
  • Where do you get this error? Also have you debugged your program? – Romasz Feb 04 '15 at 13:18
  • I did it and error is on Initailize method and CapturePhotoToStorage method and I can not find the solution to it. I have updated my code with new error all I have tried. –  Feb 04 '15 at 13:21
  • Does `GetCameraID` return a valid camera device? Is it possible that you can share a sample? – Romasz Feb 04 '15 at 13:27
  • GetCameraID returns a valid camera device and this is my actual code which i call on tap of textblock –  Feb 05 '15 at 04:55
  • Is it possible that you can share a sample with the problem? – Romasz Feb 05 '15 at 09:51
  • I have added my code sample in my question at the bottom with both methods –  Feb 05 '15 at 10:27

1 Answers1

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();
}
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • I added this code but still I am not able to launch the camera. I am having trouble in even launching the camera –  Feb 10 '15 at 06:37
  • @Rohit Just adding the code above won't help you unless you check other things I've mentioned. Do a simple test: restart your phone, run above initialization and show preview of camera on the screen - can you see something, does it work? – Romasz Feb 10 '15 at 08:23
  • I checked my code and it worked after I deleted the app and restarted it. Thanks for your help. Sometimes it throws error and needs to restart the phone –  Feb 10 '15 at 12:43
  • @Rohit It means that you hadn't freed the resources properly - what is the most important thing once dealing with *MediaCapture*. Keep that in mind - good luck. – Romasz Feb 10 '15 at 13:52