使用 WP.1 商店应用捕获图像
Capture Image using WP.1 store app
我正在 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 并且消息是与异常相关的文本不能 found.If 我使用此代码
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)--
有人可以帮忙吗?
我认为您面临着几个问题:
- 你不应该在本地范围内声明 MediaCapture
- 你应该preview your photo before taking it
- 你应该总是 Dispose() MediaCapture - 我认为这可能是最大的问题,第一次初始化它会工作正常,但是当您停止调试时,您不会处理捕获管理器 - 在这种情况下,进一步的初始化将失败,除非您重新启动 phone。 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();
}
我正在 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 并且消息是与异常相关的文本不能 found.If 我使用此代码
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)-- 有人可以帮忙吗?
我认为您面临着几个问题:
- 你不应该在本地范围内声明 MediaCapture
- 你应该preview your photo before taking it
- 你应该总是 Dispose() MediaCapture - 我认为这可能是最大的问题,第一次初始化它会工作正常,但是当您停止调试时,您不会处理捕获管理器 - 在这种情况下,进一步的初始化将失败,除非您重新启动 phone。 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();
}