百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

ocr

> 编程语言
开源

一个插件,可通过仅使用平台 API 提供从图像中进行简单文本识别的功能。

188 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个插件,可通过仅使用平台 API 提供从图像中进行简单文本识别的功能。

Plugin.Maui.OCR | Plugin.Xamarin.OCR

Plugin.Maui.OCR and Plugin.Xamarin.OCR provide the ability to do simple text from image OCR using nothing but platform APIs.

Documentation

Check out our detailed tutorial for implementation guides and examples.

Additional documentation:

  • IOcrService Interface
  • Ocr Result
  • Ocr Options
  • Pattern Matcher
  • Ocr Implementation
  • Plugin
  • UseOcr Extension Method
  • Sample App

What Works Matrix

Platform iOS Android Windows macOS
MAUI Yes Yes Yes Yes
Xamarin Yes Yes N/A N/A

Why

Why am I making this? I'm doing this because I want to make it easier for developers to do OCR in their apps. I want to make it so that you can just use this plugin and not have to worry about the platform specifics.

Too many times I've tried to do OCR and had to wrestle with external dependencies like Tesseract (with its dependencies Leptonica, etc) and these types of native dependencies can be a real pain to work with.

Xamarin??

Well, I still have to maintain a Xamarin app that uses Tesseract and I'm tired of all the problems that come with it. I want to make it easier for myself and others to do OCR in their apps.

Example App

Here's an example of what the OCR library can do. This is the processing of race results receipt, which is a typical dot-matrix printed document. The OCR is able to read the text from the image.

Install Plugin

Available on NuGet for MAUI and Xamarin.

Install with the dotnet CLI: dotnet add package Plugin.Maui.OCR or dotnet add package Plugin.Xamarin.OCR, or through the NuGet Package Manager in Visual Studio.

Supported Platforms

Platform Minimum Version Supported
iOS 13+
macOS 10.15+
Android 5.0 (API 21)
Windows 11 and 10 version 1809+

OCR Engines and Algorithms

The Plugin.Maui.OCR library is designed to provide a cross-platform, easy-to-use wrapper for native OCR implementations without relying on external OCR engines like Tesseract. Below is an overview of the OCR engines and frameworks used on supported platforms:

iOS

The plugin leverages Apple's Vision Framework for text recognition. This framework is highly optimized for Apple devices and supports various recognition features like text in different languages, accuracy levels, and bounding box detection. Key aspects include:

  • OCR Engine: Vision Framework
  • Supported Languages: Determined dynamically at runtime, based on the available languages supported by Vision's text recognition capabilities.
  • Recognition Levels: Fast (for quick recognition) and Accurate (for higher precision).
  • Other Features: Bounding box detection, CPU/GPU processing options, and real-time recognition capabilities.

For further details, you can review the official Vision Framework documentation.

Android

The plugin utilizes Google's ML Kit for on-device and cloud-based text recognition. ML Kit provides efficient OCR capabilities and can operate in both offline and online modes depending on the configuration. Key aspects include:

  • OCR Engine: ML Kit Text Recognition
  • Supported Languages:
    • On-Device: Primarily Latin-based scripts such as English, Spanish, French, German, Italian, and Portuguese.
    • Cloud-Based: A broader range of languages, including Arabic, Chinese (Simplified and Traditional), Greek, Russian, Thai, and many more.
  • Recognition Levels: The plugin offers two modes:
    • Fast (on-device, for quicker results with limited language support)
    • Accurate (cloud-based, for more accurate results and extended language support)
  • Other Features: Confidence scores for recognized text, bounding box detection for each text element, and cloud-based recognition for more extensive language support (requires an internet connection).

Windows

The plugin leverages the native Windows.Media.Ocr API for text recognition. This API enables high-quality OCR capabilities on supported Windows devices without the need for external libraries. Key aspects include:

  • OCR Engine: Windows.Media.Ocr
  • Supported Languages: The OCR engine supports languages installed on the user's system. A dynamic list of supported languages can be retrieved using the OcrEngine.AvailableRecognizerLanguages property.
  • Recognition Levels: The Windows OCR API operates with a single recognition mode, but its efficiency can be influenced by the language settings and image quality.
  • Other Features: Bounding box detection for recognized words, support for different image formats, and multi-language support based on user profile languages.

Camera Specifications and Quality Considerations

While the plugin handles the OCR process, the following can impact the recognition quality:

  • Camera Resolution: Higher resolutions generally result in better text recognition.
  • Distance from Target: Keeping the camera at an optimal distance for clear, sharp text is recommended.
  • Lighting Conditions: Adequate lighting will enhance the OCR accuracy.
  • Supported Image Formats: The plugin works with common formats such as JPEG and PNG.

