2012-04-17 30 views
1

我正在嘗試使用自動對焦功能編寫基本相機應用程序以處理條形碼。如果我按照相機聚焦的方式向後按,則會進入空白屏幕並掛在那裏 - 不會拋出異常。我已經將問題追溯到CaptureImage()方法,我可以看到它被調用,但是從不調用OnCaptureImageAvailable方法。即使我沒有解開OnNavigatedFrom方法中的OnCaptureImageAvailable,也會發生這種情況。如果在拍照時按下背面,WP7相機CaptureImage不會返回

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

問題在於你正在進入前一頁並在相機忙於捕捉「_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(); 
      }); 
     } 
    } 
0

我會嘗試在OnNavigatedFrom清除源viewfinderBrush

也嘗試不要在那裏配置_camera,看看是否有幫助。

+0

如果我刪除OnNavigateFrom中的_camera.Dispose(),它將返回調用頁面,但無法再打開此頁面。似乎相機尚未準備好進行初始化。我已經在這裏上傳了這個項目:'https:// github.com/cobusbernard/CameraIssue /'來說明問題。 – 2012-04-20 09:29:37