2014-01-30 28 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线程,并可能导致应用程序意外关闭。