QR Codes and Barcodes

Barcodes turn short strings into bars or square grids that a scanner can read back. Here is how the common formats are built and how check digits catch errors.

One dimension or two

A 1D barcode stores data in the widths of parallel bars and spaces. A scanner reads it along a single line. Most 1D formats hold a short string, often a product number.

A 2D code stores data in a grid of dark and light squares, so it reads in both directions and holds far more. QR codes are the best-known example, and other 2D formats such as Data Matrix and PDF417 work on the same grid or stacked-row idea.

Retail product codes: UPC-A and EAN-13

UPC-A, common on products in the United States and Canada, encodes 12 digits. EAN-13, used widely elsewhere, encodes 13. A UPC-A number can be turned into an EAN-13 number by adding a leading zero, and scanners in shops read both. In each case the last digit is a check digit computed from the others, so a scanner can catch most misreads.

The check digit algorithm

For EAN-13, take the first 12 digits. Multiply digits in odd positions (1st, 3rd, 5th and so on) by 1 and digits in even positions by 3. Add the products. The check digit is the amount needed to bring that sum up to the next multiple of 10, or 0 if the sum already is one. Here is a worked example for 400638133393, computed in Python:

PositionDigitWeightProduct
1414
2030
3010
46318
5313
68324
7111
8339
9313
10339
11919
12339

The products add up to 89. The next multiple of 10 is 90, so the check digit is 1 and the full code is 4006381333931.

UPC-A uses the same idea with the weights the other way round, because it has one digit fewer. For the 11 digits 03600029145, the digits in odd positions sum to 14 and are multiplied by 3, giving 42. The even positions sum to 16. The total is 58, so the check digit is 2 and the full code is 036000291452. Adding a leading zero and running the EAN-13 method gives the same check digit, 2.

To validate a complete code, apply the weights to all 13 digits, check digit included. A valid code gives a total that is a multiple of 10. Changing one digit, as in 4006391333931, makes the check fail.

def valid_ean13(code):
    total = sum(int(d) * (1 if i % 2 == 0 else 3)
                for i, d in enumerate(code))
    return total % 10 == 0

valid_ean13("4006381333931")   # True
valid_ean13("4006391333931")   # False

Code 128 and Code 39

Code 39 is an older format that encodes uppercase letters, digits and a few symbols. It is simple and forgiving to print, but it is wide for the amount of data it holds.

Code 128 can encode all 128 ASCII characters and packs pairs of digits densely, so it is more compact than Code 39. It always includes a check character. See character encoding for what ASCII contains.

How a QR code is laid out

The small squares in a QR code are called modules. Three corners carry large square finder patterns that let a scanner find the code and work out its rotation. Narrow timing patterns of alternating modules run from one finder to the next, and areas near them hold format information such as the error correction level and mask.

QR codes come in versions 1 to 40. Version 1 is 21 × 21 modules, and each version adds 4 modules per side, so the side length is 17 + 4 × version.

VersionFormulaModules
117 + 4 × 121 × 21
217 + 4 × 225 × 25
517 + 4 × 537 × 37
1017 + 4 × 1057 × 57
2017 + 4 × 2097 × 97
4017 + 4 × 40177 × 177

Error correction levels

QR codes use Reed-Solomon error correction, so a code can still be read when part of it is dirty, torn or covered. You choose one of four levels when you create the code. Higher levels recover more damage but leave less room for data at a given version.

LevelApproximate damage it can recover
Labout 7%
Mabout 15%
Qabout 25%
Habout 30%

A logo placed over the middle of a code destroys some modules, and error correction fills them back in, so use a high level for that.

Encoding modes

A QR code can switch modes to store data efficiently:

  • Numeric: digits 0 to 9 only, the most compact mode.
  • Alphanumeric: digits, uppercase letters, the space, and a few symbols such as $ % * + - . / :.
  • Byte: arbitrary 8-bit data, which is how most text and links are stored, usually as UTF-8.
  • Kanji: Japanese characters stored in a compact double-byte form.

The quiet zone

Every barcode needs a blank margin around it called the quiet zone. For QR codes the standard margin is 4 modules wide on every side. Text, borders or a busy background that intrude into this space are a frequent reason a code will not scan.

Checklist before you print a code

  • Use dark modules on a light background. Inverted or low-contrast colors fail on many scanners.
  • Keep the full quiet zone, and do not crop the code to the edge of a label.
  • Print large enough for the scanning distance, and test at the real size, not on screen.
  • Keep the data short. Less data means a lower version with bigger modules, which scans more easily.
  • For retail barcodes, verify the check digit in code before sending artwork to print.
  • Scan the final proof with more than one device before printing in bulk.