c# - 捕获二维码时屏幕挂起

标签 c# windows-phone-8 windows-phone qr-code

我正在开发需要 Qrcode scannig 的 Windows Phone 8 应用程序,我正在使用 Zxing 库扫描 Qr 代码。

在这个应用程序中,我需要在扫描完成后返回到上一页。

我正在使用下面的代码,

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);         
}

这段代码工作正常,但有时在捕获二维码时会挂起应用程序。

已编辑

当我在没有 Debug模式的情况下运行应用程序时会出现此问题。 当应用程序变得无响应时,它会在一段时间后崩溃但不会给出任何错误消息。

所以请帮我解决这个问题。提前致谢。

最佳答案

我不确定,但是否有可能您正在尝试扫描 UI 线程上的二维码从而导致挂起。尝试在不同的线程上执行它。

Windows Phone 操作系统不喜欢无响应的 UI 线程,可能会导致应用程序意外关闭。

关于c# - 捕获二维码时屏幕挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448143/

相关文章:

c# - 如何在代码隐藏中设置DB ConnectionStringSettings

c# - 如何从返回 Task<T> 的异步函数返回错误

c# - 这是将 xml 值加载到结构中的最有效方法吗?

c# - 带有是和否按钮的消息框

c# - 如何从堆栈跟踪中获取行号?

windows-phone-8 - Windows 10 UWP 可以在 Windows 8.1 设备上运行吗?

c# - 实现文件上传的 MVP 模式

c# - CustomMessageBox 中的列表框总是向上滚动

javascript - 如何在输入文本框时显示键盘

c# - 如何在 CaptureElement 中旋转相机 View ?