CameraXMrzScanner

class CameraXMrzScanner(appContext: Context, lifecycleOwner: LifecycleOwner, recognizer: MrzTextRecognizer<ImageProxy> = MlKitMrzTextRecognizer(), mode: ParsingMode = ParsingMode.STRICT, telemetry: TelemetrySink = TelemetrySinkRegistry.current, cameraSelector: CameraSelector = CameraSelector.DEFAULT_BACK_CAMERA) : MrzCameraScanner, AutoCloseable

Runs CameraX on Android and streams MrzScanResults from analysed frames.

It binds an ImageAnalysis use case to lifecycleOwner with the keep-only-latest strategy, and closes every frame after analysis. Frames that arrive while one is being analysed are dropped, so results are not one per frame. The scanner creates no preview unless you call enablePreview. The caller must hold the CAMERA permission before start; the scanner reports CameraError.PermissionDenied and never requests it.

A CameraError.PermissionDenied or a critical CameraError.CameraUnavailable ends the session. A CameraError.CameraInUse, and a CameraError.CameraUnavailable reported while CameraX is retrying, leave the session bound; the stream resumes when the camera recovers. A CameraError.OcrFailed covers one frame. Close the scanner to release the analysis executor and the recognizer.

Threads: results and surfaceRequest emit on the main thread. Recognition (the MrzTextRecognizer, the ML Kit band crop, parsing) runs on a background thread (Dispatchers.Default), never on the main thread. CameraX binds and unbinds on the main thread. The main thread must be free to run its queue (an app's UI thread always is).

Since

0.2.0

Parameters

appContext

an application Context, used to look up the camera provider. Pass context.applicationContext.

lifecycleOwner

the lifecycle the camera session is bound to. CameraX also starts and stops the camera as this lifecycle moves.

recognizer

the OCR engine. The default is MlKitMrzTextRecognizer. The scanner closes a recognizer that is AutoCloseable when it is closed.

mode

how tolerant candidate extraction is of OCR formatting noise. The default is ParsingMode.STRICT.

telemetry

where per-frame CameraFrameEvents go. The default is the registered sink (TelemetrySinkRegistry.current), or NoOpTelemetrySink when none is registered.

cameraSelector

which lens to use. The default is the back camera.

Constructors

Link copied to clipboard
constructor(appContext: Context, lifecycleOwner: LifecycleOwner, recognizer: MrzTextRecognizer<ImageProxy> = MlKitMrzTextRecognizer(), mode: ParsingMode = ParsingMode.STRICT, telemetry: TelemetrySink = TelemetrySinkRegistry.current, cameraSelector: CameraSelector = CameraSelector.DEFAULT_BACK_CAMERA)

Properties

Link copied to clipboard
open override val results: Flow<MrzScanResult>

Results for analysed frames and capture errors, delivered on the main thread.

Link copied to clipboard

The CameraX SurfaceRequest of the live preview, or null when no preview is active.

Functions

Link copied to clipboard
open override fun close()

Stops the session, releases the analysis executor, and closes the recognizer if closeable.

Link copied to clipboard
fun enablePreview(viewport: ViewPort? = null)

Arms the live preview for later start calls.

Link copied to clipboard

Whether the bound camera has a flash unit; false when no camera is bound.

Link copied to clipboard
fun setTorchEnabled(enabled: Boolean)

Turns the torch of the active camera on or off.

Link copied to clipboard
open override fun start()

Binds CameraX to lifecycleOwner and begins emitting on results.

Link copied to clipboard
open override fun stop()

Stops the session and unbinds this scanner's use cases.