Skip to content

Using OCR#

Applies to LUNA ID for Android only.

The OCR (Optical Character Recognition) module enables on-device scanning and recognition of identity and official documents using the device’s built-in camera. It extracts both textual and graphical data (for example, photos, signatures) from supported document types.

Key considerations#

Memory usage#

The OCR module may use up to 256 MB of RAM during initialization and recognition.

Consider this when:

  • Running on devices with limited resources.
  • Using OCR concurrently with other resource-intensive modules.
  • Launching OCR in the background or within complex screen hierarchies.

Camera permission#

The camera permission is required for OCR to function.

Errors#

License and LUNA ID errors are always propagated and must be handled at the application level.

Step 1: Add the OCR dependency#

To use the OCR functionality, you must explicitly include the OCR module as a dependency in you Android project. To do this, specify it in the build.gradle.kts file:

implementation("ai.company.product:ocr:X.X.X@aar")

Step 2: Activate the OCR license#

The OCR functionality requires a valid license entry in the license.conf file:

<?xml version="1.0"?>
<settings>
    <section name="Licensing::Settings">
        <!-- Other license parameters -->
        <param name="OCR" type="Value::String" text="ocrLicense" />
    </section>
</settings>

Important notes:

  • If the OCR license parameter is missing, the OCR functionality is disabled.
  • If the parameter is present but contains an invalid value, the OCR initialization fails with an error.
  • If the parameter is present and valid, OCR is activated successfully.
  • OCR licensing is independent of core LUNA ID licensing. Both can be enabled or disabled separately.

Step 3: Initialize OCR#

After adding the dependency and confirming the license, initialize the OCR using:

Ocr.initialize(context, licenseFile)

The OCR initialization result is returned via Ocr.ocrInitStateFlow as the OcrInitStatus object. All initialization statuses are defined in the OcrInitStatus class hierarchy:

ealed class OcrInitStatus {
    data object NotInitialized : OcrInitStatus()
    data object InProgress : OcrInitStatus()
    data object Success : OcrInitStatus()
    data object NotIncluded : OcrInitStatus()
    data class Failure(val cause: Throwable? = null) : OcrInitStatus()
}

In case of an error, the corresponding reason will be passed to the flow.

Step 4: Start the OCR#

Use the following method to start the OCR:

Ocr.start(context)

After calling the method, a new Activity and the device camera will be launched, and OCR recognition will be started.

Step 5: Handle results#

Once the process is completed, the result is sent to LunaID.eventChannel as an event:

sealed class OcrResult {
    data class Success(val data: OcrData) : OcrResult()
    data class Failure(val error: Throwable) : OcrResult()
}
Result Description
OcrResult.Success Recognition was successful. Contains the OcrData object, which includes:
  • Document type
  • Text fields
  • Graphical fields (for example, photo, signature)
  • OcrResult.Failure Recognition failed. Contains an error object (Throwable) which is an OcrError. All possible OCR errors are defined in the sealed class OcrError.