.NET MAUI OCR Using IronOCR

Introduction

Microsoft released .NET MAUI (Multi-platform App UI), which is a framework for building cross-platform applications with the .NET Framework. It allows you to write code that runs on Android, iOS and Windows using the same codebase, saving you time, resources and effort. .NET MAUI is open source. You can get the source code of .NET MAUI project with examples on the GitHub.

In this How-To Guide, we'll learn how to create an OCR processor apps on .NET MAUI using the IronOCR library with examples.

IronOCR: .NET OCR library

IronOCR is a .NET OCR NuGet library that enables developers to easily integrate Optical Character Recognition (OCR) functionality into their projects. Using IronOCR, PDF documents can be scanned and converted into searchable and editable text/data without any loss of data quality. This makes it easy for users to find the information they need from PDF documents and make changes or corrections if necessary.

IronOCR is the most advanced build of the Tesseract binaries available for any platform. It offers increased speed, accuracy, and a native DLL/API that supports all versions of Tesseract (from Tesseract 3 up to Tesseract 5) with one easy install/download.

IronOCR's language support is extensive, with 125 international languages available to users. The English language is installed by default with the tool/DLL. However, you can easily add more languages by installing them through NuGet or by downloading DLLs.

Comparison with Tesseract

IronOCR is specifically designed for C# developers and integrates seamlessly with .NET applications. By contrast, Tesseract is a generic OCR library that requires developers to write their own wrappers in order to use it with C#. In addition, IronOCR offers superior accuracy and speed as compared to other libraries, thanks to its innovative artificial intelligence algorithms.

IronOCR comes with comprehensive documentation and technical support, making it easy for even novice developers to get up and running quickly.

IronOCR is much more accurate than Tesseract. In fact, it has an accuracy rate of over 99%, while Tesseract's accuracy rate is only around 70.2% to 92.9%. Get more info and support about IronOCR and Tesseract comparison on YouTube video.

Steps to Create an OCR MAUI app

Follow the next steps to create an OCR app in the .NET MAUI framework using IronOCR.

Prerequisites

For creating an OCR app in .NET MAUI, there are some prerequisites.

  1. Visual Studio 2022 (Latest version)
  2. .NET 6 or 7
  3. MAUI packages installed in Visual Studio
  4. A .NET MAUI project running in Visual Studio

Install IronOCR

The first step is to install the IronOCR library using the NuGet Packages Manager Console. Open the NuGet Packages Console by right-clicking on the solution explorer and writing the following command to install the IronOCR library.

Install-Package IronOcr

Frontend design

We will design the frontend of the application in this section. Open the MainPage.xaml file.

.NET MAUI OCR Tutorial Using IronOCR - Figure 1: MainPage.xaml

MainPage.xaml

We designate a button that will help us to select the image or PDF document for OCR. The button's clicked property is set to execute the IOCR function. We will define this function in the next section.

<Button
    x:Name="OCR"
    Text="Click to OCR"
    Clicked="IOCR"
    HorizontalOptions="Center" />
XML

Here, we create a Image box with the name OCRImage. This image box will help to display the selected file.

<Image
    x:Name="OCRImage"
    SemanticProperties.Description="Selected Image"
    HeightRequest="300"
    HorizontalOptions="Center" />
XML

Next, we create a Editor control. It'll be used to show the extracted text from the image or PDF document.

<Editor
    x:Name="outputText"
    HorizontalOptions="Center"
    WidthRequest="600"
    HeightRequest="300"
    />
XML

Here is the completed XAML User Interface markup.

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

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">
            <Button
                x:Name="OCR"
                Text="Click to OCR"
                Clicked="IOCR"
                HorizontalOptions="Center" />
            <Image
                x:Name="OCRImage"
                SemanticProperties.Description="Selected Image"
                HeightRequest="300"
                HorizontalOptions="Center" />

            <Editor
                x:Name="outputText"
                HorizontalOptions="Center"
                WidthRequest="600"
                HeightRequest="300"
                />
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
XML

Now, it's time to write the code for OCR functionality.

