Machine-Readable Zone (MRZ) recognition is an essential technology in various sectors, including security, immigration, and automation. Its application in Android software can enhance a wide array of services by enabling quick and accurate data capture from identification documents like passports and ID cards. This article aims to guide you through the entire process of integrating MRZ recognition into a .NET MAUI Android application. Starting with Android Archive (AAR) files, we'll journey through packaging these into a NuGet library and ultimately deploying them in a .NET MAUI app.
Prerequisites
- Download Dynamsoft Label Recognizer for Android and extract the
DynamsoftCore.aar
andDynamsoftLabelRecognizer.aar
files from the zip file. - Request a 30-day FREE trial license for Dynamsoft Label Recognizer and save the license key for later use.
Tools
- Android Studio
- Visual Studio 2022
Building Android MRZ SDK to an AAR File
- Download Dynamsoft's Android MRZ scanner example from GitHub.
- Open the project in Android Studio.
-
Click the
Build Variants
tab on the left side of the window and selectrelease
from the dropdown menu ofMRZLib
. -
Select the
MRZLib
module and clickBuild
>Make Module 'MRZScanner.MRZLib'
to build theMRZLib-release.aar
file, which will be located in theMRZScanner/MRZLib/build/outputs/aar
folder.
Creating a NuGet Package from AAR Files for .NET MAUI Android Development
-
In Visual Studio, select a new project and choose the
Android Java Library Binding
template -
Drag the
DynamsoftCore.aar
,DynamsoftLabelRecognizer.aar
andMRZLib-release.aar
files into the project. -
Change the build configuration to
Release
, then right-click the project and selectPack
to create a NuGet package. The package will be located in thebin/Release
folder.
Released NuGet Package
https://www.nuget.org/packages/MrzSDK/
Integration MRZ Recognition into .NET MAUI Android App
- Create a new .NET MAUI project in Visual Studio.
-
Install the
MrzSDK
NuGet package via the NuGet Package Manager.Note: An .NET MAUI project scaffolds for multiple platforms, including Android, iOS, macOS, and Windows. The
MrzSDK
NuGet package is only available for Android. Thus. it is conditionally referenced in the.csproj
file as follows:
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'"> <PackageReference Include="MrzSDK"> <Version>0.1.2</Version> </PackageReference> </ItemGroup>
-
Add the following code to the
Platforms/Android/MainActivity.cs
file to activate the Dynamsoft Label Recognizer license:
using Com.Dynamsoft.Core; public class MainActivity : MauiAppCompatActivity { public class LicenseVerificationListener : Java.Lang.Object, ILicenseVerificationListener { public void LicenseVerificationCallback(bool isSuccess, CoreException error) { if (!isSuccess) { System.Console.WriteLine(error); } } } protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); LicenseManager.InitLicense("LICENSE-KEY", this, new LicenseVerificationListener()); } }
-
In
MainPage.xaml
, create a button event handler to pick an image file from photo gallery and return the path of the image file:
async Task<string> LoadPhotoAsync(FileResult photo) { if (photo == null) { return ""; } var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName); using (var stream = await photo.OpenReadAsync()) using (var newStream = File.OpenWrite(newFile)) await stream.CopyToAsync(newStream); return newFile; } private async void OnCounterClicked(object sender, EventArgs e) { try { FileResult photo = await FilePicker.PickAsync(); String photoPath = await LoadPhotoAsync(photo); Console.WriteLine($"CapturePhotoAsync COMPLETED: {photoPath}"); if (photoPath.Equals("")) { return; } await Navigation.PushAsync(new ImagePage(photoPath)); } catch (FeatureNotSupportedException fnsEx) { } catch (PermissionException pEx) { } catch (Exception ex) { Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}"); } }
-
Create a new .NET MAUI content page
ImagePage.xaml
for recognizing MRZ from the image file: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="MauiAndroidMrz.ImagePage" Title="ImagePage"> <ScrollView> <Grid> <Image x:Name="Image" /> <Label x:Name="Result" TextColor="Red" Padding="10,20,10,20"/> </Grid> </ScrollView> </ContentPage>
C#:
#if ANDROID using Com.Dynamsoft.Dlr; #endif namespace MauiAndroidMrz; public partial class ImagePage : ContentPage { #if ANDROID MRZRecognizer recognizer; #endif public ImagePage(string imagepath) { InitializeComponent(); Image.Source = imagepath; #if ANDROID recognizer = new MRZRecognizer(); MRZResult? mrsResult = recognizer.RecognizeMRZFromFile(imagepath); if (mrsResult != null) { string mrzInfo = ""; mrzInfo += "DocId: " + mrsResult.DocId + '\n'; mrzInfo += "DocType: " + mrsResult.DocType + '\n'; mrzInfo += "Nationality: " + mrsResult.Nationality + '\n'; mrzInfo += "Issuer: " + mrsResult.Issuer + '\n'; mrzInfo += "Birth: " + mrsResult.DateOfBirth + '\n'; mrzInfo += "Expiration: " + mrsResult.DateOfExpiration + '\n'; mrzInfo += "Gender: " + mrsResult.Gender + '\n'; mrzInfo += "Surname: " + mrsResult.Surname + '\n'; mrzInfo += "GivenName: " + mrsResult.GivenName + '\n'; mrzInfo += "IsParsed: " + mrsResult.IsParsed + '\n'; mrzInfo += "IsVerified: " + mrsResult.IsVerified + '\n'; Result.Text = mrzInfo; } #endif } }
Since the code for MRZ recognition is only available for Android, we need to add the
#if ANDROID
and#endif
preprocessor directives to the code block. -
Connect an Android device to your computer and then run the app.
Top comments (0)