QRCode-ZXing.Net.Mobile,Maui QRCode

[删除(380066935@qq.com或微信通知)]

Redth/ZXing.Net.Mobile: Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen (github.com)

ZXing.Net.Mobile

ZXing.Net.Mobile is a C#/.NET library based on the open source Barcode Library: ZXing (Zebra Crossing), using the ZXing.Net Port. It works with Xamarin.iOS, Xamarin.Android, Tizen, and UWP. The goal of ZXing.Net.Mobile is to make scanning barcodes as effortless and painless as possible in your own applications.

Build Status NuGet NuGet

Usage

The simplest example of using ZXing.Net.Mobile looks something like this:

buttonScan.Click += (sender, e) => {

	#if __ANDROID__
	// Initialize the scanner first so it can track the current context
	MobileBarcodeScanner.Initialize (Application);
  	#endif
  	
	var scanner = new ZXing.Mobile.MobileBarcodeScanner();

	var result = await scanner.Scan();

	if (result != null)
		Console.WriteLine("Scanned Barcode: " + result.Text);
};

Xamarin Forms

For Xamarin Forms there is a bit more setup needed. You will need to initialize the library on each platform in your platform specific app project.

Android

On Android, in your main Activity's OnCreate (..) implementation, call:

Xamarin.Essentials.Platform.Init(Application);
ZXing.Net.Mobile.Forms.Android.Platform.Init();

ZXing.Net.Mobile for Xamarin.Forms also handles the new Android permission request model for you via Xamarin.Essentials, but you will need to add the following override implementation to your main Activity as well:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

The Camera permission should be automatically included for you in the AndroidManifest.xml however if you would like to use the Flashlight API you will still need to add the Flashlight permission yourself. You can do this by using the following assembly level attribute:

[assembly: UsesPermission (Android.Manifest.Permission.Flashlight)]
iOS

In your AppDelegate's FinishedLaunching (..) implementation, call:

ZXing.Net.Mobile.Forms.iOS.Platform.Init();
Windows Universal UWP

In your main Page's constructor, you should add:

ZXing.Net.Mobile.Forms.WindowsUniversal.Platform.Init();

If you notice that finishing scanning or pressing the back button is causing your Page to jump back further than you'd like, or if you're having trouble updating the UI of a Page after scanning is completed, you may need to set NavigationCacheMode="Enabled" within your Page's XAML <Page ... /> element.

macOS

In your AppDelegate's FinishedLaunching (..) implementation, call:

ZXing.Net.Mobile.Forms.MacOS.Platform.Init();

Features

  • Xamarin.iOS
  • Xamarin.Android
  • Tizen
  • UWP
  • Xamarin.Mac (rendering only, not scanning)
  • Simple API - Scan in as little as 2 lines of code!
  • Scanner as a View - UIView (iOS) / Fragment (Android) / Control (WP)

Custom Overlays

By default, ZXing.Net.Mobile provides a very simple overlay for your barcode scanning interface. This overlay consists of a horizontal red line centered in the scanning 'window' and semi-transparent borders on the top and bottom of the non-scanning area. You also have the opportunity to customize the top and bottom text that appears in this overlay.

If you want to customize the overlay, you must create your own View for each platform. You can customize your overlay like this:

var scanner = new ZXing.Mobile.MobileBarcodeScanner();
scanner.UseCustomOverlay = true;
scanner.CustomOverlay = myCustomOverlayInstance;
var result = await scanner.Scan();
//Handle result

Keep in mind that when using a Custom Overlay, you are responsible for the entire overlay (you cannot mix and match custom elements with the default overlay). The ZxingScanner instance has a CustomOverlay property, however on each platform this property is of a different type:

  • Xamarin.iOS => UIView
  • Xamarin.Android => View
  • UWP => UIElement

All of the platform samples have examples of custom overlays.

Barcode Formats

By default, all barcode formats are monitored while scanning. You can change which formats to check for by passing a ZxingScanningOptions instance into the StartScanning method:

//NOTE: On Android you should call the initialize method with an application instance
#if __ANDROID__
// Initialize the scanner first so it can track the current context
MobileBarcodeScanner.Initialize (Application);
#endif

var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
options.PossibleFormats = new List<ZXing.BarcodeFormat>() { 
    ZXing.BarcodeFormat.Ean8, ZXing.BarcodeFormat.Ean13 
};

var scanner = new ZXing.Mobile.MobileBarcodeScanner(); 
var result = await scanner.Scan(options);
//Handle result