Pattern Matching

One of the more common things I do with OCR is recognize a text pattern. For example, I might want to read a date, a phone number or an email address. This is where the OcrPatternConfig class comes in.

Let's say you want to recognize an Ontario Health Card Number (HCN) in the text of your image. Numbers of those types have some specific qualities that make it easy to match.

  1. An Ontario HCN is 10 digits long.
  2. The number must be Luhn valid (meaning it has a check digit and it's correct).

To do this, you can create an OcrPatternConfig object like so:

…

MAUI Setup and Usage

For MAUI, to initialize make sure you use the MauiAppBuilder extension UseOcr() like so:

…

Xamarin Setup and Usage

For Xamarin, if you have some kind of DI framework in place then you can just register the OcrPlugin with it.

csharp
public App()
{
    InitializeComponent();

    DependencyService.RegisterSingleton(OcrPlugin.Default);

    MainPage = new MainPage();
}

If you don't have a DI framework in place, you can use the OcrPlugin.Default property to access the IOcrService instance.

csharp
private readonly IOcrService _ocr;

public MainPage(IOcrService? ocr)
{
    InitializeComponent();

    _ocr = ocr ?? OcrPlugin.Default;
}

Details

The IOcrService interface exposes the following methods:

…

Permissions

Before you can start using Feature, you will need to request the proper permissions on each platform.

iOS

If you're handling camera, you'll need the usual permissions for that.

Android

If you're handling camera, you'll need the usual permissions for that. The only extra part you'll want in the AndroidManifest.xml is the following:

xml

  

This will cause the model necessary to be installed when the application is installed.

OcrOptions and Builder

The OcrOptions class provides a flexible way to configure OCR settings. You can use the OcrOptions.Builder class to create instances of OcrOptions with various configurations.

OcrOptions Class

The OcrOptions class holds the configuration for OCR operations.

…

Usage Example

Using the OcrOptions.Builder to create an OcrOptions instance is straightforward and flexible:

csharp
var options = new OcrOptions.Builder()
    .SetLanguage("en-US")
    .SetTryHard(true)
    .AddPatternConfig(new OcrPatternConfig(@"\d{10}"))
    .SetCustomCallback(myCustomCallback)
    .Build();

Dependency Injection

You will first need to register the OcrPlugin with the MauiAppBuilder following the same pattern that the .NET MAUI Essentials libraries follow.

csharp
builder.Services.AddSingleton(OcrPlugin.Default);

You can then enable your classes to depend on IOcrService as per the following example.

csharp
public class OcrViewModel
{
    readonly IOcrService _ocr;

    public OcrViewModel(IOcrService? ocr)
    {
        _ocr = ocr ?? OcrPlugin.Default;
    }

    public void DoSomeOcr()
    {
        byte[] imageData = GetImageData();

        var result = await _ocr.RecognizeTextAsync(imageData);
    }
}

Straight usage

Alternatively if you want to skip using the dependency injection approach you can use the Feature.Default property.

csharp
public class OcrViewModel
{
    public void DoSomeOcr()
    {
        byte[] imageData = GetImageData();

        var result = await OcrPlugin.Default.RecognizeTextAsync(imageData);
    }
}

Feature

Once you have the OCR instance, you can interact with it in the following ways:

Events

RecognitionCompleted

This event is fired when the OCR service has completed recognizing text from an image. The event args contain the OcrResult object. Only fires if the StartRecognizeTextAsync method is called.

Properties

SupportedLanguages

A list of supported languages for the OCR service. This is populated after calling InitAsync. Allows you to know what language codes can be used in OcrOptions.

Methods

InitAsync(CancellationToken ct = default)

Initialize the feature. If supported on the platform (like iOS), SupportedLanguages will be populated with the available languages.

RecognizeTextAsync(byte[] imageData, bool tryHard = false, CancellationToken ct = default)

Recognize text from an image. Specify "tryHard" if you want to tell the platform API to do a better job (fast vs accurate, and use language correction (ios/mac)) though it seems very accurate normally.

RecognizeTextAsync(byte[] imageData, OcrOptions options, CancellationToken ct = default)

Recognize text from an image. OcrOptions contains options for the OCR service, including the language to use and whether to try hard.

Task StartRecognizeTextAsync(byte[] imageData, OcrOptions options, CancellationToken ct = default)

Start recognizing text from an image. This is a task that will fire the RecognitionCompleted event when it completes with the result.

Acknowledgements

Thanks to the great Gerald Versluis for making an amazing video about using this project: https://www.youtube.com/watch?v=alY_6Qn0_60

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#dotnetmauiocrxamarin

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言