QrScanner
Fast QR code decoding for Elixir, built for full page images: scanned worksheets, rendered PDF pages and phone photos where the code can be anywhere, at any angle, surrounded by handwriting.
- Decodes with rxing (a Rust port of ZXing / zxing-cpp) through a Rustler NIF.
- Runs on a dirty CPU scheduler, so a slow image never blocks the BEAM's normal schedulers.
- Tells you how the page is turned: every code comes back with its corners,
exact angle, the quarter turn (
0/90/180/270) and the remaining skew. - Precompiled NIFs for macOS and Linux (gnu and musl), so you don't need Rust.
Installation
def deps do
[
{:qr_scanner, "~> 0.1.0"}
]
end
Usage
{:ok, [code]} = QrScanner.scan("scans/page-03.png")
code.text #=> "91GWPF-T6T000-001WT2"
code.rotation #=> 180 # page is upside down
code.angle #=> 179.52 # exact direction of the code's top edge
code.skew #=> -0.48 # tilt left after undoing the quarter turn
code.corners #=> [{299.6, 2073.9}, {142.0, 2075.2}, {140.5, 1917.7}, {298.2, 1915.9}]
Three entry points, all returning {:ok, [%QrScanner.Code{}]}, {:ok, []} when
there's no code, or {:error, reason}:
| Function | Input |
|---|---|
QrScanner.scan(path) |
a PNG or JPEG file |
QrScanner.decode(bytes) |
PNG or JPEG bytes |
QrScanner.decode_gray(pixels, width, height) |
raw 8-bit grayscale, one byte per pixel |
decode_gray/3 skips image decoding, which is most of the cost on large pages.
Raw pixels come straight out of tools like pdftoppm -gray +
magick page.pgm -depth 8 gray:page.raw, or Vix/Image.
%QrScanner.Code{}
| Field | Meaning |
|---|---|
text |
decoded payload |
corners |
[top_left, top_right, bottom_right, bottom_left] as {x, y}, in the symbol's own frame (on an upside-down page top_left is near the image's bottom-right) |
angle |
direction of the top edge (top-left → top-right), degrees clockwise, (-180, 180] |
rotation |
angle snapped to 0, 90, 180 or 270: how far the content is turned clockwise |
skew |
angle - rotation, normalised to [-45, 45] |
Straightening a page
Rotate the image by -angle to straighten it exactly, or by -rotation to only
undo the quarter turn:
{:ok, [code | _]} = QrScanner.scan(path)
System.cmd("magick", [path, "-rotate", "#{-code.angle}", "upright.png"])
Performance
46 real worksheet pages (1700×2200, 200 DPI grayscale, handwriting), turned 0/90/180/270° with up to ±1.2° tilt, Apple M-series, one decode at a time:
| Decoder | Found + correct turn | Median / page | Peak RSS over baseline |
|---|---|---|---|
QrScanner.decode_gray/3 |
46/46 | ~3 ms | ~+5 MB |
QrScanner.scan/1 (PNG) |
46/46 | ~13 ms | ~+5 MB |
| QRex 0.1.0 (PNG) | 46/46 | ~14 ms | ~+14 MB |
A page with no code returns {:ok, []} in ~6 ms (gray). On a 20 MP image of
pure noise decoding takes ~200 ms while a busy BEAM process on the only normal
scheduler kept running with gaps of ≤2 ms.
How it works
- PNG/JPEG bytes are decoded to 8-bit luma with the
imagecrate (skipped bydecode_gray/3, which reads the binary in place without copying). - rxing's zxing-cpp QR reader runs with a hybrid binarizer, looking for every QR code in the image.
- If nothing is found it tries again with rxing's
TryHardermode.
Building from source
Precompiled NIFs are downloaded automatically. To compile locally (needs Rust):
QR_SCANNER_BUILD=1 mix deps.compile qr_scanner --force
In this repository's dev and test environments the NIF is always built from
source.
License
MIT. rxing is licensed under Apache-2.0.