Get read string from ZxingScannerPage, Xamarin Forms

0

I would like to know if you know how to get the text obtained from a QR code reading, in X.Forms, the problem is that I do not understand the code very well, the Device.BeginInovke ... confuses me:

    private async void escanearQR()
    {
        // Página que escanea códigos
        var scannerPage = new ZXingScannerPage();
        // Título de la página
        await Navigation.PushAsync(scannerPage);
        // Resultado del escaner
        scannerPage.OnScanResult += (resultado) =>
        {
            // Detener escaneo del dispositivo
            scannerPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(async () =>
            {
                await Navigation.PopAsync();
                // return resultado.text ?????
            });
        };

    }
    
asked by Rodrigo Contreras 07.12.2018 в 14:12
source

1 answer

0

For what I see in your code and on pages like this the base example is set so that you, from a screen, call a new scanner type screen qr and when you get the result it is when you launch the await Navigation.PopAsync();

I use it in an application like this:

    private async Task LaunchQRScan()
    {
        //controlo que tipos de qr quiero usar
        var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
        options.PossibleFormats = new List<ZXing.BarcodeFormat>() {
            ZXing.BarcodeFormat.QR_CODE
        };

        //lanzo la nueva página de zxing dandole un titulo
        ZXingScannerPage scanPage = new ZXingScannerPage();
        scanPage.Title = AppResources.qrTitle;

        //espero al resultado
        scanPage.OnScanResult += (result) =>
        {
            //con resultado cancelo el escaneo activo
            scanPage.IsScanning = false;
            //llamo a una nueva función que procese el resultado
            ProcesResult(result);
        };

        //lanzo esa página de escaneo qr "encima" de la página actual
        await Application.Current.MainPage.Navigation.PushAsync(scanPage);
    }


    private void ProcesResult(ZXing.Result result)
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            //quito la página de qr
            await Application.Current.MainPage.Navigation.PopAsync();
            if (result != null)
            {
                //si result.Text es un número, hago cosas con el
                int idMuestra = 0;
                if (int.TryParse(result.Text, out idMuestra))
                {
                    //cosas  
                }
            }
        });
    }
    
answered by 11.12.2018 / 10:14
source