我正在尝试通过Windows Phone应用程序中的TorchControl类来操作手电筒应用程序:这是我的代码private static async TaskDeviceInformation GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera){DeviceI...

我正在尝试通过Windows Phone应用程序中的TorchControl类来操作手电筒应用程序:
这是我的代码
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera {0} doesn't exist", desiredCamera));
}
async private void Button_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = String.Empty,
VideoDeviceId = cameraID.Id
});
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
tc.Enabled = true;
mediaDev.Dispose();
}
但是问题是,我每次第二次单击按钮时,应用程序都会崩溃.有人告诉我使用mediaDev.Dispose()方法,但它也无法正常工作.
这是例外:
A first chance exception of type ‘System.Exception’ occurred in
mscorlib.ni.dll WinRT information: The text associated with this error
code could not be found.
>突出显示“ initializeasync”中的文本时显示
解决方法:
MediaCapture在重新初始化时将引发异常.要解决此问题,只需确保当您导航回到“相机”页面或单击“相机”按钮时,不要两次初始化MediaCapture.
MediaCapture mediacapture = new MediaCapture();
bool initialized;
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
if (initialized == false)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
await mediacapture.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
//Selecting Maximum resolution for Video Preview
var maxPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Height > (i2 as VideoEncodingProperties).Height ? i1 : i2);
//Selecting 4rd resolution setting
var selectedPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ElementAt(3);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, selectedPhotoResolution);
await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, maxPreviewResolution);
// in my .xaml <CaptureElement Name="viewfinder" />
viewfinder.Source = mediacapture;
mediacapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
await mediacapture.StartPreviewAsync();
initialized = true;
}
另外,在导航至其他页面之前或在相机再次开始预览之前,请确保相机停止预览.无需处置MediaCapture.
private async void GoBack_Click(object sender, RoutedEventArgs e)
{
await mediacapture.StopPreviewAsync();
this.Frame.Navigate(typeof(MainPage));
//Not needed
//mediacapture.Dispose();
}
GetCameraID方法归功于Romasz的博客. http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/
本文标题为:c#-Flashlight应用程序每次在Windows Phone中崩溃


基础教程推荐
- Unity实现鼠标或者手指点击模型播放动画 2023-02-06
- c#中判断类是否继承于泛型基类 2023-05-30
- C#实现自定义打印文字和图片的示例代码 2023-05-31
- c# – 将集合绑定到Windows窗体中的DataGridView 2023-09-19
- C# Aspose.Words 删除word中的图片操作 2023-03-28
- C#中使用Spire.XLS来操作Excel数据的实现 2023-07-18
- C#实现QQ聊天窗口 2023-05-16
- .net core 中实现一个堆结构 2023-09-27
- Unity实现轮盘方式的按钮滚动效果 2023-01-11
- C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压 2023-07-04