Code for OCR using IronOCR

Open the "MainPage.xaml.cs" class file and write the following function in it.

.NET MAUI OCR Tutorial Using IronOCR - Figure 2: MainPage.xaml.cs

MainPage.xaml.cs

private async void IOCR(object sender, EventArgs e)
{
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });
    var path = images.FullPath.ToString();
    OCRImage.Source = path;

    var ocr = new IronTesseract();
    using (var input = new OcrInput())
    {
        input.AddImage(path);
        OcrResult result = ocr.Read(input);
        string text = result.Text;
        outputText.Text = text; 
    }
}
private async void IOCR(object sender, EventArgs e)
{
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });
    var path = images.FullPath.ToString();
    OCRImage.Source = path;

    var ocr = new IronTesseract();
    using (var input = new OcrInput())
    {
        input.AddImage(path);
        OcrResult result = ocr.Read(input);
        string text = result.Text;
        outputText.Text = text; 
    }
}
Private Async Sub IOCR(ByVal sender As Object, ByVal e As EventArgs)
	Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
		.PickerTitle = "Pick image",
		.FileTypes = FilePickerFileType.Images
	})
	Dim path = images.FullPath.ToString()
	OCRImage.Source = path

	Dim ocr = New IronTesseract()
	Using input = New OcrInput()
		input.AddImage(path)
		Dim result As OcrResult = ocr.Read(input)
		Dim text As String = result.Text
		outputText.Text = text
	End Using
End Sub
VB   C#

Let's break down the above code.

The following piece of code helps to pick the image using C#'s FilePicker object. The FilePicker title and FilePicker file type are both set to values, along with the path of the image.

var images = await FilePicker.Default.PickAsync(new PickOptions
{
    PickerTitle = "Pick image",
    FileTypes = FilePickerFileType.Images
});
var path = images.FullPath.ToString();
var images = await FilePicker.Default.PickAsync(new PickOptions
{
    PickerTitle = "Pick image",
    FileTypes = FilePickerFileType.Images
});
var path = images.FullPath.ToString();
Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
	.PickerTitle = "Pick image",
	.FileTypes = FilePickerFileType.Images
})
Dim path = images.FullPath.ToString()
VB   C#

The Image box control is next configured to use the image specified in the path variable above.

OCRImage.Source = path;
OCRImage.Source = path;
OCRImage.Source = path
VB   C#

Afterward, we start to make use of IronOCR by creating a new instance of the IronTesseract class. We use it to extract text from the selected image and next to display the content in Editor control.

var ocr = new IronTesseract();
    using (var input = new OcrInput())
    {
        input.AddImage(path);
        OcrResult result = ocr.Read(input);
        string text = result.Text;
        outputText.Text = text; 
    }
var ocr = new IronTesseract();
    using (var input = new OcrInput())
    {
        input.AddImage(path);
        OcrResult result = ocr.Read(input);
        string text = result.Text;
        outputText.Text = text; 
    }
Dim ocr = New IronTesseract()
	Using input = New OcrInput()
		input.AddImage(path)
		Dim result As OcrResult = ocr.Read(input)
		Dim text As String = result.Text
		outputText.Text = text
	End Using
VB   C#

Output

After running the project, the below UI shows up. When you click on the button, it'll prompt you to select the image/PDF from any location.

.NET MAUI OCR Tutorial Using IronOCR - Figure 3: OCR Output

OCR Output

After selecting the image, IronOCR processes the image and shows the recognized words in the Editor control. You can copy the text from the Editor control.

.NET MAUI OCR Tutorial Using IronOCR - Figure 4: OCR Image

OCR Image

From results, it shows that IronOCR performs a wonderful job at processing complex images with patterns, showing accurate results. IronOCR is able to detect the red dot on the letter and to select the exact letter required using its pre-trained models.

Conclusion

For further reading, refer to this tutorial which gives additional information about how to use IronOCR to read text from images.

IronOCR is free for development purposes. You can purchase it for the very low price starting from just $749. Check out the pricing plan here.