我正在尝试使用自动对焦功能编写一个基本的相机应用程序来处理条形码。如果我在相机聚焦时按下回,它会进入一个空白屏幕并挂在那里 - 不会引发异常。我已将问题追溯到 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);
}