ZXing.Net.Mobile

repository·master·Indexed 22 days ago

https://github.com/redth/zxing.net.mobile

A C#/.NET library providing barcode scanning capabilities for mobile platforms including Xamarin.iOS, Xamarin.Android, Tizen, and UWP, based on the ZXing (Zebra Crossing) library. It features the MobileBarcodeScanner class for simple implementation, support for custom scanning overlays, and the ability to use Apple's native AVCaptureSession engine on iOS.

Tokens
1.7K
Snippets
5
Records
6
Agent score
28%

What's inside ZXing.Net.Mobile

  1. Customize the scanning overlay

    master

    By default, the library provides a simple overlay with a horizontal red line and semi-transparent borders. To use a custom UI, you must provide a platform-specific view and set UseCustomOverlay to true.

    Note: When using a custom overlay, you are responsible for the entire overlay; you cannot mix custom elements with the default one. The type of the CustomOverlay property varies by platform:

    • Xamarin.iOS: UIView
    • Xamarin.Android: View
    • UWP: UIElement
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    scanner.UseCustomOverlay = true;
    scanner.CustomOverlay = myCustomOverlayInstance;
    var result = await scanner.Scan();
    var scanner = new ZXing.Mobile.MobileBarcodeScanner();
    scanner.UseCustomOverlay = true;
    scanner.CustomOverlay = myCustomOverlayInstance;
    var result = await scanner.Scan();
    //Handle result
  2. Use reusable scanner components (View/Fragment/Control)

    master

    Instead of using the MobileBarcodeScanner class, you can use platform-specific reusable components directly in your UI. These components provide methods like ToggleTorch() and StopScanning(), but you are responsible for calling StartScanning(...) with a callback and MobileBarcodeScanningOptions when you want scanning to begin.

    Platform Components:

    • iOS: ZXingScannerView (UIView)
    • iOS (Apple Engine): AVCaptureScannerView (UIView) — Uses Apple's AVCaptureSession metadata engine instead of ZXing.Net.
    • Android: ZXingScannerFragment (Fragment)
    • UWP: ZXingScannerControl (UserControl)
  3. Quickstart: Scan a barcode with MobileBarcodeScanner

    master

    The simplest way to implement barcode scanning is using the MobileBarcodeScanner class. On Android, you must initialize the scanner with the Application instance first to ensure it can track the current context.

    buttonScan.Click += async (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);
    };
    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);
    };
  4. Setup ZXing.Net.Mobile for Xamarin.Forms

    master

    To use ZXing.Net.Mobile with Xamarin.Forms, you must initialize the library on each platform in your platform-specific project.

    Android

    In your main Activity's OnCreate implementation, call:

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

    To handle permission requests, override OnRequestPermissionsResult in your main Activity:

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

    If you need to use the Flashlight API, add this assembly-level attribute to your AndroidManifest.xml:

    [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, add:

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

    Tip: If navigation or UI updates fail after scanning, set NavigationCacheMode="Enabled" in your Page's XAML <Page ... /> element.

    macOS

    In your AppDelegate's FinishedLaunching implementation, call:

    ZXing.Net.Mobile.Forms.MacOS.Platform.Init();
    // Android Activity OnCreate
    Xamarin.Essentials.Platform.Init(Application);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
    
    // iOS AppDelegate FinishedLaunching
    ZXing.Net.Mobile.Forms.iOS.Platform.Init();
    
    // UWP Page Constructor
    ZXing.Net.Mobile.Forms.WindowsUniversal.Platform.Init();
  5. Restrict scanning to specific barcode formats

    master

    By default, all barcode formats are monitored. To improve performance or accuracy, you can pass a ZXing.Mobile.MobileBarcodeScanningOptions instance to the Scan method to restrict detection to specific formats.

    #if __ANDROID__
    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);
    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);
  6. Use Apple's AVCaptureSession engine on iOS

    master

    On iOS, you can opt to use Apple's native AVCaptureSession metadata engine for scanning instead of the ZXing engine. This can be done via the AVCaptureScannerView/AVCaptureScannerViewController classes or by passing true to the useAVCaptureEngine parameter in the MobileBarcodeScanner.Scan method.

    // Scan using the AV Capture Engine
    // Scan(MobileBarcodeScanningOptions options, bool useAVCaptureEngine)
    scanner.Scan(options, true);

    Supported formats for AVCaptureSession:

    • Aztec, Code 128, Code 39, Code 93, EAN13, EAN8, PDF417, QR, UPC-E

    Note: The library will gracefully degrade to the ZXing engine if the device does not support AVCaptureSession (pre-iOS7) or if you request a format not supported by the Apple engine.

    //Scan(MobileBarcodeScanningOptions options, bool useAVCaptureEngine)
    scanner.Scan(options, true);