DEV Community

Cover image for Build a Cross-Platform QR Code Scanner in .NET MAUI using ZXing.Net.MAUI
supriya shah
supriya shah

Posted on

Build a Cross-Platform QR Code Scanner in .NET MAUI using ZXing.Net.MAUI

Originally published on Medium:
https://medium.com/@supsabhi/build-a-cross-platform-qr-code-scanner-in-net-maui-using-zxing-net-maui-5638b479372d

A step-by-step beginner’s guide to integrating QR and barcode scanning easily in your MAUI app
If you want to build a cross-platform app with QR code scanning in .NET MAUI, there are several ways to do it. You can use libraries like ZXing.Net.Maui.Controls, Camera.MAUI, IronQR, or Google Vision ML Kit.

Each has its own strengths:

  1. - ZXing is open-source and widely adopted.
  2. - IronQR offers a simple and commercial-ready cross-platform library.
  3. - Google Vision ML Kit provides a machine learning–based alternative.

But today, we’ll focus on ZXing.Net.MAUI, one of the most popular and developer-friendly options.

In this guide, you’ll learn everything you need to integrate ZXing QR Code Scanner in .NET MAUI — including installation, initialization, permissions, and a working example with XAML and C# code.
We’ll also cover useful options like torch and scan area, common issues, and testing tips.

🧩 Why Choose ZXing for .NET MAUI?
There are plenty of reasons developers prefer ZXing.Net.MAUI when adding barcode or QR code scanning features to their apps:

  1. Open-source and widely used — trusted by many developers.
  2. Supports multiple barcode types — QR, EAN, Code128, PDF417, and more.
  3. Works across platforms — Android, iOS, and Windows (WinUI).
  4. Includes both live camera scanning and image decoding.
  5. Easy to integrate and customize — choose barcode formats, enable torch, or adjust detection options.
  6. Actively maintained and community-supported, ensuring stability and updates.

⚙️ 1. Prerequisites
Before diving in, make sure you have the following ready:

✅ The latest .NET SDK *(recommended *.NET 8)
Visual Studio 2022 or later with the Mobile development with .NET workload installed
✅ A working .NET MAUI project

If you’re new to MAUI, open Visual Studio → Create New Project → choose .NET MAUI App.

📦 2.Installation
Step 1: Install the ZXing.Net.MAUI NuGet Package
Think of a NuGet package as a toolbox that adds new features to your app.
To enable QR scanning, install the ZXing.Net.Maui.Controls package.

Option 1 — Using CLI:

dotnet add package ZXing.Net.Maui.Controls --version 0.4.0
Enter fullscreen mode Exit fullscreen mode

Option 2 — Using Visual Studio:

  1. Right-click on your .NET MAUI project in Solution Explorer.
  2. Select Manage NuGet Packages
  3. Go to the Browse tab and search for ZXing.Net.Maui.Controls.
  4. Click Install.
  5. This gives you access to ready-to-use UI controls such as the camera scanner view.

Step 2: Initialize ZXing in MauiProgram.cs

Your MAUI app needs to know that ZXing is part of the project. You do this by registering the barcode reader service in MauiProgram.cs.


using ZXing.Net.Maui;
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseBarcodeReader(); // register ZXing
        return builder.Build();
    }
Enter fullscreen mode Exit fullscreen mode

💡 Note: Depending on the package version, you may need to use
.UseZXingNetMaui() instead of .UseBarcodeReader().

Step 3: Add Camera Permissions

Since scanning requires camera access, your app must request permissions or it’ll show a black screen.

🔹 Android

Edit Platforms/Android/AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />

Enter fullscreen mode Exit fullscreen mode

For Android 6.0 (API 23) and above, request permission at runtime:

var status = await Permissions.RequestAsync<Permissions.Camera>();
if (status != PermissionStatus.Granted)
{
    // Handle permission denied
}
Enter fullscreen mode Exit fullscreen mode

🔹 iOS

Open Platforms/iOS/Info.plist and add:

