MrzRowRescue

The pure rules both platform recognizers use to read a short MRZ row again (mrz-camera's ML Kit recognizer and mrz-camera-ios's Vision saved-image recognizer), kept here so both platforms follow ONE rule and it is tested once, in this module's commonTest. It is public only so those modules can call it, and marked InternalTesseraApi: not consumer API, left out of the ABI dumps, hidden from Swift.

The problem. OCR engines collapse long runs of the < filler, so an MRZ row comes back shorter than its fixed ICAO width and the exact-width shape match downstream never fires. Typical case: TD3 line 1 (mostly filler) reads short, while line 2 (mostly characters) reads at exactly 44.

What the rescue does - selection, never invention. A row that looks like MRZ text but is shorter than an exact-width neighbour row is cropped again across the neighbour's full width and read again. Only a re-read of exact width is kept (an exact multiple is split into its rows); a short re-read is dropped. Kept lines are marked as second reads. Nothing is padded, guessed, or corrected: every kept line is a string the recognizer actually produced, and the parser's check digits remain the judge of it.

What comes after. When a name line is still short after the rescue, line detection in the shared analyse-frame core may pad it with < at the end and report it on the result (MrzFillerPadding, TES-264). That step is separate from this one, and the rescue itself never pads.

Types

Link copied to clipboard
data class Box(val left: Double, val top: Double, val right: Double, val bottom: Double)

An axis-aligned rectangle, top-left origin, in whatever pixel space the caller documents.

Link copied to clipboard
data class BoxedLine(val text: String, val confidence: Float?, val box: MrzRowRescue.Box)

One recognized line with its box, as the recognizer reported it.

Link copied to clipboard
data class RereadPlan(val row: List<Int>, val rowBox: MrzRowRescue.Box, val crop: MrzRowRescue.Box, val width: Int)

One planned re-read of a short row.

Functions

Link copied to clipboard

Maps a box from a re-read crop (scaled by scale, cut at crop's corner) back to the image's space.

Link copied to clipboard
fun isMrzCandidate(stripped: String): Boolean

Whether a row's joined, whitespace-stripped text marks it as a candidate MRZ row.

Link copied to clipboard

The lines kept from a re-read for plan. reread boxes must already be in the image's space (see fromCropSpace). Only re-read rows whose vertical centre lies inside the short row's own extent count (the padded crop can catch a sliver of the neighbour); each such row's joined text goes through rescuedLines, and only lines of the neighbour's width are kept. Confidence is the lowest of the row's fragments.

Link copied to clipboard

Plans a re-read for every near-miss row that has an exact-width MRZ neighbour row directly above or below (the one above is preferred when both are). The crop spans the neighbour's horizontal extent plus HORIZONTAL_PAD_FRACTION of its width on each side, and the short row's vertical extent plus VERTICAL_PAD_FRACTION of its height above and below, clamped to the image. A row already of exact width is never planned: re-reading it can only make it worse.

Link copied to clipboard
fun rereadScale(rowHeight: Double, cropLongestSide: Double): Int

The integer upscale for a re-read crop: enough for the row (about one character tall) to reach MIN_CHARACTER_PX, at least DEFAULT_SCALE, at most MAX_SCALE, and lowered (to no less than 1) so the scaled crop's longest side stays within MAX_SCALED_SIDE_PX.

Link copied to clipboard
fun rescuedLines(stripped: String): List<String>

The lines kept from one re-read: an exact-width read is kept, and a read whose stripped length is an exact multiple (2..MAX_SPLIT_ROWS) of one width is split into its rows in the read's own order. Anything else yields nothing. Nothing is added or dropped inside a kept read.

Link copied to clipboard
fun <T> splice(original: List<T>, replacements: Map<List<Int>, List<T>>): List<T>

Puts each rescued row's lines in place of that row's fragments: the replacement goes where the row's first fragment was, its other fragments are dropped, every other item stays as it was. A row with no replacement (or an empty one) is left untouched.