2013-02-04 47 views
2

我使用PhotoCamera API構建QR碼掃描頁面(使用ZXing)。但是,此頁面只是應用程序的一小部分,因此並不總是顯示。因此,應用程序在這個頁面和其他一些具有通用控件的頁面之間進行導航。PhotoCamera API:如何正確處置它?

問題是,有時候,掃描後,整個應用程序減速到30fps而不是60fps,沒有真正的原因。我懷疑相機仍在後臺運行,幀同步將應用程序鎖定爲30fps,這是我的問題:如何正確處理使用PhotoCamera API的頁面?

我的XAML:

<Grid Background="Black"> 
    <ProgressBar x:Name="PBar" IsIndeterminate="True" VerticalAlignment="Center" /> 

    <Rectangle x:Name="ScanRect"> 
     <Rectangle.Fill> 
      <VideoBrush x:Name="ScanVideoBrush" /> 
     </Rectangle.Fill> 
    </Rectangle> 
</Grid> 

我的C#停止掃描過程:

private void StopScan() { 
     if (analysisTimer != null) { 
      analysisTimer.Stop(); 
      analysisTimer = null; 
     } 

     if (focusTimer != null) { 
      focusTimer.Stop(); 
      focusTimer = null; 
     } 

     if (camera != null) { 
      camera.Dispose(); 
      camera.Initialized -= OnCameraInitialized; 
      camera = null; 
     } 

     // Following two lines are a try to dispose stuff 
     // as much as possible, but the app still lags 
     // sometimes after a scan... 

     ScanVideoBrush.SetSource(new MediaElement()); 
     ScanRect.Fill = new SolidColorBrush(Colors.Black); 
    } 

注:我在的Lumia 920

回答

0

測試應用程序調用cam.Dispose();應處理圖像源流和Camera對象使用的空閒資源,所以很好。

是否確定要釋放內存,即取消訂閱PhotoCamera類Events?

你的StopScan方法何時被調用?一個好的做法可以叫做PhoneApplicationPageOnNavigatingFrom

下面是從MSDN的代碼示例部署的PhotoCamera:

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) 
{ 
    if (cam != null) 
    { 
     // Dispose camera to minimize power consumption and to expedite shutdown. 
     cam.Dispose(); 

     // Release memory, ensure garbage collection. 
     cam.Initialized -= cam_Initialized; 
     cam.CaptureCompleted -= cam_CaptureCompleted; 
     cam.CaptureImageAvailable -= cam_CaptureImageAvailable; 
     cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable; 
     cam.AutoFocusCompleted -= cam_AutoFocusCompleted; 
     CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress; 
     CameraButtons.ShutterKeyPressed -= OnButtonFullPress; 
     CameraButtons.ShutterKeyReleased -= OnButtonRelease; 
    } 
} 

Source

+0

我知道,我也正是因爲MSDN說,但還是有一些在後臺不時。我會嘗試更深入地剖析應用程序。謝謝。 –

+0

您是否有處置相機的成功?看起來我和你有同樣的問題 – Vovich