1

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

2 回答 2

0

我会尝试viewfinderBrush在您的 OnNavigatedFrom 中清除源代码。

也尽量不要_camera在那里处理,看看是否有帮助。

于 2012-04-17T16:47:40.957 回答
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();
            });
        }
    }
于 2012-04-20T14:00:10.377 回答