Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms Barcode Scanner

I couldn't find any working source of Xamarin forms barcode scanner. Is there any working sample of Xamarin forms barcode scanner using zxing library?

like image 300
Aneesh.A.M Avatar asked May 24 '16 06:05

Aneesh.A.M


2 Answers

You can try the code below. Add the zxing library/component to all the projects in the solution

public class Home : ContentPage
{
    string message = "";
    public Home()
    {
        //Intialize the button
        Button btnScan = new Button
        {
            Text = "Start Scan",
            BackgroundColor = Color.FromRgb(207, 197, 159),
            TextColor = Color.White,
            BorderRadius = 5,
            TranslationY = 120
        };
        //Attach the click event
        btnScan.Clicked += btnScan_Clicked;

        this.Content = new StackLayout
        {
            BackgroundColor = Color.FromRgb(150, 172, 135),
            Spacing = 10,
            Padding = 25,
            Children =
            {
                btnScan
            }
        };
    }

    async void btnScan_Clicked(object sender, EventArgs e)
    {
        var scanner = new MobileBarcodeScanner();
        scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
        scanner.BottomText = "Wait for the barcode to automatically scan!";

        //This will start scanning
        ZXing.Result result = await scanner.Scan();

        //Show the result returned.
        HandleResult(result);
    }

    void HandleResult(ZXing.Result result)
    {
        var msg = "No Barcode!";
        if (result != null)
        {
            msg = "Barcode: " + result.Text + " (" + result.BarcodeFormat + ")";
        }

        DisplayAlert("", msg, "Ok");
    }
}
like image 157
Akash Amin Avatar answered Oct 17 '22 09:10

Akash Amin


You can use ZXing.Net.Mobile.Forms. But you attention.

  1. ZXing.Net.Mobile.Forms current version is 2.4.1. I used this version and build failed on Xamarin.Forms.Android project. => Crash App.

    => I used version 2.3.2. It working fine.

  2. In Android project file MainActivity.cs, you add following code:

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    

    It seems, the tutorial code on here is incorrect

  3. Call scanner:

    private async void BtnScan_OnClicked(object sender, EventArgs e)
    {
        ZXingScannerPage scanPage = new ZXingScannerPage();
        scanPage.OnScanResult += (result) =>
        {
            scanPage.IsScanning = false;
            Device.BeginInvokeOnMainThread(() =>
            {
                Navigation.PopAsync();
                EtInputCode.Text = "Code: " + result.Text;
            });
        };
        await Navigation.PushAsync(scanPage);
    }
    
like image 27
Bình Nguyễn Quang Avatar answered Oct 17 '22 09:10

Bình Nguyễn Quang