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!
How to use IronOCR on Android in .NET MAUI
- Download the C# library to perform OCR on Android
- Create a .NET MAUI App project
- Edit the XAML file to display an activation button and output text
- Edit the corresponding C# file to perform the OCR
- Download the sample project for a quick start
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 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".

Include the IronOCR.Android library
The library can be added in various ways. The easiest is perhaps by using NuGet.
- Inside Visual Studio, right-click on "Dependencies" and select "Manage NuGet Packages ...".
- Select the "Browse" tab and search for "IronOcr.Android".
- Select the "IronOcr.Android" package and click on "Install".

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:
-
Right-click on the project and select "Edit Project File".
-
Create a new ItemGroup element as such:
<ItemGroup Condition="$(TargetFramework.Contains('android')) == true"> </ItemGroup>XML -
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
MainActivitymethod and invoke theInitializemethod.
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 NamespaceEdit "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>
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.LoadImage(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);
}
}
}
}
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.
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 the IronOCR Android package?
The IronOCR.Android package enables OCR features on Android devices using .NET cross-platform projects. It's specifically designed to integrate with the .NET MAUI framework for Android development.
How do I install the IronOCR.Android package using NuGet?
Open Visual Studio, right-click on 'Dependencies' and select 'Manage NuGet Packages'. Under the 'Browse' tab, search for 'IronOcr.Android', select the package, and click 'Install'.
Can I use IronOCR.Android with other platforms?
IronOCR.Android is tailored for Android platforms. For cross-platform support, such as iOS, you should use the platform-specific versions like IronOcr.iOS.
How do I configure a .NET MAUI project for IronOCR?
Start by creating a .NET MAUI App project in Visual Studio. Include the IronOCR.Android library via NuGet and modify the .csproj file to target the Android platform specifically.
What steps are required to perform OCR in a .NET MAUI application?
To perform OCR, initialize IronTesseract in your 'MainActivity.cs', configure your XAML files for UI, and handle file inputs and OCR processing in 'MainPage.xaml.cs'.
How does IronOCR handle file selection and OCR in C#?
IronOCR uses the FilePicker.PickAsync method for file selection in .NET MAUI apps. After a user selects a file, IronTesseract processes the image to perform OCR and extracts text data.
What is required in the project file to ensure it targets only Android?
In the .csproj file, create an ItemGroup with a condition to target Android using `
How can you download a complete sample project using IronOCR?
You can download the complete .NET MAUI App project with the IronOCR.Android setup from the provided guide link, which includes a zipped file for use in Visual Studio.
Is it possible to use IronOCR in an Avalonia project?
Yes, IronOCR.Android can be used in an Avalonia project with comparable setup steps as outlined for .NET MAUI applications.
How do you initialize IronTesseract in a MAUI project?
Within your 'MainActivity', initialize IronTesseract by calling `IronTesseract.Initialize(this);` to prepare it for OCR operations in your Android app.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.