初始化时照片相机问题

时间:2014-01-09 13:42:43

标签: c# windows-phone-7 windows-phone-8

我正在开发使用PhotoCamera捕获图像的Windows Phone 8应用程序。当我按下屏幕锁定键时,我遇到了问题。当我按下锁定键时,我的PhotoCamera对象没有处理掉。我在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 方法重新初始化了photocamera

这是 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之前按下锁定键。有没有办法清理视频画笔?

1 个答案:

答案 0 :(得分:1)

大家好,我的问题解决了。

我更改了OnNavigatedFromOnPhotoCameraInitialized中的代码。

以下是代码 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();
                    }
                });
            }

此处当我在初始化之前按screen lock key时,我做了第一件事,即我完全初始化了相机,然后我dispose。现在一切正常。