我正在尝试使用自动对焦功能编写基本的相机应用来处理条形码。如果我在相机聚焦时向后按,则会进入空白屏幕并挂在那里 - 没有异常被抛出。我已经将问题追溯到CaptureImage()方法,我可以看到它被调用,但是从不调用OnCaptureImageAvailable方法。即使我没有在OnNavigatedFrom方法中取消挂起OnCaptureImageAvailable,也会发生这种情况。
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
_camera.AutoFocusCompleted -= OnCameraAutoFocusCompleted;
_camera.CaptureImageAvailable -= OnCaptureImageAvailable;
_camera.Initialized -= OnCameraInitialized;
_camera.Dispose();
_camera = null;
}
private void InitializeCamera()
{
_camera = new PhotoCamera();
_camera.Initialized += OnCameraInitialized;
viewfinderBrush.SetSource(_camera);
}
void OnCameraInitialized(object sender, EventArgs e)
{
_camera.Initialized -= OnCameraInitialized;
_camera.AutoFocusCompleted += OnCameraAutoFocusCompleted;
_camera.CaptureImageAvailable += OnCaptureImageAvailable;
_camera.FlashMode = FlashMode.Off;
_camera.Focus();
}
private void OnCameraAutoFocusCompleted(object sender, EventArgs e)
{
_camera.CaptureImage();
}
private void OnCaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
if (_camera != null && e.ImageStream != null)
ScanBarcode(e);
}
答案 0 :(得分:0)
我会尝试在OnNavigatedFrom中清除viewfinderBrush
的来源。
同时尝试不要在那里处理_camera
,看看是否有帮助。
答案 1 :(得分:0)
问题是,当相机忙于在“_camera.CaptureImage();”中捕捉图像时,您将转到上一页并处理相机。
如何保留两个布尔标志:“doBack”和“captureCompleted”。
然后,当OnCameraAutoFocusCompleted事件触发时,请不要返回:
private void OnCameraAutoFocusCompleted(object sender, EventArgs e)
{
_camera.CaptureImage();
}
按下后退键时,设置doBack标志并忽略,除非设置了captureCompleted:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (!captureCompleted)
{
this.doBack = true;
e.Cancel = true;
}
base.OnBackKeyPress(e);
}
然后,当OnCameraCaptureCompleted事件触发时,如果设置了doBack标志,则设置captureCompleted标志并转到上一页(并配置相机):
private void OnCameraCaptureCompleted(object sender, EventArgs e)
{
this.captureCompleted = true;
if (doBack)
{
this.Dispatcher.BeginInvoke(delegate()
{
NavigationService.GoBack();
});
}
}