Integrating Android MRZ Recognition in .NET MAUI: From AAR Files to a NuGet Package to a Complete App
Tags: dotnet, android, androiddev, maui
Published: 2023-09-21 00:58:12

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](https://www.dynamsoft.com/label-recognition/downloads) and extract the `DynamsoftCore.aar` and `DynamsoftLabelRecognizer.aar` files from the zip file.
- Request a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense/?product=dlr&source=codepool) 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
1. Download Dynamsoft's [Android MRZ scanner example](https://github.com/Dynamsoft/label-recognizer-mobile-samples/tree/master/android/MRZScanner) 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](https://www.dynamsoft.com/codepool/img/2023/09/build-mrz-aar-release.png)
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](https://www.dynamsoft.com/codepool/img/2023/09/build-mrz-aar-release.png)


## 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](https://www.dynamsoft.com/codepool/img/2023/09/android-java-library-binding-mrz-sdk.png)

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

    ![aar to dotnet](https://www.dynamsoft.com/codepool/img/2023/09/build-aar-to-dotnet-library.png)

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](https://www.dynamsoft.com/codepool/img/2023/09/android-aar-nuget-package.png)


## Released NuGet Package
[https://www.nuget.org/packages/MrzSDK/](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](https://www.dynamsoft.com/codepool/img/2023/09/nuget-package-install.png)

    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:

    ```xml
    <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:

    ```csharp
    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:

    ```csharp
    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
    <?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#:

    ```csharp
    #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](https://www.dynamsoft.com/codepool/img/2023/09/dotnet-maui-android-mrz-recognition.jpg)

## Source Code
[https://github.com/yushulx/dotnet-maui-android-mrz-sdk](https://github.com/yushulx/dotnet-maui-android-mrz-sdk)