How to Perform OCR on Android in .NET MAUI

by Chaknith Bin

Android related to How to Perform OCR on Android in .NET MAUI

.NET MAUI (Multi-platform App UI) is an evolution of the Xamarin.Forms framework, designed to create cross-platform apps for Android, iOS, macOS, and Windows using .NET. .NET MAUI aims to simplify the process of building native user interfaces that can run on multiple platforms.

The IronOcr.Android package brings OCR support to Android!!

IronOCR Android Package

The IronOcr.Android package enables OCR features on Android devices via .NET cross-platform projects. The vanilla IronOCR package is not needed.

PM > Install-Package IronOcr.Android
C# NuGet Library for PDF

Install with NuGet

Install-Package IronOcr.Android

Create a .NET MAUI project

Open Visual Studio and click on "Create a new project". Search for MAUI, select .NET MAUI App and "Next".

Create .NET MAUI App project

Include the IronOCR.Android library

The library can be added in various ways. The easiest is perhaps by using NuGet.

  1. Inside Visual Studio, right-click on "Dependencies" and select "Manage NuGet Packages ...".
  2. Select the "Browse" tab and search for "IronOcr.Android".
  3. Select the "IronOcr.Android" package and click on "Install".

Download IronOcr.Android package

To prevent issues with other platforms, modify the csproj file to only include the package when targeting the Android platform. In order to do so:

  1. Right-click on the project and select "Edit Project File".
  2. Create a new ItemGroup element as such:
    <ItemGroup Condition="$(TargetFramework.Contains('android')) == true">
    </ItemGroup>
    XML
  3. Move the "IronOcr.Android" PackageReference inside the ItemGroup we just created.

The above steps will prevent the "IronOcr.Android" package from being used on e.g. iOS platforms (for that purpose, install IronOcr.iOS instead).

Edit "MainActivity.cs"

  • Open the "MainActivity.cs" file by navigating to Platforms -> Android.
  • Add the MainActivity method and invoke the Initialize method.
namespace MAUIIronOCRAndroidSample
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity
    {
        public MainActivity()
        {
            IronTesseract.Initialize(this);
        }
    }
}
namespace MAUIIronOCRAndroidSample
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity
    {
        public MainActivity()
        {
            IronTesseract.Initialize(this);
        }
    }
}
Namespace MAUIIronOCRAndroidSample
	<Activity(Theme := "@style/Maui.SplashTheme", MainLauncher := True, ConfigurationChanges := ConfigChanges.ScreenSize Or ConfigChanges.Orientation Or ConfigChanges.UiMode Or ConfigChanges.ScreenLayout Or ConfigChanges.SmallestScreenSize Or ConfigChanges.Density)>
	Public Class MainActivity
		Inherits MauiAppCompatActivity

		Public Sub New()
			IronTesseract.Initialize(Me)
		End Sub
	End Class
End Namespace
VB   C#

Edit "MainPage.xaml"

Edit the XAML file to display a button and a label to show the OCR result. For example:

<?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="MAUIIronOCRAndroidSample.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button
            Text="Import File"
            Clicked="ReadFileOnImport"
            Grid.Row="0"
            HorizontalOptions="Center"
            Margin="20, 20, 20, 10"/>

        <ScrollView
            Grid.Row="1"
            BackgroundColor="LightGray"
            Padding="10"
            Margin="10, 10, 10, 30">
            <Label x:Name="OutputText"/>
        </ScrollView>
    </Grid>

</ContentPage>
XML

Edit "MainPage.xaml.cs"

First, create an instance of the IronTesseract object. Ensure that IronTesseract is initialized once within a class, as demonstrated in the code below. Instantiating it within a method can be ineffective and may cause unexpected errors.

Next, use the FilePicker.PickAsync method to select a file, then open a reading stream from the FileResult. Create a new OcrInput object and load the image into it. Perform OCR on the image using the tesseract instance and retrieve the text. Finally, display the resulting text in a label.

Please note, the current implementation only supports image files. The package does not yet support PDF documents. Therefore, any PDF-related configurations are deactivated by default and should remain so.

using IronOcr;

namespace MAUIIronOCRAndroidSample;

public partial class MainPage : ContentPage
{
    // Initialize IronTesseract once in a class
    private IronTesseract ocrTesseract = new IronTesseract();

    public MainPage()
    {
        InitializeComponent();
        // Apply License key
        IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01";
    }

    private async void ReadFileOnImport(object sender, EventArgs e)
    {
        try
        {
            var options = new PickOptions
            {
                PickerTitle = "Please select a file"
            };
            var result = await FilePicker.PickAsync(options);
            if (result != null)
            {
                using var stream = await result.OpenReadAsync();
                // Instantiate OcrInput
                using var ocrInput = new OcrInput();
                // Load image stream
                ocrInput.LoadImage(stream);
                // Perform OCR
                var ocrResult = ocrTesseract.Read(ocrInput);
                OutputText.Text = ocrResult.Text;
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            System.Diagnostics.Debug.WriteLine(ex);
        }
    }
}
using IronOcr;

namespace MAUIIronOCRAndroidSample;

public partial class MainPage : ContentPage
{
    // Initialize IronTesseract once in a class
    private IronTesseract ocrTesseract = new IronTesseract();

    public MainPage()
    {
        InitializeComponent();
        // Apply License key
        IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01";
    }

    private async void ReadFileOnImport(object sender, EventArgs e)
    {
        try
        {
            var options = new PickOptions
            {
                PickerTitle = "Please select a file"
            };
            var result = await FilePicker.PickAsync(options);
            if (result != null)
            {
                using var stream = await result.OpenReadAsync();
                // Instantiate OcrInput
                using var ocrInput = new OcrInput();
                // Load image stream
                ocrInput.LoadImage(stream);
                // Perform OCR
                var ocrResult = ocrTesseract.Read(ocrInput);
                OutputText.Text = ocrResult.Text;
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            System.Diagnostics.Debug.WriteLine(ex);
        }
    }
}
Imports IronOcr

Namespace MAUIIronOCRAndroidSample

	Partial Public Class MainPage
		Inherits ContentPage

		' Initialize IronTesseract once in a class
		Private ocrTesseract As New IronTesseract()

		Public Sub New()
			InitializeComponent()
			' Apply License key
			IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01"
		End Sub

		Private Async Sub ReadFileOnImport(ByVal sender As Object, ByVal e As EventArgs)
			Try
				Dim options = New PickOptions With {.PickerTitle = "Please select a file"}
				Dim result = Await FilePicker.PickAsync(options)
				If result IsNot Nothing Then
					Dim stream = Await result.OpenReadAsync()
					' Instantiate OcrInput
					Dim ocrInput As New OcrInput()
					' Load image stream
					ocrInput.LoadImage(stream)
					' Perform OCR
					Dim ocrResult = ocrTesseract.Read(ocrInput)
					OutputText.Text = ocrResult.Text
				End If
			Catch ex As Exception
				' Handle exceptions
				System.Diagnostics.Debug.WriteLine(ex)
			End Try
		End Sub
	End Class
End Namespace
VB   C#

Lastly, in the .csproj file, make sure that you are only building the project for Android. Since the package we added is only for Android, building the project for all platforms will fail.

Run the Project

This will show you how to run the project and perform OCR.

Execute .NET MAUI App project

Download .NET MAUI App Project

You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as a .NET MAUI App project.

Click here to download the project.

Using IronOcr.Android in Avalonia

Similar to MAUI, IronOcr.Android can be used in an Avalonia project with the same setup as described above.

If you want to perform OCR on iOS, navigate to the following article to learn more: "How to Perform OCR on iOS in .NET MAUI"