Plugin.Maui.OCR and Plugin.Xamarin.OCR

repository·main·Indexed 41 days ago

https://github.com/kfrancis/ocr

Cross-platform wrappers for text recognition (OCR) in .NET MAUI and Xamarin. It provides a unified IOcrService interface to leverage native platform APIs—Apple Vision (iOS/macOS), Google ML Kit (Android), and Windows Media OCR—without requiring external dependencies like Tesseract.

Tokens
17.3K
Snippets
46
Records
69
Agent score
50%

What's inside Plugin.Maui.OCR

  1. Overview of kfrancis/ocr functionality

    main

    The kfrancis/ocr project provides cross-platform Optical Character Recognition (OCR) capabilities for mobile (Android, iOS) and desktop (Windows) applications. It allows developers to extract text from images using a consistent API regardless of the underlying platform.

    Key features include:

    • Consistent API: A unified interface for different platforms.
    • Text Pattern Matching: Ability to find specific data types like emails or phone numbers within recognized text.
    • Configurable Performance: Options to balance recognition speed against accuracy.
    • Image Preprocessing: Support for improving image quality to enhance recognition results.
  2. What is OcrPatternMatcher and when to use it

    main
    The OcrPatternMatcher is a utility class designed to extract specific pieces of information (like phone numbers, emails, or currency amounts) from the raw text produced by an OCR scan. Instead of manually parsing large blocks of text, you define regular expression patterns to identify target data. It is particularly useful for structured data extraction from unstructured documents like business cards, receipts, or invoices.
  3. What is IOcrService and how does it work?

    main

    The IOcrService interface is a cross-platform abstraction that provides a consistent way to perform Optical Character Recognition (OCR) operations regardless of the underlying operating system (iOS, Android, or Windows).

    It solves the problem of platform fragmentation by acting as a universal interface: while iOS might use Apple's Vision framework and Android might use Google's ML Kit, your application code interacts only with IOcrService. This ensures your OCR logic remains platform-independent.

    Workflow:

    1. Your app calls an IOcrService method.
    2. The request is forwarded to a platform-specific implementation.
    3. The implementation invokes the native OCR engine.
    4. The native engine's output is formatted into a standardized OcrResult and returned to your app.
  4. Understand the OcrResult data structure

    main

    The OcrResult object is the unified format returned by all platform implementations. It abstracts away the differences between native engines (like ML Kit on Android or Vision on iOS).

    Key properties of OcrResult include:

    • AllText: The complete string of recognized text.
    • Lines: A collection of text lines found in the image.
    • Elements: A collection of individual text elements (words/characters) containing metadata like Text, Confidence, and bounding box coordinates (X, Y, Width, Height).
    • MatchedValues: A collection of strings that matched any provided OcrPatternConfig patterns.
    • Success: A boolean flag indicating if the OCR process completed successfully.
  5. Configure OCR settings with OcrOptions

    main

    The OcrOptions class allows you to customize the OCR process, such as specifying the expected language, prioritizing accuracy over speed, and defining patterns for automatic data extraction.

    Depending on your platform/version, you use different instantiation methods:

    MAUI Version (Builder Pattern)

    Use the OcrOptions.Builder for a fluent configuration experience.

    Xamarin Version (Constructor Approach)

    Use the OcrOptions constructor directly with named parameters.

    // MAUI Version
    var options = new OcrOptions.Builder()
        .SetLanguage("en")
        .SetTryHard(true)
        .Build();
    
    // Xamarin Version
    var options = new OcrOptions(
        language: "en",
        tryHard: true
    );
  6. Compare UseOcr vs. OcrPlugin

    main

    The library provides two ways to access OCR functionality:

    UseOcr (Dependency Injection Approach)

    • Pros: Follows MAUI best practices, works well with other DI services, easier to unit test with mock services, and promotes cleaner architecture.
    • Cons: Requires constructor injection in every class that needs it; slightly more setup code.

    OcrPlugin (Static Access Approach)

    • Pros: Simpler for small apps; allows direct access from anywhere without passing services between classes.
    • Cons: Harder to test (difficult to replace with mocks) and does not follow modern DI patterns.
  7. How OcrImplementation works across platforms

    main

    The OcrImplementation system provides a unified way to perform OCR across different operating systems by wrapping native OCR engines behind a common IOcrService interface. Instead of writing platform-specific code for every device, you interact with the common interface, and the library automatically selects the correct implementation at runtime.

    Supported Platforms and Engines

    • Android: Uses Google's ML Kit Text Recognition.
    • iOS/macOS: Uses Apple's Vision framework (VNRecognizeTextRequest).
    • Windows: Uses Windows.Media.OCR.
    // You write this once in your app
    IOcrService ocrService = /* get the service via Plugin or Extension Method */; 
    OcrResult result = await ocrService.RecognizeTextAsync(imageBytes);
    
    // Behind the scenes, the library selects the correct implementation:
    // On Android: result = await OcrImplementation.android.RecognizeTextAsync(imageBytes);
    // On iOS:     result = await OcrImplementation.macios.RecognizeTextAsync(imageBytes);
    // On Windows: result = await OcrImplementation.windows.RecognizeTextAsync(imageBytes);
  8. Understand the OcrResult class

    main

    The OcrResult class serves as the primary container for all data extracted during an OCR operation. It encapsulates the status of the operation and provides multiple levels of granularity for the extracted content, ranging from the full text block down to individual elements with spatial positioning.

    Key Properties

    • Success: A boolean indicating whether the OCR engine successfully processed the input. Always check this property before accessing other data to avoid processing invalid results.
    • AllText: A string containing the complete, concatenated text extracted from the image.
    • Lines: A collection representing the individual lines of text found in the image.
    • Elements: A collection of OcrElement objects. These represent the finest level of granularity, typically providing individual words or characters along with their specific positions/bounding boxes.
    • MatchedValues: A mechanism to automatically extract specific patterns (like regex matches) from the extracted text.
  9. How OcrPlugin works as a central access point

    main

    The OcrPlugin class acts as a static, singleton-like access point for OCR services throughout your application. Instead of creating new instances of an OCR service in every page or passing services between components, you can access the shared service via OcrPlugin.Default.

    This pattern ensures:

    • Consistent Service: Every part of your app uses the same implementation.
    • Simplified Lifecycle: You can initialize the service once at app startup and use it anywhere.
    • Reduced Boilerplate: No need for manual dependency injection or service location for basic usage.
    // Access the shared service from anywhere
    IOcrService ocrService = OcrPlugin.Default;
  10. Understand the OcrResult class

    main

    The OcrResult class is the structured container returned after an OCR operation via IOcrService.RecognizeTextAsync. It organizes extracted information into different levels of granularity, allowing you to access everything from the full text block down to individual word coordinates and confidence scores.

    // The result of an OCR operation
    OcrResult result = await ocrService.RecognizeTextAsync(imageBytes);
  11. UseOcr vs. OcrPlugin: Choosing an integration approach

    main

    The library provides two distinct ways to access OCR capabilities. Choosing between them depends on your preferred architectural pattern:

    1. UseOcr (Dependency Injection Approach):

      • Recommended for modern MAUI development.
      • Registers the OCR service in the DI container.
      • Access the service via constructor injection in Pages, ViewModels, or other services.
      • Better for testing and following MAUI best practices.
    2. OcrPlugin (Static Access Approach):

      • Provides a static way to access the OCR functionality.
      • Useful for quick implementations or scenarios where DI is not easily accessible, but generally less flexible for testing.
  12. Register a custom OCR implementation in MAUI

    main
    While OcrPlugin.Default provides a convenient static access point, in modern .NET MAUI applications, you can also register a custom implementation of the OCR service using Dependency Injection (DI). This allows you to swap out the default engine for a specialized one while still utilizing the plugin's architecture.