Question

I'm doing a qr code reader.

When I scan a qr I'd like to navigate in another page named reader.xaml where I do some stuff.

but when i press the button BACK from reader.xaml to the initial page... sometimes appears a green screen and i must reboot my lumia 920...

I don't understand why!?!?!

I'm using zxing library.

Someone can help me please

    public MainPage()
    {
        InitializeComponent();
        /* CHECKING CONNECTION STATUS */
        bool isConnected = NetworkInterface.GetIsNetworkAvailable();
        if (!isConnected)
        {
            MessageBox.Show("No internet connection", MessageBoxButton.OK);
            Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
            game.Exit(); 
        }

        //reading value
        if (IsolatedStorageSettings.ApplicationSettings.Contains("idDevice"))
        {
            idDevice = (string)IsolatedStorageSettings.ApplicationSettings["idDevice"];
        }   
        else
        {
            //insert
            Random random = new Random();
            var duration = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0);
            long ticks = (long)duration.TotalSeconds;

            idDevice = ticks.ToString() + "-" + random.Next(1, 2000000001).ToString();
            IsolatedStorageSettings.ApplicationSettings.Add("idDevice", idDevice);
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
        GameTimer gameTimer = new GameTimer();
        gameTimer.UpdateInterval = TimeSpan.FromMilliseconds(33);

        // Call FrameworkDispatcher.Update to update the XNA Framework internals.
        gameTimer.Update += delegate { try { FrameworkDispatcher.Update(); } catch { } };

        // Start the GameTimer running.

        if (Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Playing || Microsoft.Xna.Framework.Media.MediaPlayer.State == MediaState.Paused)
        {
            if (MessageBox.Show("Media is currently playing, do you want to stop it?", "Stop Player", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                try
                {
                    gameTimer.Stop();
                    Microsoft.Xna.Framework.Media.MediaPlayer.Stop();
                    AudioPlayerS.Source = new Uri("/audioProva.mp3", UriKind.Relative);
                }
                catch { }
            }
            else
            {
                Microsoft.Xna.Framework.Game game = new Microsoft.Xna.Framework.Game();
                game.Exit(); 
            }  

       }

        // Prime the pump or we'll get an exception.
        FrameworkDispatcher.Update();
    }


    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        _photoCamera = new PhotoCamera();
        _photoCamera.Initialized += OnPhotoCameraInitialized;
        _previewVideo.SetSource(_photoCamera);

        //CameraButtons.ShutterKeyHalfPressed += (o, arg) => _photoCamera.Focus();
        try
        {
            if (_photoCamera.IsFocusSupported)
            {
                _timer.Tick += (o, arg) => { try { _photoCamera.FlashMode = FlashMode.Off; _photoCamera.Focus(); } catch (Exception) { } };
                _photoCamera.AutoFocusCompleted += (o, arg) => { if (arg.Succeeded) ScanPreviewBuffer(); };
            }
            else
            {
                _timer.Tick += (o, arg) => ScanPreviewBuffer();
            }
        }
        catch
        {
            _timer.Tick += (o, arg) => ScanPreviewBuffer();
        }
        base.OnNavigatedTo(e);
    }

    private void OnPhotoCameraInitialized(object sender, CameraOperationCompletedEventArgs e)
    {
        int width = Convert.ToInt32(_photoCamera.PreviewResolution.Width);
        int height = Convert.ToInt32(_photoCamera.PreviewResolution.Height);

        _luminance = new PhotoCameraLuminanceSource(width, height);
        _reader = new QRCodeReader();

        Dispatcher.BeginInvoke(() =>
        {
            _timer.Start();
            _previewTransform.Rotation = _photoCamera.Orientation; 
        });
    }


    private void ScanPreviewBuffer()
    {

        try
        {
            _photoCamera.GetPreviewBufferY(_luminance.PreviewBufferY);
            var binarizer = new HybridBinarizer(_luminance);
            var binBitmap = new BinaryBitmap(binarizer);
            var result = _reader.decode(binBitmap);
            Dispatcher.BeginInvoke(() => CheckQr(result.Text));
        }
        catch
        {
        }
    }

    private void CheckQr(string link)
    {
        if (IsValidHttpUri(link) == false)
        {
            MessageBox.Show("error", MessageBoxButton.OK);
            return;
        }
        else
        {
            _timer.Stop();
            try
            {
                _photoCamera.CancelFocus();
            }
            catch
            {
            }
            NavigationService.Navigate(new Uri("/reader.xaml?linkQr=" + link, UriKind.Relative));
        }
    }

When i come back from reader.xaml appear the green screen.

https://zxingnet.codeplex.com/discussions/407383

Was it helpful?

Solution

The link provided in the question contained the solution, you need to

"did you try to Dispose() the PhotoCamera in the OnNavigatedFrom?"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top