2014-01-30 29 views
4

我正在使用需要Qrcode scannig的Windows Phone 8應用程序,並且正在使用Zxing庫掃描Qr代碼。屏幕在捕獲Qrcode時掛起

在這個應用程序,我需要回到前一頁掃描完成後。

我使用下面的代碼,

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 

    _phoneCamera = new PhotoCamera(); 
    _phoneCamera.Initialized += cam_Initialized; 
    _phoneCamera.AutoFocusCompleted += _phoneCamera_AutoFocusCompleted; 

    CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed; 

    viewfinderBrush.SetSource(_phoneCamera); 

    _scanTimer = new DispatcherTimer(); 
    _scanTimer.Interval = TimeSpan.FromMilliseconds(1500); 
    _scanTimer.Tick += (o, arg) => ScanForBarcode(); 

    base.OnNavigatedTo(e); 
} 

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) 
{ 
    _scanTimer.Stop(); 
    if (cameraInit) 
    { 
     Dispatcher.BeginInvoke(() => 
      {      
       if (_phoneCamera != null) 
       { 
        _phoneCamera.CancelFocus(); 
        _phoneCamera.Dispose();       
        _phoneCamera.Initialized -= cam_Initialized; 
        _phoneCamera = null; 
        cameraInit = false; 
       } 
      }); 
    } 
} 

void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e) 
{ 

    if (e.Succeeded) 
    { 
     cameraInit = true; 
     this.Dispatcher.BeginInvoke(() => 
     {     
      _phoneCamera.FlashMode = FlashMode.Auto; 
      _previewBuffer = new WriteableBitmap((int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height); 
      _barcodeReader = new BarcodeReader(); 

      (int)_phoneCamera.PreviewResolution.Width, (int)_phoneCamera.PreviewResolution.Height, 0, 100); 

      var supportedBarcodeFormats = new List<BarcodeFormat>(); 
      supportedBarcodeFormats.Add(BarcodeFormat.QR_CODE); 
      supportedBarcodeFormats.Add(BarcodeFormat.DATA_MATRIX); 
      _barcodeReader.Options.PossibleFormats = supportedBarcodeFormats; 

      _barcodeReader.Options.TryHarder = true;    
      _barcodeReader.ResultFound += _bcReader_ResultFound; 
      _scanTimer.Start(); 
     }); 
    } 
    else 
    { 
     Dispatcher.BeginInvoke(() => 
     { 
      MessageBox.Show("Unable to initialize the camera"); 
     }); 
    } 

} 


void _bcReader_ResultFound(Result obj) 
{ 
    Dispatcher.BeginInvoke(() => 
    { 
     VibrateController.Default.Start(TimeSpan.FromMilliseconds(100)); 
     a = obj.Text; 
     _scanTimer.Stop(); 
     NavigationService.GoBack(); //here I am going back to previos page 
    }); 
} 

private void ScanForBarcode() 
{      
    _phoneCamera.GetPreviewBufferArgb32(_previewBuffer.Pixels); 
    _previewBuffer.Invalidate(); 
    _barcodeReader.Decode(_previewBuffer);   
} 

此代碼工作正常,但有些時候,同時捕捉它掛起應用程序的QR碼。

EDITED

當我運行而不調試模式下的應用程序會出現此問題。 而當應用程序變得沒有響應時,在某段時間後崩潰但不會給出任何錯誤消息。

所以請幫我解決這個問題。提前致謝。

+1

定義「掛起」。應用程序在一段時間內無響應,還是會崩潰?是否有相關的錯誤信息? – J0e3gan

+0

它變得沒有響應,並在某段時間後崩潰,但它不顯示任何錯誤消息。我現在編輯我的問題.... – user2428823

+0

@ user2428823你解決了嗎? – asitis

回答

0

我不確定,但可能是因爲您試圖掃描UI線程上的QR碼導致掛起。嘗試在不同的線程上做它。

Windows Phone操作系統不喜歡沒有反應的UI線程,並可能導致應用程序意外關閉。