scan
@profile(t1);
import io from "io";
import fs from "fs";
import gfx from "gfx";
import image from "image";
import scan from "scan";
fn main() -> i32 {
let bytes: string = fs.read_file("ticket.jpg") or { return 1; };
var picture: gfx.Surface = image.decode(bytes) or { return 2; };
let codes: scan.Vec_Found = scan.qr(&picture);
for (i in 0..codes.len()) {
let c: scan.Found = codes.get(i);
io.println("{c.data} at ({c.corners[0].x}, {c.corners[0].y})");
}
picture.free();
return 0;
}scan reads QR codes from a gfx.Surface: a frame from a webcam, a
photo, a screenshot, anything image.decode gives. It is the
other half of barcode, which makes them. scan is a
pure module: it reads only the pixels you hand it, so it runs at compile
time too.
| Function | Does |
|---|---|
scan.qr(&surface) -> Vec_Found | Every QR code that reads; empty when none does |
scan.first_qr(&surface) -> ?string | The data of the first one, or null |
A Found:
| Field | Is |
|---|---|
data: string | The bytes the code holds, as they were encoded. A code marked ISO-8859-1 (ECI 3) comes back as UTF-8; a GS1 code has ASCII 29 ("\x1d") between its fields |
corners: [4]gfx.Point | The code’s outer corners in the picture’s pixels: first the corner at the finder square between the other two (top left on an upright code), then clockwise as the picture shows them |
kind: barcode.Kind | barcode.Kind.Qr |
A code that cannot be read is left out: it is not in the list with an error. A code damaged past its error correction gives nothing rather than wrong data.
A webcam
A phone screen held up to a laptop’s camera is the hard case: the code is
small, tilted, blurred, dim on one side, and the screen’s pixels beat
against the camera’s (moiré). scan is made for it:
- Uneven light and low contrast. Each pixel is judged against the light round it, not the whole picture, so a code half in shadow, or grey on grey, still reads.
- Tilt and perspective. The code may be turned any way and seen at an angle: it reads up to about 50 degrees off square when the camera is close, more from further away. A mirrored code (a selfie camera) reads too.
- Blur, noise, glare. The modules are sampled at several points each; glare over part of the code is damage the error correction restores.
- Light on dark. A code drawn light on a dark background (a phone in dark mode) is tried when nothing else reads.
What it needs: the whole code in the picture, its finder squares (the three big corner squares) visible, and about 2 pixels a module or more (3 or more to be safe). A version 2 code (25 x 25 modules) then needs about 75 pixels across, a long link at version 10 about 170.
It is fast enough to run on every frame: about 7 ms for a 1280 x 720
frame with a code in it in a native build, 15 to 40 ms for one without
(it tries harder before giving up). In tape run’s VM it is many times
slower: fine for a picture, not for a video.
fn on_frame(frame: *const gfx.Surface) {
let text: string = scan.first_qr(frame) or { return; };
io.println("scanned: {text}");
}What it reads
QR Code Model 2, versions 1 to 40, every error-correction level, all eight
masks; numeric, alphanumeric and byte data, ECI, GS1 (FNC1) and the
header of a structured-append part (each part reads on its own). Not
read: Kanji segments (the code is left out), Micro QR and rMQR, and the
other symbologies barcode makes.
Several codes in one picture are all found, each with its own corners.
When none reads, scan looks again with other settings (smaller and
larger neighbourhoods for its light judgement, then light on dark), so a
picture without a code takes a few times longer than one with.
Testing a reader against your own codes
barcode and scan round-trip, so a program can check what it prints:
let code: barcode.Symbol = barcode.qr("https://tapelang.org") or { return 1; };
var picture: gfx.Surface = barcode.render(&code, &barcode.Style { scale: 3; }) or { return 2; };
let back: string = scan.first_qr(&picture) or { "" }; // "https://tapelang.org"
picture.free();Last modified: