Xamarin形成条形码扫描仪

时间:2016-05-24 06:50:04

标签: xamarin xamarin.forms zxing barcode-scanner

我找不到任何Xamarin表格条码扫描器的工作来源。是否有使用zxing库的Xamarin形式条码扫描器的工作样本?

4 个答案:

答案 0 :(得分:13)

您可以尝试以下代码。 将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 :(得分:0)

这很直接,

var scanner = new MobileBarcodeScanner();
var result = await scanner.Scan();

if(result == null) 
   return;

results.Text = result.Text;

以下是使用XZing库扫描条形码和QR码的XamarinForms的工作示例。

示例代码:https://github.com/hnabbasi/CheckInator

演示:https://www.youtube.com/watch?v=OdPinaBLbvE

答案 2 :(得分:0)

您可以使用https://github.com/rails/rails/issues/31656。但是你要注意。

  1. ZXing.Net.Mobile.Forms的当前版本是2.4.1。我使用了此版本,并且在Xamarin.Forms.Android项目上构建失败。 => 崩溃应用

    =>我使用的版本是 2.3.2 。工作正常。

  2. 在Android项目文件MainActivity.cs中,添加以下代码:

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

    似乎ZXing.Net.Mobile.Forms上的教程代码不正确

  3. 呼叫扫描器:

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

答案 3 :(得分:0)

您可以使用ZXing.Net.Mobile nuget。该库位于github中的以下URL https://github.com/Redth/ZXing.Net.Mobile中。您将在第一页中找到用法文档。但是,我将分三步简单地解释一下:

  1. 将nuget添加到您的项目中

  2. 创建一个ContentPage。在xaml一侧,创建一个按钮或一个图像按钮。在以下示例中,我像这样使用图像按钮:

    <ImageButton  x:Name="ScanButton"  Source="scannimage.png"  />
    
  3. 在代码绑定中,将以下代码放入构造函数或OnAppearing()方法中:

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