Skip to main content

Module scan

Module scan 

Source
Available on crate feature scan only.
Expand description

Reading barcodes back out of images.

The mirror of render, meeting the rest of the crate at the same seam:

image ──▶ GrayImage ──▶ binarize ──▶ BitMatrix ──▶ Decoder::decode ──▶ payload

A Scanner locates the symbol and turns pixels back into modules; a Decoder turns modules back into a payload. Neither half knows what the other is doing, so adding a scannable symbology means implementing Decoder and describing the symbology’s character structure in SymbologyKind::linear_character, not touching this module.

§Example

use smart_package_tracker::{Barcode, RenderOptions, scan};

let png = Barcode::code128("PKG-9ED9285C")?.to_png(&RenderOptions::default())?;

let found = scan::scan_png(&png)?;
assert_eq!(found.payload(), "PKG-9ED9285C");

§What this handles, and what it does not

Reading a barcode off a photograph of a parcel and reading one out of a rendered label, a flatbed scan or a screenshot are different problems. This solves the second.

Handled: any module width; extra margin or a crop flush to the bars; the symbol anywhere in a larger image; light-on-dark as well as dark-on-light; uneven lighting across the image; moderate noise and blur; a label turned on its side, since columns are scanned as well as rows; and a printed width that drifts from nominal, because each character is measured against its own width rather than against one estimate for the whole symbol.

Not handled: rotation by anything other than a quarter turn, perspective, and the curvature of a barcode wrapped around a parcel. Those want a dedicated scanning engine, not a label-generation crate — reach for one of those if you are decoding camera frames.

Only linear symbologies can be read. There is no QR decoder here; QR encodes but does not scan.

Structs§

GrayImage
An 8-bit greyscale image: what a scanner actually works on.
Scan
A barcode found in an image.
Scanner
Reads barcodes out of images.

Functions§

scan_pngpng
Read any barcode this crate can decode out of a PNG image.
scan_png_filepng and std
Read any barcode this crate can decode out of a PNG file.