xamarin.forms 使用ZXing扫描二维码

1. Android工程包含ZXing.Net.Mobile.Forms

xamarin.forms 使用ZXing扫描二维码_第1张图片

 

2. 在C# 工程中, 包含ZXing.Net.Mobile

xamarin.forms 使用ZXing扫描二维码_第2张图片

 

3. 在Android工程的MainActivity.cs中, 添加下面代码:

global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

ZXing.Mobile.MobileBarcodeScanner.Initialize(this.Application);

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace TestScan.Droid
{
    [Activity(Label = "TestScan", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

            ZXing.Mobile.MobileBarcodeScanner.Initialize(this.Application);
            LoadApplication(new App());
        }
    }
}

 

4. 在C#页面工程中就可以这样调用扫码功能了:

private async void Button_Clicked(object sender, EventArgs e)
{    
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    var result = await scanner.Scan();
    if (result != null)
        Console.WriteLine("Scanned Barcode: " + result.Text);
}

xamarin.forms 使用ZXing扫描二维码_第3张图片

 

demo下载地址

 

你可能感兴趣的:(Xamarin,Android)