Integrating Android MRZ Recognition in .NET MAUI: From AAR Files to a NuGet Package to a Complete App

Xiao Ling - Sep 21 '23 - - Dev Community

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

Tools

  • Android Studio
  • Visual Studio 2022

Building Android MRZ SDK to an AAR File

  1. Download Dynamsoft's Android MRZ scanner example from GitHub.
  2. Open the project in Android Studio.
  3. Click the Build Variants tab on the left side of the window and select release from the dropdown menu of MRZLib.

    Android Studio Build Variants

  4. Select the MRZLib module and click Build > Make Module 'MRZScanner.MRZLib' to build the MRZLib-release.aar file, which will be located in the MRZScanner/MRZLib/build/outputs/aar folder.

    Android Studio Build aar

Creating a NuGet Package from AAR Files for .NET MAUI Android Development

  1. In Visual Studio, select a new project and choose the Android Java Library Binding template

    android java library binding

  2. Drag the DynamsoftCore.aar, DynamsoftLabelRecognizer.aar and MRZLib-release.aar files into the project.

    aar to dotnet

  3. Change the build configuration to Release, then right-click the project and select Pack to create a NuGet package. The package will be located in the bin/Release folder.

    android aar in nuget package

Released NuGet Package

https://www.nuget.org/packages/MrzSDK/

Integration MRZ Recognition into .NET MAUI Android App

  1. Create a new .NET MAUI project in Visual Studio.
  2. Install the MrzSDK NuGet package via the NuGet Package Manager.

    install mrz sdk nuget package

    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>
    
  3. 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());
        }
    }
    
  4. 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}");
        }
    }
    
  5. 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.

  6. Connect an Android device to your computer and then run the app.

    .NET MAUI Android MRZ recognition

Source Code

https://github.com/yushulx/dotnet-mrz-sdk/tree/main/android

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player