### Install TesseractOcrMaui via Dotnet CLI Source: https://github.com/henrivain/tesseractocrmaui/blob/master/TesseractOcrMaui.IOS/Properties/README.md Add the TesseractOcrMaui package to your project using the dotnet CLI command. ```ps dotnet add package TesseractOcrMaui ``` -------------------------------- ### Install TesseractOcrMaui via Package Reference Source: https://github.com/henrivain/tesseractocrmaui/blob/master/TesseractOcrMaui.IOS/Properties/README.md Add the TesseractOcrMaui package to your .NET MAUI project using a PackageReference in your project file. ```xml ``` -------------------------------- ### Configure MauiAsset Build Action Source: https://github.com/henrivain/tesseractocrmaui/blob/master/TesseractOcrMauiTestApp/Resources/Raw/AboutAssets.txt Add this entry to your .csproj file to ensure files in the Resources/Raw directory are included in the application package. ```xml ``` -------------------------------- ### Configure Dependency Injection Source: https://github.com/henrivain/tesseractocrmaui/blob/master/README.md Register TesseractOcr in the MauiProgram.cs file, ensuring required traineddata files are added to the Resources/Raw folder. ```csharp 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() .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, so services are injected to its constructor builder.Services.AddSingleton(); return builder.Build(); } } ``` -------------------------------- ### Read Raw Asset Stream Source: https://github.com/henrivain/tesseractocrmaui/blob/master/TesseractOcrMauiTestApp/Resources/Raw/AboutAssets.txt Use FileSystem.OpenAppPackageFileAsync to access the stream of a bundled raw asset. ```csharp async Task LoadMauiAsset() { using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt"); using var reader = new StreamReader(stream); var contents = reader.ReadToEnd(); } ``` -------------------------------- ### Configure OCR Character Whitelist Source: https://github.com/henrivain/tesseractocrmaui/blob/master/Documentation/OcrCharacterWhiteList.md Demonstrates loading traineddata from app packages and setting the character whitelist variable on the TessEngine. ```csharp using TesseractOcrMaui; // Folder where traineddata is loaded string traineddataFolder = FileSystem.Current.CacheDirectory; // Load data var traineddataPath = Path.Combine(traineddataFolder, "eng.traineddata"); if (!File.Exists(traineddataPath)) { using Stream traineddata = await FileSystem.OpenAppPackageFileAsync("eng.traineddata"); using FileStream fileStream = File.Create(traineddataPath); traineddata.CopyTo(fileStream); } // Create engine using var engine = new TessEngine("eng", traineddataFolder); // configure engine bool success = engine.SetVariable("tessedit_char_whitelist", "mychars"); // Recognize text using var image = Pix.LoadFromFile(@"\path\to\file.png"); using var result = engine.ProcessImage(image); output.Text = result.GetText(); ``` -------------------------------- ### Apache License 2.0 Source: https://github.com/henrivain/tesseractocrmaui/blob/master/TesseractOcrMaui.IOS/Properties/README.md The project is licensed under the Apache License, Version 2.0. ```licence Copyright 2025 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. ``` -------------------------------- ### ITesseract Configuration Properties Source: https://github.com/henrivain/tesseractocrmaui/blob/master/README.md Properties for configuring the Tesseract engine behavior and output. ```APIDOC ## Properties - **TessDataFolder** (string) - Gets the tessdata folder path from TessDataProvider. - **EngineMode** (EngineMode) - Sets the tesseract engine to use (e.g., LSTM model or Tesseract only). - **EngineConfiguration** (Action?) - Accesses the used TessEngine for configuration (e.g., whitelist characters). - **OutputFormat** (TextFormat) - Sets the format for recognized text output (e.g., TextOnly, HOCR, ALTO). - **PageNumber** (int) - Sets the page number used by special output formats. ``` -------------------------------- ### Inject ITesseract and Recognize Text Source: https://github.com/henrivain/tesseractocrmaui/blob/master/README.md Inject the ITesseract interface into a page and use RecognizeTextAsync to process images selected by the user. ```csharp 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 image path var pickResult = await GetUserSelectedImagePath(); // 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; } private static async Task GetUserSelectedImagePath() { /* This method lets user to select image file by opening file selection dialog. */ #if IOS /* Note that this method uses conditional compilation for iOS because MediaPicker is better option to use for image picking on iOS. */ var pickResult = await MediaPicker.PickPhotoAsync(new MediaPickerOptions() { Title = "Pick jpeg or png image" }); #else var pickResult = await FilePicker.PickAsync(new PickOptions() { PickerTitle = "Pick jpeg or png image", // Currently usable image types are png and jpeg FileTypes = new FilePickerFileType(new Dictionary>() { [DevicePlatform.Android] = new List() { "image/png", "image/jpeg" }, [DevicePlatform.WinUI] = new List() { ".png", ".jpg", ".jpeg" }, }) }); #endif return pickResult?.FullPath; } } ``` -------------------------------- ### ITesseract Interface Definition Source: https://github.com/henrivain/tesseractocrmaui/blob/master/README.md Defines the core methods and properties for interacting with the Tesseract OCR engine, including synchronous and asynchronous recognition. ```csharp public interface ITesseract { RecognizionResult RecognizeText(string imagePath); RecognizionResult RecognizeText(byte[] imageBytes); RecognizionResult RecognizeText(Pix image); Task RecognizeTextAsync(string imagePath); Task RecognizeTextAsync(byte[] imageBytes); Task RecognizeTextAsync(Pix pix); // Loads traineddata files for use from app packages to appdata folder Task LoadTraineddataAsync(); // Gets tessdata folder path from TessDataProvider (from configuration) string TessDataFolder { get; } // Sets tesseract engine to use for example lstm model or tesseract only EngineMode EngineMode { get; set; } // Access used TessEngine for configuration (E.g. Whitelist chafacters) Action? EngineConfiguration { set; } // Format for recognized text output, like TextOnly, HOCR, ALTO TextFormat OutputFormat { get; set; } // Page number used by special output formats int PageNumber { get; set; } } ``` -------------------------------- ### ITesseract Interface Methods Source: https://github.com/henrivain/tesseractocrmaui/blob/master/README.md Methods available for performing text recognition and managing trained data. ```APIDOC ## RecognizeText ### Description Performs text recognition on the provided image source. ### Signature - RecognizeText(string imagePath) -> RecognizionResult - RecognizeText(byte[] imageBytes) -> RecognizionResult - RecognizeText(Pix image) -> RecognizionResult ## RecognizeTextAsync ### Description Performs asynchronous text recognition on the provided image source. ### Signature - RecognizeTextAsync(string imagePath) -> Task - RecognizeTextAsync(byte[] imageBytes) -> Task - RecognizeTextAsync(Pix pix) -> Task ## LoadTraineddataAsync ### Description Loads traineddata files for use from app packages to the appdata folder. ### Signature - LoadTraineddataAsync() -> Task ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.