<key>NSCameraUsageDescription</key>
<string>This app uses the camera to scan QR codes.</string>
Enter fullscreen mode Exit fullscreen mode

If you plan to scan images from the gallery, also add:

<key>NSPhotoLibraryUsageDescription</key>
<string>This app accesses your photo library to scan QR codes from images.</string>
Enter fullscreen mode Exit fullscreen mode

🔹 Windows (WinUI)

Edit Package.appxmanifest and enable Webcam under Capabilities.

🎥 3. Create the Scanner UI in XAML

Now let’s design the UI. ZXing provides a CameraBarcodeReaderView control for real-time scanning.

Open your XAML page (e.g., ScannerPage.xaml) and add:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:zxing="clr-namespace:ZXing.Net.Maui;assembly=ZXing.Net.MAUI"
    x:Class="MyMauiApp.Pages.ScannerPage">
<Grid RowDefinitions="*,Auto" Padding="10">
        <!-- Camera scanner view -->
        <zxing:CameraBarcodeReaderView 
                        x:Name="cameraView"
                        BarcodesDetected="CameraView_BarcodeDetected"
                        IsScanning="True"
                        IsTorchOn="False"
                        IsDetecting="True"
                        VerticalOptions="FillAndExpand" 
                  />
        <!-- Result Label -->
            <StackLayout Grid.Row="1" 
             Orientation="Horizontal" 
              HorizontalOptions="Center">
            <Label x:Name="ResultLabel"
            Text="No code scanned yet" 
            VerticalOptions="Center" />
        </StackLayout>
    </Grid>
</ContentPage>
Enter fullscreen mode Exit fullscreen mode
  1. x:Name="cameraView" → gives a name to access it in C#.
  2. IsDetecting="True" → enables live scanning.
  3. BarcodesDetected="CameraView_BarcodeDetected" → triggers when a QR code is found.

💻 4. Handle Scanned Results in Code-Behind

Open the code-behind file (ScannerPage.xaml.cs) and implement your scan logic.

using ZXing.Net.Maui;
using ZXing.Net.Maui.Controls;
public partial class ScannerPage : ContentPage
{
    bool _isProcessing = false;
    public ScannerPage()
    {
        InitializeComponent();
    }
    private void CameraView_BarcodeDetected(object sender, BarcodeDetectionEventArgs e)
    {
        var result = e.Results?.FirstOrDefault();
        if (result == null) return;
        if (_isProcessing) return;
        _isProcessing = true;
        MainThread.BeginInvokeOnMainThread(async () =>
        {
            cameraView.IsDetecting = false;
            ResultLabel.Text = result.Value ?? "Empty";
            await DisplayAlert("Scanned", result.Value, "OK");
            // Resume scanning if needed
            cameraView.IsDetecting = true;
            _isProcessing = false;
        });
    }
    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        cameraView.IsDetecting = false;
    }
}
Enter fullscreen mode Exit fullscreen mode

🛠️ 5. Common Problems & Fixes

Black Screen or Blank Preview

  1. Usually caused by missing camera permission.
  2. Always check and request permissions at runtime.

No Barcode Detected

  1. Check lighting conditions or camera focus.
  2. Move the device closer to the QR code.
  3. Ensure IsDetecting="True" is set.

⚡ Optimize Scanning Performance

  1. Restrict formats for faster detection:
cameraView.Options = new BarcodeReaderOptions {   
  Formats = BarcodeFormats.QrCode // or BarcodeFormats.All 
};
Enter fullscreen mode Exit fullscreen mode

2.Use TryHarder = true for difficult lighting conditions.

🧠 Final Thoughts

That’s it! You’ve now integrated a fully functional QR Code Scanner in .NET MAUI using ZXing.Net.MAUI.

ZXing is open-source, cross-platform, and easy to set up, making it an excellent choice for most use cases. With just a few steps — installing the package, setting up permissions, and adding the XAML and C# logic — your MAUI app can now read QR codes and barcodes in real time.

Whether you’re building a kiosk, attendance app, or mobile tool, ZXing.Net.MAUI gives you the flexibility and reliability you need.

Top comments (0)