在摄像头完全初始化之前锁定手机的问题.当我解锁手机时,它坏了
本文关键字:手机 解锁 坏了 问题 初始化 锁定 摄像头 | 更新日期: 2023-09-27 18:28:01
我正在开发一个相机应用程序,当我在相机完全初始化之前离开该应用程序并试图返回到它们时,我遇到了问题。在我的OnNavigatedFrom方法中,我有:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
try
{
if (cameraInit)
{
Dispatcher.BeginInvoke(() => {
if (cam != null)
{
cam.Dispose();
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
cam = null;
cameraInit = false;
}
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是OnNavigatedTo我的方法:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
{
if (cam == null)
{
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
//Create the camera event handlers
//// Event is fired when the capture sequence is complete.
cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);
//// Event is fired when the capture sequence is complete and an image is available.
cam.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(cam_CaptureImageAvailable);
//// The event is fired when auto-focus is complete.
cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
//// The event is fired when the shutter button receives a full press.
cam.Initialized += cam_Initialized;
CameraButtons.ShutterKeyPressed += OnButtonFullPress;
}
////Set the VideoBrush source to the camera.
canvasCamBrush.SetSource(cam);
}
这是我的cam_initialized方法:
void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
{
if (e.Succeeded)
{
cameraInit = true;
if (cameraInit)
{
cam.FlashMode = FlashMode.Off;
}
}
}
主要问题是,当onNavigatedFrom方法启动时,凸轮未完全初始化,然后当我返回应用程序时,它会中断OnNavigatedTo的方法,因此我需要在那里等待,直到cam_initialized方法被激发,然后再运行onNavigatedProm。
以下是另一个有这个问题但我无法解决的人的例子:
初始化时出现PhotoCamera问题
谢谢大家,L
在离开应用程序直到凸轮完全初始化之前,我通过"无限循环"解决了这个问题。OnNavigatedFrom代码是:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
try
{
while (!cameraInit)
{
System.Threading.Thread.Sleep(100);
}
if (cameraInit)
{
Dispatcher.BeginInvoke(() => {
if (cam != null)
{
cam.Dispose();
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
cam = null;
cameraInit = false;
}
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我能找到的最好的解决方案。希望这能帮助到别人。感谢