我正在尝试通过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中崩溃
基础教程推荐
- 使用Log4net进行日志记录 2023-05-11
- unity实现延迟回调工具 2023-05-06
- C# DateTime.Compare()方法案例详解 2023-04-28
- WPF自定义TreeView控件样式实现QQ联系人列表效果 2022-12-11
- C#异步原理详情 2023-05-06
- C#中Razor模板引擎简单使用 2023-05-22
- C# 利用VS编写一个简单的网游客户端 2023-05-11
- .NET MemoryCache如何清除全部缓存 2023-05-11
- 教你创建一个带诊断工具的.NET镜像 2023-06-27
- C#实现简易画图板的示例代码 2023-04-14