barcode-grader

Find every barcode in a PDF or image and check it against GS1: size, quiet zones

This is a fork of trulyfinal/barcode-grader, kept for reference and contributions. It is not an original project.

barcode-grader: every barcode in your artwork, checked against GS1 before it goes to print

Try it in your browser  ·  Docs  ·  Benchmark  ·  GitHub Action

CI npm MIT licence


A label dropped into the demo: three barcodes boxed on the artwork, the undersized UPC-A marked as likely to fail, its card explaining why

barcode-grader finds every barcode and 2D code in a PDF, PNG, JPEG or SVG and checks it against GS1's rules: check digit, size, bar height, quiet zones, contrast in a scanner's red light, and a predicted ISO/IEC 15416 or 15415 print-quality grade. It runs from the command line, in CI, in Node and in the browser, and the file never leaves your machine.

Used in TrulyFinal.

Why

A barcode that fails at the printer or at a retailer's audit usually failed in the artwork first: scaled to 70% to fit the panel, bars trimmed to save height, text set in the quiet zone, printed in a brand red that a scanner's red light can't see. A verifier catches that after the job is printed. This catches it in the file, before anyone signs it off.

Try it

npx barcode-grader label.pdf
Terminal output: an EAN-13 and a QR code look right; a UPC-A at 70% with short bars and no clear space on the right will likely fail

Or drop a file on the demo page. Nothing is uploaded.

What it checks

check measured against
Check digit GS1 check digit algorithm, for every GTIN, SSCC and GLN it reads
Size the X-dimension and magnification range in GS1's symbol specification tables, for the application (retail till, carton, healthcare)
Bar height GS1's minimum at the size the code is drawn; truncated bars are the most common artwork fault
Quiet zones clear space on each side, in modules, against what the symbology needs
Contrast bar and space colours as a 660 nm scanner sees them, so brand reds, yellows and cyans are judged as the till will judge them
Symbology rules GS1 Application Identifiers and their syntax, UPC-E number system, ITF-14 bearer bars, GS1 Digital Link URIs
Predicted grade ISO/IEC 15416 for linear codes and 15415 for 2D, from the artwork as drawn

Each finding says what to change in plain words ("Make it at least 80%"), with the clause behind it in the spec map. Coverage for each symbology (EAN/UPC, ITF-14, GS1-128, GS1 DataBar, QR, Data Matrix, GS1 Digital Link) is in Symbologies, including what is untested.

Measured

On the synthetic corpus in this repo: 387 files, one code each, with known ground truth. npm run bench reproduces every number below, and CI fails if any of them changes without this table changing too.

check should flag: caught should pass: false alarms
code found and read 381 / 387
check digit 12 / 12 0 / 375
size 34 / 34 0 / 336
bar height 19 / 19 0 / 366
quiet zones 23 / 27 0 / 340
colour contrast 24 / 24 7 / 349
symbology rules 14 / 14 2 / 365
digits under the code 8 / 9 0 / 378

Predicted grade against a reference ISO/IEC 15416 grade: 110 of 125 linear codes exact, 117 within one grade.

Private sets: real artwork that can't be redistributed

These were run on third-party artwork, so the files are not in this repo and you can't reproduce these two results. bench/README.md shows how to run the same scripts on your own files.

  • 77 codes on 32 real files (GS1's own guideline PDFs, photos of retail packs from Open Food Facts) added to the 387: 435 of 464 found and read.
  • 18 published food and drink labels (13 real codes): 13 read, 0 missed, 3 false alarms, all the same advisory (a QR code on a retail pack that opens a plain web address rather than a GS1 Digital Link).

The corpus is published as a dataset with its generator (bench/DATASET.md, CC0), so you can score any other tool on it.

Compared with open-source decoders

The same 387 files, given to each decoder once; barcode-grader draws each page several ways and reads each code again at print resolution. npm run bench:compare produces this table.

found and read measures size quiet zones red-light contrast predicts a grade
barcode-grader 381 ✓ ✓ ✓ ✓
zxing-cpp, 300 dpi 352
rxing, 150 or 300 dpi 351
ZBar, 150 or 300 dpi 341
Quagga2, 150 or 300 dpi (1D only) 294
OpenCV, 150 or 300 dpi 242

These are decoders, not graders, and barcode-grader reads with one of them (zxing-cpp). The table shows what looking several ways adds, and that the checking is the part none of them do. Versions, per-symbology results and the method: bench/compare/results.md.

What it is not

Use it

Command line

npx barcode-grader "artwork/**/*.pdf"                         # a pattern, a folder or files
npx barcode-grader carton.pdf --process flexo --min-grade B
npx barcode-grader artwork.png --width 50                      # printed width in mm, for a picture without a resolution
npx barcode-grader label.pdf --format html -o barcodes.html    # also json, sarif, markdown, junit

Exit code 0 when every code looks right, 1 when something needs checking or fixing, 2 when a file has no code or can't be read. Every option.

In a pull request

- uses: trulyfinal/barcode-grader@v0
  with:
    files: artwork/**/*.pdf

Fails the job when a code will likely fail to scan, writes the report into the job summary and annotates each file. SARIF for code scanning is one input away. GitHub Action.

In Node

import { gradeFile } from "barcode-grader";

const report = await gradeFile("label.pdf");
for (const code of report.codes) {
  console.log(code.name, code.value, code.verdict, code.size?.magnification, code.grade?.letter);
  for (const issue of code.issues) console.log(` ${issue.rule}: ${issue.text}`);
}

Typed, ESM, Node 20.16 or later. The JSON report has a schema. Library.

In the browser

import { configure, gradeBytes } from "barcode-grader/browser";

configure({ pdfWorkerSrc: "/pdf.worker.min.mjs", zxingWasmUrl: "/zxing_reader.wasm" });
const report = await gradeBytes(new Uint8Array(await file.arrayBuffer()));

The demo is built on this and its source is in demo/. Browser.

How it works

Finding. Each PDF page is drawn with pdf.js four times on different pixel grids (150 dpi, the same shifted half a pixel, 200 and 300 dpi on other phases), because whether a decoder finds a 100% EAN-13 at under two pixels a module depends on where its bars fall. Codes the plain picture hides are looked for again in its inverse (reversed codes), its red channel (what a till sees), a near-black-only picture (codes on dark panels) and with a fixed threshold (heavy ink spread).

Confirming. Each code is drawn again from the original at about ten pixels a module, only its own region, and read again. A 2D "code" that doesn't read again at print resolution is a pattern in type or a logo and is dropped.

Measuring. On that close-up the bars are measured along the symbol's axis: X-dimension, bar height, clear space each side in modules, bar and space colours, judged against the GS1 General Specifications.

Grading. Ten scan profiles through a circular aperture of the size GS1 names (0.150 mm for EAN/UPC), reflectance from the linear red channel, and the ISO/IEC 15416 parameters: decode, minimum reflectance, symbol contrast, edge contrast, modulation, defects, decodability. 2D symbols follow ISO/IEC 15415.

Text in PDFs is drawn from pdf.js's own fonts, not the machine's, so a file gives the same result on macOS, Linux and Windows.

Develop

npm install
npm test               # build and run the tests
npm run bench          # the synthetic corpus; node bench/check.mjs compares with the published numbers
npm run bench:compare  # the decoder comparison
npm run demo           # the web demo on a local server

Issues and pull requests get an answer within a day. The most useful report is a disagreement with a real verifier: the artwork, the predicted grade and the verifier's report for the printed code. See CONTRIBUTING and the roadmap.

Licence

MIT, copyright Agentic Flow LLC. Built on pdf.js (Apache-2.0), zxing-wasm (MIT, wrapping zxing-cpp, Apache-2.0) and @napi-rs/canvas (MIT). The benchmark corpus is CC0.