Screen hangs while capturing Qrcode

I am working on a Windows Phone 8 application that requires a Qrcode scannig, and I use the Zxing library to scan Qr codes.

In this application, I need to return to the previous page after scanning is completed.

I use the code below

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

This code works fine, but several times when capturing qrcode, it hangs the application.

edited

This problem occurs when I run the application without debugging mode. And when the application becomes unresponsive, after some time it crashes, but does not give any error messages.

So help me solve this problem. Thanks in advance.

+4
source share
1

, , QR- , . .

Windows Phone .

0

Source: https://habr.com/ru/post/1524427/


All Articles