From 1251aab9f8f9f38aa5bdefd4fed1f58236166cf8 Mon Sep 17 00:00:00 2001 From: cirakg Date: Tue, 20 Oct 2026 11:24:58 +0200 Subject: test(codes): compare Data Matrix and PDF417 with references and decode them, also from screenshots Co-Authored-By: Claude Opus 5.5 --- tools/barcode_ref.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'tools/barcode_ref.py') diff --git a/tools/barcode_ref.py b/tools/barcode_ref.py index f56a7c3..9c4c6d5 100644 --- a/tools/barcode_ref.py +++ b/tools/barcode_ref.py @@ -14,9 +14,12 @@ from typing import Any, cast import zxingcpp from PIL import Image -QR, CODE128, CODE39, EAN13, EAN8, UPCA, UPCE, ITF, CODABAR = range(9) +QR, CODE128, CODE39, EAN13, EAN8, UPCA, UPCE, ITF, CODABAR, DATAMATRIX, PDF417, AZTEC = range(12) FORMATS = { QR: zxingcpp.BarcodeFormat.QRCode, + DATAMATRIX: zxingcpp.BarcodeFormat.DataMatrix, + PDF417: zxingcpp.BarcodeFormat.PDF417, + AZTEC: zxingcpp.BarcodeFormat.Aztec, CODE128: zxingcpp.BarcodeFormat.Code128, CODE39: zxingcpp.BarcodeFormat.Code39, EAN13: zxingcpp.BarcodeFormat.EAN13, @@ -55,15 +58,17 @@ def render_linear(modules: str, scale: int, height: int = 40) -> Image.Image: READ_FORMATS = {**FORMATS, CODE39: zxingcpp.BarcodeFormat.Code39Std, UPCA: zxingcpp.BarcodeFormat.EAN13} -def render_matrix(packed: bytes, size: int, scale: int, quiet: int) -> Image.Image: - """A square matrix (row-major bits, 8 per byte) as black modules on white with a quiet zone.""" - side = (size + 2 * quiet) * scale - image = Image.new("L", (side, side), 255) - for i in range(size * size): +def render_matrix(packed: bytes, shape: tuple[int, int], scale: int, *, quiet: int, row_height: int = 1) -> Image.Image: + """A matrix (row-major bits, 8 per byte; shape = width and stored rows, + each row row_height modules high) as black modules on white with a + quiet zone.""" + size, rows = shape + image = Image.new("L", ((size + 2 * quiet) * scale, (rows * row_height + 2 * quiet) * scale), 255) + for i in range(size * rows): if packed[i // 8] & (0x80 >> (i % 8)): x, y = i % size, i // size - left, top = (quiet + x) * scale, (quiet + y) * scale - image.paste(0, (left, top, left + scale, top + scale)) + left, top = (quiet + x) * scale, (quiet + y * row_height) * scale + image.paste(0, (left, top, left + scale, top + scale * row_height)) return image -- cgit v1.2.3