How to Build a .NET MAUI Barcode and QR Code Reader for Windows and Android

Dev Environment

  • Visual Studio 2022 Preview

Steps to Build Barcode and QR Code Reader in .NET MAUI

In the following paragraphs, we make a .NET MAUI app from scratch. You will see how to install different dependencies, as well as how to define cross-platform API for Windows and Android.

Step 1: Create a .NET MAUI Project

In project creation dialog, we enter maui in the search box or select MAUI from All project types to quickly find the MAUI templates.

Step 2: Install Dependencies Respectively for Windows and Android

Launch NuGet Package Manager to install Barcode and QR Code SDK:

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">
<PackageReference Include="Xamarin.Dynamsoft.Barcode.Android">
<Version>9.0.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.Contains('-windows')) == true ">
<PackageReference Include="BarcodeQRCodeSDK" Version="1.2.1" />
</ItemGroup>

Step3: Define the Cross-platform API

According to the official tutorial — Invoke platform code, we define a partial class named BarcodeQRCodeService.cs in the Services folder:

namespace BarcodeQRCode.Services
{
public partial class BarcodeQRCodeService
{
public partial void InitSDK(string license);
public partial string DecodeFile(string filePath);
}
}
using BarcodeQRCode.Platforms.Android;
using Com.Dynamsoft.Dbr;

namespace BarcodeQRCode.Services
{
public partial class BarcodeQRCodeService
{
BarcodeReader reader;

public partial void InitSDK(string license)
{
BarcodeReader.InitLicense(license, new DBRLicenseVerificationListener());
reader = new BarcodeReader();
}

public partial string DecodeFile(string filePath)
{
string decodingResult = "";

try
{

TextResult[] results = reader.DecodeFile(filePath);
if (results != null)
{
foreach (TextResult result in results)
{
decodingResult += result.BarcodeText + "\n";
}
}
else
{
decodingResult = "No barcode found.";
}
}
catch (Exception e)
{
decodingResult = e.Message;
}

return decodingResult;
}
}
}
using Dynamsoft;

namespace BarcodeQRCode.Services
{
public partial class BarcodeQRCodeService
{
BarcodeQRCodeReader? reader = null;

public partial void InitSDK(string license)
{
BarcodeQRCodeReader.InitLicense(license);

try
{
reader = BarcodeQRCodeReader.Create();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public partial string DecodeFile(string filePath)
{
if (reader == null)
return "";

string decodingResult = "";
try
{
string[]? results = reader.DecodeFile(filePath);
if (results != null)
{
foreach (string result in results)
{
decodingResult += result + "\n";
}
}
else
{
decodingResult = "No barcode found.";
}
}
catch (Exception e)
{
decodingResult = e.Message;
}

return decodingResult;
}
}
}
using Com.Dynamsoft.Dbr;

namespace BarcodeQRCode.Platforms.Android
{
public class DBRLicenseVerificationListener : Java.Lang.Object, IDBRLicenseVerificationListener
{
public void DBRLicenseVerificationCallback(bool isSuccess, Java.Lang.Exception error)
{
if (!isSuccess)
{
System.Console.WriteLine(error.Message);
}
}
}

}

Step 4: Add UI Elements

We use MAUI template to create a new content page named ReaderPage.xaml:

<?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"
x:Class="BarcodeQRCode.ReaderPage"
Title="ReaderPage">
<ScrollView>
<VerticalStackLayout>
<Button
Text="Select a file"
FontAttributes="Bold"
SemanticProperties.Hint="Decode barcode and QR code from the file"
Clicked="OnFilePickerClicked"
HorizontalOptions="Center" />
<Label
FontSize="18"
FontAttributes="Bold"
x:Name="ResultLabel"
HorizontalOptions="Center" />
<Image
x:Name="Image"
SemanticProperties.Description="Decode barcode and QR code from the image file"
WidthRequest="640"
HeightRequest="640"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
namespace BarcodeQRCode;

using BarcodeQRCode.Services;

public partial class ReaderPage : ContentPage
{
BarcodeQRCodeService _barcodeQRCodeService;

public ReaderPage()
{
InitializeComponent();

InitService();
}

private async void InitService()
{
await Task.Run(() =>
{
_barcodeQRCodeService = new BarcodeQRCodeService();

try
{
_barcodeQRCodeService.InitSDK("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==");
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "OK");
}

return Task.CompletedTask;
});
}

private async void OnFilePickerClicked(object sender, EventArgs e)
{
FileResult file;
try
{
file = await FilePicker.PickAsync(PickOptions.Images);

if (file == null) return;

FileLabel.Text = $"File picked: {file.FileName}";

Image.Source = ImageSource.FromFile(file.FullPath);

var result = _barcodeQRCodeService.DecodeFile(file.FullPath);
ResultLabel.Text = result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
<Button 
Text="Barcode/QR Code Reader"
FontAttributes="Bold"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnReaderClicked"
HorizontalOptions="Center" />
private void OnReaderClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new ReaderPage());
}
  • Android
  • Windows

Source Code

https://github.com/yushulx/dotnet-maui-barcode-qrcode-reader

--

--

Manager of Dynamsoft Open Source Projects | Tech Lover

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Xiao Ling

Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover