我正在开发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) - 有人可以帮忙吗?
答案 0 :(得分:0)
我认为您遇到了几个问题:
我已经尝试过这样的代码并且运行得很好:
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();
}