初始化时 PhotoCamera 问题 [英] PhotoCamera issue while Initialization
问题描述
我正在开发使用 PhotoCamera
捕捉图像的 windows phone 8 应用程序.按屏幕锁定键时出现问题.当我按下锁定键时,我的 PhotoCamera
对象没有处理.我在相机初始化
时按屏幕锁定键时出现问题.
I am developing windows phone 8 app which use PhotoCamera
for capturing image. I am getting issue when I press screen lock key. My PhotoCamera
object not disposing when I press lock key. I am getting the problem when I press screen lock key at the time of Camera Initialization
.
这是一些代码.
按钮点击代码
_photoCamera = new PhotoCamera();
_photoCamera.Initialized += OnPhotoCameraInitialized;
_photoCamera.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(cam_CaptureImageAvailable);
_photoCamera.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
_previewTransform.Rotation = _photoCamera.Orientation;
_previewVideo.SetSource(_photoCamera);
这是我的OnPhotoCameraInitialized代码
private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
{
try
{
Dispatcher.BeginInvoke(() =>
{
gvCamera.Visibility = Visibility.Visible;
gvCameraImage.Visibility = Visibility.Collapsed;
Cancel.Visibility = Visibility.Visible;
imgScanCancle.Visibility = Visibility.Visible;
});
cameraInit = true;
_photoCamera.FlashMode = FlashMode.Auto;
_photoCamera.Focus();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
这是我的NavigationFrom代码
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
try
{
if (cameraInit)
{
if (_photoCamera != null)
{
_photoCamera.Dispose();
_photoCamera.Initialized -= OnPhotoCameraInitialized;
_photoCamera.CaptureImageAvailable -= cam_CaptureImageAvailable;
_photoCamera.AutoFocusCompleted -= cam_AutoFocusCompleted;
_photoCamera = null;
cameraInit = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我在 OnNavigatedTo 方法中重新初始化了相机
I have re-Initialized photocamera in OnNavigatedTo Method
这是OnNavigatedTo 代码.
if (GlobalSettings.IspreservedState)
{
if (!GlobalSettings.istest_performed)
{
if (_photoCamera == null)
{
_nbTry = 0;
_photoCamera = new PhotoCamera();
_photoCamera.Initialized += OnPhotoCameraInitialized;
_photoCamera.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(cam_CaptureImageAvailable);
_photoCamera.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
_previewTransform.Rotation = _photoCamera.Orientation;
_previewVideo.SetSource(_photoCamera);
}
}
}
基本上我想做的是捕获图片并显示它.在相机初始化之前按下锁定键时出现问题.当我按下锁定键时,OnNavigatedFrom 代码开始执行.当它到达 if(cameraInit)
内部代码时不会执行,因为 cameraInit
不正确.我在 OnPhotoCameraInitialized 方法中设置了 cameraInit = true;
.但不幸的是这个代码块没有执行.所以我的问题是如何处理相机对象?我只能在它完全初始化时处理它.但是我在 Initialization
之前按了锁定键.有没有办法清理视频刷?
Basically what I am trying to do is Capture the picture and display it. I have a problem when I press a lock key before camera Initialization. When I press lock key, OnNavigatedFrom code start executing. When it reach to if(cameraInit)
inside code not executed because cameraInit
not true. I have set cameraInit = true;
in OnPhotoCameraInitialized method. But unfortunately this code block not execute. so my question is How can I dispose camera object? I can dispose it only when it is fully Initialized. But I press lock key before Initialization
. Is there any way to clean the video brush?
推荐答案
大家好,我的问题都解决了.
Hello to all my problem is solved.
我更改了 OnNavigatedFrom
和 OnPhotoCameraInitialized
中的代码.
I have changed code in OnNavigatedFrom
and OnPhotoCameraInitialized
.
这是代码OnNavigatedFrom
if (cameraInit)
{
Dispatcher.BeginInvoke(() =>
{
if (_photoCamera != null)
{
_photoCamera.Dispose();
_photoCamera.Initialized -= OnPhotoCameraInitialized;
_photoCamera.CaptureImageAvailable -= cam_CaptureImageAvailable;
_photoCamera.AutoFocusCompleted -= cam_AutoFocusCompleted;
_photoCamera = null;
cameraInit = false;
}
});
}
这是我的 OnPhotoCameraInitialized
if (e.Succeeded)
{
cameraInit = true;
Dispatcher.BeginInvoke(() =>
{
gvCamera.Visibility = Visibility.Visible;
gvCameraImage.Visibility = Visibility.Collapsed;
Cancel.Visibility = Visibility.Visible;
imgScanCancle.Visibility = Visibility.Visible;
if (cameraInit)
{
_photoCamera.FlashMode = FlashMode.Auto;
_photoCamera.Focus();
}
});
}
这里当我在初始化之前按下屏幕锁定键
时,我做的第一件事就是完全初始化相机,然后我处理
它.现在一切正常.
Here When I press screen lock key
before Initialization first thing I did i.e. I fully Initialized camera and then I dispose
it. Now all working fine.
这篇关于初始化时 PhotoCamera 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!