我正在尝试通过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中崩溃
- c# – WPF MessageBox看起来没有样式,而WindowsForms MessageBox看起来不错 2023-09-19
- C# BitArray点阵列的使用 2023-07-18
- C# 微信支付 wx.chooseWXPay 签名错误的解决方法 2022-12-01
- 详解WCF服务中的svc文件 2023-05-25
- c# – 从silverlight应用程序访问SQL Server数据库 2023-11-13
- C#列表List<T>、HashSet和只读集合介绍 2023-06-05
- C#和vb.net实现PDF 添加可视化和不可见数字签名 2023-04-27
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-31
- C# 操作PostgreSQL 数据库的示例代码 2022-11-19
- 使用C#Driver以名称打开MongoDB GridFS 2023-11-13