Xamarin Forms 条形码扫描器

20

我找不到任何可以使用的 Xamarin Forms 条码扫描器的源代码。是否有使用 zxing 库的可用于 Xamarin Forms 的条码扫描器示例?

4个回答

20

您可以尝试使用下面的代码。 将zxing库/组件添加到解决方案中的所有项目

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

1
它有效。我如何使用我的自定义视图来捕获条形码? - Aneesh.A.M
1
在https://components.xamarin.com/gettingstarted/zxing.net.mobile.forms上搜索Custom Overlays。我不知道Custom Views,但是使用这个Custom overlays,你可以添加任何你想要的东西。 - Akash Amin
你能给一个例子吗? - Aneesh.A.M
抱歉,我没有这方面的样本。但是它在链接中,您可以从那里尝试。 - Akash Amin
我已经通过NuGet下载了ZXing。目前我使用的是版本0.16.4。其中未定义MobileBarcodeScanner。我在包中找不到它。哪个类替代了它? - Makuna
使用ZXing.Net.Mobile库,makuna。 - Gianluca Musa

4
你可以使用 ZXing.Net.Mobile.Forms。但是要注意。
  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);
    }
    

安装 ZXing.Net.Mobile 而不仅仅是 ZXing.Net.Mobile.Forms 可以修复 v2.4.1 的构建错误。 - dvjanm

4
您可以使用 ZXing.Net.Mobile nuget。该库可在以下 URL 的 github 上获得:https://github.com/Redth/ZXing.Net.Mobile。您将在第一页找到使用文档。但是我将简要地用以下3个步骤来解释它:
  1. Add the nuget to your project

  2. Create a ContentPage. In the xaml side, create a button or an image button. In the following exemple, I'm using like an image button like this :

    <ImageButton  x:Name="ScanButton"  Source="scannimage.png"  />
    
  3. In the code bind, put the following code in the constructor or in the OnAppearing() method:

    ScanButton.Clicked += async (sender, e) =>
    {
        var scanner = new ZXing.Mobile.MobileBarcodeScanner();
        var result = await scanner.Scan();
    
        if (result != null)
        {
            await DisplayAlert("Code barre", "Scanned Barcode: " + result.Text, "Ok");
        }
    };
    

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接