使用WP.1商店应用程序捕获图像

时间:2015-02-04 13:09:49

标签: c# camera windows-phone-8.1

我正在开发WP8.1中的Windows应用商店应用。我需要通过相机拍照并将其保存并用于显示,但每次都会抛出异常。我的代码是 -

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
            {

            }
        }

它抛出一个异常对象必须被初始化,如果我使用InitalizeAsync它显示System.Exception并且消息是与Exception相关的文本无法找到。如果我使用此代码

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

我发现了错误------找不到请求的属性。 (来自HRESULT的异常:0xC00D36E6) -  有人可以帮忙吗?

Link to my project named Camera

1 个答案:

答案 0 :(得分:0)

我认为您遇到了几个问题:

  • 您不应在本地范围内声明 MediaCapture
  • 你应该preview your photo before taking it
  • 你应该总是 Dispose() MediaCapture - 我认为这可能是最大的问题,第一次初始化它会正常工作,但是当你停止调试时您没有配置捕获管理器 - 在这种情况下,除非您重新启动手机,否则进一步初始化将失败。 MediaCapture 需要小心处理
  • 还要注意不要多次触发 Tapped 事件

我已经尝试过这样的代码并且运行得很好:

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();
}
相关问题