TesseractOcrMaui 1.0.8

There is a newer version of this package available.
See the version list below for details.
dotnet add package TesseractOcrMaui --version 1.0.8                
NuGet\Install-Package TesseractOcrMaui -Version 1.0.8                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TesseractOcrMaui" Version="1.0.8" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TesseractOcrMaui --version 1.0.8                
#r "nuget: TesseractOcrMaui, 1.0.8"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TesseractOcrMaui as a Cake Addin
#addin nuget:?package=TesseractOcrMaui&version=1.0.8

// Install TesseractOcrMaui as a Cake Tool
#tool nuget:?package=TesseractOcrMaui&version=1.0.8                

TesseractOcrMaui

Tesseract wrapper for windows and Android for .NET MAUI.

What is this?

I didn't find any good up to date C# wrapper to Tesseract that would function with Maui on Android devices. This library wrapps native Tesseract C/C++ libraries to usable C# interfaces. Currently only Android and Windows are supported, because I don't have resources to test on MacOs or IOS. My own projects only support Windows and Android, so I didn't need those other platforms. If you need support for Apple devices, you have to build those libraries yourself and add them to project.

Supported platforms

Currently supports Windows and Android. Library is meant to be used with .NET MAUI project. You can see supported cpu architechtures down below.

platform Architechture
Windows x86_64
Android Arm64-v8a
Android Arm-v7a
Android x86_64
Android x86

Supported runtimes

net7.0 or newer
net7.0-windows10.0.19041 or newer
net7.0-android or newer

Only png and jpeg libraries are compiled into tesseract native libraries, so only these image types are supported. Additional image libraries are added if needed later.

Getting started

1. Add nuget package to your project.

Package is available in nuget.org. You can use it in your project by adding it in your visual studio `nuget package manager or by running command

dotnet add package TesseractOcrMaui

2. Add package to dependency injection (see TesseractOcrMauiTestApp)

Page should be injected or injecting Tesseract won't work. AddTesseractOcr also loads all libraries that are needed to run library. files.AddFile("eng.traineddata"); adds all your traineddata files to be loaded, when tesseract is used. For example I add eng.traineddata, so I must add traineddata file with same name to my project's TesseractOcrMauiTestApp\Resources\Raw folder.

MauiProgram.cs

using Microsoft.Extensions.Logging;
using TesseractOcrMaui;  // include library namespace

namespace TesseractOcrMauiTestApp;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
        // Inject logging, (optional, but might give useful information)
        builder.Services.AddLogging();

        // Inject library functionality
        builder.Services.AddTesseractOcr(
            files =>
            {
                // must have matching files in Resources/Raw folder
                files.AddFile("eng.traineddata");
            });

        // Inject main page
        builder.Services.AddSingleton<MainPage>();

        return builder.Build();
    }
}

3. Inject ITesseract into your page

Now you can constructor inject ITesseract interface to you page. I have two labels ("confidenceLabel" and "resultLabel") in my main page. I added button with clicked event handler. I you can see my Button_Clicked functionality down below.

Mainpage.xaml.cs

using TesseractOcrMaui; // Include library namespace
using TesseractOcrMaui.Extensions;  // Help for handling result object from recognizion

namespace TesseractOcrMauiTestApp;

public partial class MainPage : ContentPage
{
    // inject ITesseract
    public MainPage(ITesseract tesseract)
    {
        InitializeComponent();
        Tesseract = tesseract;
    }

    ITesseract Tesseract { get; }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        // Make user pick file
        var pickResult = await FilePicker.PickAsync(new PickOptions()
        {
            PickerTitle = "Pick jpeg or png image",
            // Currently usable image types
            FileTypes = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>()
            {
                [DevicePlatform.Android] = new List<string>() { "image/png", "image/jpeg" },
                [DevicePlatform.WinUI] = new List<string>() { ".png", ".jpg", ".jpeg" },
            })
        });
        // null if user cancelled the operation
        if (pickResult is null)
        {
            return;
        }

        // Recognize image
        var result = await Tesseract.RecognizeTextAsync(pickResult.FullPath);

        // Show output
        confidenceLabel.Text = $"Confidence: {result.Confidence}";
        if (result.NotSuccess())
        {
            resultLabel.Text = $"Recognizion failed: {result.Status}";
            return;
        }
        resultLabel.Text = result.RecognisedText;
    }
}

ITesseract API

You can find following methods in the main high level API.

public interface ITesseract
{
    RecognizionResult RecognizeText(string imagePath);
    RecognizionResult RecognizeText(byte[] imageBytes);
    RecognizionResult RecognizeText(Pix image);

    Task<RecognizionResult> RecognizeTextAsync(string imagePath);
    Task<RecognizionResult> RecognizeTextAsync(byte[] imageBytes);
    Task<RecognizionResult> RecognizeTextAsync(Pix pix);

    // Loads traineddata files for use from app packages to appdata folder
    Task<DataLoadResult> LoadTraineddataAsync();


    // Gets tessdata folder path from TessDataProvider (from configuration)
    string TessDataFolder { get; }
}

Licence

Copyright 2023 Henri Vainio

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

NOTE: Tesseract depends on other packages that may be licensed under different open source licenses.

This project does not depend on any third-party C# packages, but it needs traineddata files to function. Parts of the code are also is reused from Charlesw Windows Tesseract wrapper.

Contributing, IOS and MacOS

If you are interested developing this project, please, open conversation in issues and describe your changes you want to make. Package doesn't currently support IOS or MacOS so any help for them is well appreciated.

Documentation

Support

If you have any questions about anything related to this project, open issue or send me an email.

Only Android and Windows are supported currently, because I have no recources to build and test for IOS and MacOS. Integrating these platforms should not be very big problem if someone needs them, but I don't need them. If you want to add them, just contact me.

Henri Vainio
matikkaeditorinkaantaja(at)gmail.com

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-android33.0 is compatible.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows10.0.19041 is compatible.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net7.0-android33.0

    • No dependencies.
  • net7.0-windows10.0.19041

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 9,612 5/10/2024
1.1.6 1,885 2/21/2024
1.1.5 5,139 1/25/2024
1.1.2 192 1/23/2024
1.1.1 242 1/22/2024
1.1.0 345 1/16/2024
1.0.9 483 1/1/2024
1.0.8 826 11/5/2023
1.0.7 1,129 8/19/2023
1.0.6 880 4/12/2023
1.0.4 538 4/10/2023
1.0.3 382 4/10/2023
1.0.2 424 4/10/2023
1.0.1 546 4/10/2023