Reputation: 6590
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
.
Here is some code.
Button click code
_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);
Here is my OnPhotoCameraInitialized code
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);
}
}
Here is my NavigationFrom code
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);
}
}
I have re-Initialized photocamera in OnNavigatedTo Method
Here is OnNavigatedTo code.
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);
}
}
}
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?
Upvotes: 0
Views: 647
Reputation: 6590
Hello to all my problem is solved.
I have changed code in OnNavigatedFrom
and OnPhotoCameraInitialized
.
Here is code 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;
}
});
}
Here is my 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.
Upvotes: 1