How to Perform OCR on Android in .NET MAUI

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.

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>
    <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.
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using IronOcr;

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()
        {
            // Initialize IronTesseract for OCR purposes
            IronTesseract.Initialize(this);
        }
    }
}
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using IronOcr;

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()
        {
            // Initialize IronTesseract for OCR purposes
            IronTesseract.Initialize(this);
        }
    }
}
Imports Android.App
Imports Android.Content.PM
Imports Android.Runtime
Imports Android.OS
Imports IronOcr

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()
			' Initialize IronTesseract for OCR purposes
			IronTesseract.Initialize(Me)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

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 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 the 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.

using IronOcr;
using Microsoft.Maui.Controls;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

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

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

        private async void ReadFileOnImport(object sender, EventArgs e)
        {
            try
            {
                // Configure the file picker
                var options = new PickOptions
                {
                    PickerTitle = "Please select a file"
                };

                // Await user's file selection
                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 for OCR processing
                    ocrInput.AddImage(stream);
                    // Perform OCR
                    var ocrResult = ocrTesseract.Read(ocrInput);
                    // Display extracted text
                    OutputText.Text = ocrResult.Text;
                }
            }
            catch (Exception ex)
            {
                // Log and handle exceptions
                Debug.WriteLine(ex);
            }
        }
    }
}
using IronOcr;
using Microsoft.Maui.Controls;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

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

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

        private async void ReadFileOnImport(object sender, EventArgs e)
        {
            try
            {
                // Configure the file picker
                var options = new PickOptions
                {
                    PickerTitle = "Please select a file"
                };

                // Await user's file selection
                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 for OCR processing
                    ocrInput.AddImage(stream);
                    // Perform OCR
                    var ocrResult = ocrTesseract.Read(ocrInput);
                    // Display extracted text
                    OutputText.Text = ocrResult.Text;
                }
            }
            catch (Exception ex)
            {
                // Log and handle exceptions
                Debug.WriteLine(ex);
            }
        }
    }
}
Imports IronOcr
Imports Microsoft.Maui.Controls
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Threading.Tasks

Namespace MAUIIronOCRAndroidSample
	Partial Public Class MainPage
		Inherits ContentPage

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

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

		Private Async Sub ReadFileOnImport(ByVal sender As Object, ByVal e As EventArgs)
			Try
				' Configure the file picker
				Dim options = New PickOptions With {.PickerTitle = "Please select a file"}

				' Await user's file selection
				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 for OCR processing
					ocrInput.AddImage(stream)
					' Perform OCR
					Dim ocrResult = ocrTesseract.Read(ocrInput)
					' Display extracted text
					OutputText.Text = ocrResult.Text
				End If
			Catch ex As Exception
				' Log and handle exceptions
				Debug.WriteLine(ex)
			End Try
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

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"

Frequently Asked Questions

What is .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. It simplifies building native user interfaces that are runnable on multiple platforms.

How can I perform OCR on Android using IronOCR?

You can perform OCR on Android by using the IronOcr.Android package within a .NET MAUI project. Install the package via NuGet, configure your project files, and use the IronTesseract class to process images for text extraction.

Why should I use the IronOcr.Android package?

The IronOcr.Android package enables OCR features specifically for Android devices within .NET cross-platform projects, eliminating the need for the vanilla IronOCR package.

How do I install the IronOcr.Android package?

Install the IronOcr.Android package using NuGet in Visual Studio. Right-click on 'Dependencies', select 'Manage NuGet Packages...', search for 'IronOcr.Android', and click on 'Install'.

What changes are needed in the .csproj file for Android?

In the .csproj file, create a new ItemGroup element with a condition targeting Android platforms. Move the 'IronOcr.Android' PackageReference inside this ItemGroup to prevent issues when building for other platforms.

How do I edit the MainActivity.cs file?

Open the MainActivity.cs file under Platforms -> Android. Add the MainActivity method and invoke the Initialize method from IronTesseract to set up OCR capabilities.

Can IronOcr.Android be used in Avalonia projects?

Yes, IronOcr.Android can be used in Avalonia projects with a similar setup process to .NET MAUI.

What is the process to edit MainPage.xaml for OCR?

Edit MainPage.xaml to include a button for file import and a label to display OCR results. This setup involves defining UI elements that interact with the OCR processing logic.

How do I handle exceptions during OCR processing?

Handle exceptions by using try-catch blocks around OCR processing code. Log exceptions using Debug.WriteLine or other logging mechanisms to diagnose issues.

Where can I download a complete .NET MAUI App project for IronOCR?

You can download the complete code for a .NET MAUI App project using IronOCR from the provided link in the guide. It comes as a zipped file that can be opened in Visual Studio.