EAN-13, UPC-A or Code 128 on a Product Label? Check Digits, Quiet Zones and Print Size, Tested With a Simulated Scanner
Most product-label barcode problems come down to three questions. Which format is this? Is the number right? Will it scan once it's printed at its real size? A barcode generator answers the first question for you and par
Most product-label barcode problems come down to three questions. Which format is this? Is the number right? Will it scan once it's printed at its real size? A barcode generator answers the first question for you and part of the second. The third depends on the file you take away and on what you do with it in your layout app.
I made barcodes in five formats with the free FindUtils Barcode Generator, downloaded the PNG and SVG for each one, and decoded them with zxing-cpp, a widely used open-source barcode library. Then I checked the check-digit maths with every possible typo, worked out what the pixel sizes become on paper, and ran the codes through a simulated scanner with busy artwork placed close to the bars.
As in the QR code print test, a simulated scanner is not a checkout scanner. The simulation is useful for comparisons (this layout reads, that one doesn't). The size rules come from GS1, the organisation that runs the retail barcode system, not from my simulation.
Pick the format by where the label will be scanned
| Format | What it holds | Where it's used | Bars (modules) in my test |
|---|---|---|---|
| EAN-13 | 12 digits + check digit | Retail products, worldwide | 95 |
| UPC-A | 11 digits + check digit | Retail, mostly the US and Canada | 95 |
| EAN-8 | 7 digits + check digit | Very small retail packs | 67 |
| Code 128 | Any printable ASCII | Your own SKUs, bins, shipping labels | 178 for SKU-0042-BLUE
|
| Code 39 | Capitals, digits, -.$/+% and space |
Older warehouse and asset systems | 194 for SKU-0042-BLUE
|
The important split is between the retail formats and the rest. A checkout scanner expects EAN-13 or UPC-A carrying a GTIN, a product number that comes from a company prefix you license from GS1. You can't make one up. A barcode that decodes perfectly but carries a number nobody assigned to you still won't be accepted by a retailer. GS1 explains this step in how to get a GTIN. For labels that only your own systems read (stock bins, internal SKUs, returns), use Code 128 and put whatever text you like in it.
UPC-A and EAN-13 are closer than they look. When I decoded the UPC-A for 036000291452, zxing-cpp reported it as EAN-13 0036000291452. A UPC-A is an EAN-13 whose first digit is 0. The bars are identical, which is why a US code scans anywhere in the world. If your database stores both forms, normalise them first (pad to 13 or 14 digits). Otherwise the same product can show up as two different items.
The check digit: 12 lines, and what it doesn't catch
All the retail formats end in a check digit. You multiply the digits by 3 and 1 alternately, starting from the right, and pick the last digit that brings the total to a multiple of 10. GS1 publishes the manual method. Here is the same thing in Python:
# GS1 check digit (EAN-13, UPC-A, EAN-8, GTIN-14): weights 3,1,3,1... from the right, ignoring the check digit.
def check_digit(body: str) -> int:
total = sum(int(d) * (3 if i % 2 == 0 else 1) for i, d in enumerate(reversed(body)))
return (10 - total % 10) % 10
def is_valid(code: str) -> bool:
return code.isdigit() and len(code) in (8, 12, 13, 14) and check_digit(code[:-1]) == int(code[-1])
if __name__ == '__main__':
for body in ['400638133393', '03600029145', '9638507']:
print(body, '->', body + str(check_digit(body)))
print('UPC-A 036000291452 valid:', is_valid('036000291452'), '| as EAN-13 0036000291452 valid:', is_valid('0036000291452'))
400638133393 -> 4006381333931
03600029145 -> 036000291452
9638507 -> 96385074
UPC-A 036000291452 valid: True | as EAN-13 0036000291452 valid: True
The generator gave the same three results. With 12 digits for EAN-13, 11 for UPC-A or 7 for EAN-8, it added the check digit. When I pasted a full EAN-13 with the wrong last digit (4006381333932), it showed "EAN-13 check digit should be 1" and the download buttons stayed disabled. That's the right behaviour: it won't quietly fix the number and give you bars for a code that isn't in your product database.
So what does a check digit actually protect you from? I took 2,000 random 12-digit numbers, added their check digits, and tried every possible single-digit change and every swap of two neighbouring digits:
single-digit errors caught: 234000/234000 (100.0%)
adjacent swaps caught: 19223/21588 (89.0%); missed pairs: ['05', '16', '27', '38', '49']
Every single wrong digit was caught. About one swap in nine wasn't, and they were always the same pairs: digits that differ by exactly 5 (0 and 5, 1 and 6, and so on). Swapping them changes the weighted sum by a multiple of 10, so the check digit can't tell. A "valid" number has the right shape. It's not proof that it's the right number. When people type product numbers by hand, compare against your product list as well. The FindUtils article what a barcode check digit can and cannot prove goes further into this.
A numeric Code 128 can be much shorter
Code 128 has three character sets. Set B covers letters, digits and punctuation. Set C packs two digits into one symbol, so long runs of digits take about half the width. I compared the generator's output with python-barcode, which switches to set C for digit runs:
000123456789 python-barcode: 101 modules; all-code-B would be 167
SKU-0042-BLUE python-barcode: 178 modules; all-code-B would be 178
0042 python-barcode: 57 modules; all-code-B would be 79
The FindUtils download for 000123456789 was 167 modules wide, which is the set B width (its barcode formats cheat sheet says so: "Code 128 subset B"). For a mixed SKU like SKU-0042-BLUE it makes no difference: 178 modules either way. For a 12-digit order number, set B is 65% wider at the same bar size. On a small shipping label you have two options: use an encoder that does set C, or accept a smaller bar width for the same label space (and check the scan distance).
Quiet zones: the generator's margin is fixed
Every linear barcode needs clear space on both sides, the quiet zone, measured in modules (the width of the thinnest bar). GS1's table for retail products asks for 11 modules on the left and 7 on the right for EAN-13, 9 and 9 for UPC-A, 7 and 7 for EAN-8. Code 128 needs at least 10 on each side.
I measured the white space in each download (output shortened):
EAN-13 bar 2px: PNG 230x164 bars 95 modules quiet zone L 10 / R 10 modules (spec 11/7)
EAN-13 bar 4px: PNG 420x164 bars 95 modules quiet zone L 5 / R 5 modules (spec 11/7)
UPC-A bar 2px: PNG 230x164 bars 95 modules quiet zone L 10 / R 10 modules (spec 9/9)
Code 128 bar 2px: PNG 396x164 bars 178 modules quiet zone L 10 / R 10 modules (spec 10/10)
Code 128 bar 4px: PNG 752x164 bars 178 modules quiet zone L 5 / R 5 modules (spec 10/10)
The margin is always 20 pixels, whatever the bar width. The generator guide says the same: it's a fixed margin, not a quiet-zone check. At the default 2-pixel bars that's 10 modules: fine for UPC-A, EAN-8 and Code 128, one module short on the left of an EAN-13. At 4-pixel bars it's 5 modules, short for every format. All of the downloads decoded on their own. The problem shows up when you place the file in a layout and the artwork moves in close.
How much does that matter? I put an EAN-13 next to random dark stripes (like text or a photo edge), made the clear space narrower on one or both sides, and ran 50 noisy scans for each width:
Quiet zone test (3 px per module, 50 random artworks each):
narrow left 0:5/50 1:7/50 2:14/50 3:16/50 5:50/50 7:50/50 9:50/50 11:50/50
narrow right 0:14/50 1:25/50 2:50/50 3:50/50 5:50/50 7:50/50 9:50/50 11:50/50
narrow both 0:0/50 1:4/50 2:4/50 3:16/50 5:31/50 7:50/50 9:50/50 11:50/50
The decoder was more forgiving than the standard: on one side, 5 modules of clear space was enough every time. With both sides narrowed to 5, only 31 of 50 scans read, and anything under 3 modules mostly failed. The left side was the sensitive one, which fits GS1 asking for more space there. Treat the 11/7 rule as the target and these numbers as the reason not to shave it. A real checkout scanner, reading at an angle off a curved pack, has less to work with than a clean simulated image.
Pixels are not millimetres
GS1 sets the size of an EAN-13 or UPC-A by its module width, the X-dimension: 0.330 mm is nominal (100%), and the allowed range for retail is 0.264 mm (80%) to 0.660 mm (200%). At 100%, an EAN-13 with its quiet zones is 37.29 mm wide.
A PNG has no size on paper until something decides how many pixels go into a millimetre. The generator's PNGs carry no DPI tag, so your layout app picks the size. If it places one pixel per printer dot, this is what you get (selected lines):
300 dpi, 2 px -> X = 0.169 mm = 51% of nominal, EAN-13 incl. quiet zones 19.1 mm wide: below 80% minimum
300 dpi, 3 px -> X = 0.254 mm = 77% of nominal, EAN-13 incl. quiet zones 28.7 mm wide: below 80% minimum
300 dpi, 4 px -> X = 0.339 mm = 103% of nominal, EAN-13 incl. quiet zones 38.3 mm wide: in range
600 dpi, 5 px -> X = 0.212 mm = 64% of nominal, EAN-13 incl. quiet zones 23.9 mm wide: below 80% minimum
The default 2-pixel bars at 300 dpi are about half the smallest size retail allows. On a 600 dpi printer, even the largest bar width (5 px) is too small. This is the most likely way to get a barcode that scans from your screen and fails on the shelf.
The other trap is resizing to fit a box. Bars have to be whole printer dots, so a module of 3.9 dots doesn't exist. When I stretched a 2-pixel PNG by 1.3 (nearest neighbour, as many apps do for crisp edges), the one-module bars came out 2 or 3 pixels wide, so some thin bars were 50% wider than others. zxing-cpp still read it on screen. A print verifier, which grades bar-width errors, would mark it down, and a tired scanner might fail it.
For printers, round the module to whole dots:
203 dpi: 2.64 dots wanted, use 3 -> X = 0.375 mm (114%)
300 dpi: 3.90 dots wanted, use 4 -> X = 0.339 mm (103%)
600 dpi: 7.80 dots wanted, use 8 -> X = 0.339 mm (103%)
(203 dpi is common on thermal label printers. There, 2 dots gives 0.250 mm, 76%, which is outside the normal range.)
A recipe that comes out right at 300 dpi
- In the generator, choose EAN-13, enter your 12 assigned digits, keep Bar width 2, and set Height 140. Download the SVG.
- In your layout app, place the SVG at 38.95 mm wide with the proportions locked. The file is 230 units wide (95 modules of bars plus two 20-unit margins at 2 units per module), so each module becomes 0.339 mm, exactly 4 dots at 300 dpi.
- The bars become 23.7 mm tall. GS1's minimum for that X-dimension is about 23.5 mm. Don't crop the bar height to save space.
- Keep artwork 3.73 mm away on the left (11 modules) and 2.37 mm on the right (7 modules). The file's own margin on the left is one module short, so this clear space has to come from your layout.
- Dark bars on a light background. No inverted codes, no red bars (red scanner light makes red look white).
- Print one label at real size on the real material and scan it with a real scanner, not only a phone app. If a retailer requires a verification report, get one; a phone that reads the code doesn't grade it.
What this doesn't test
- Real checkout scanners and print. The scans were synthetic images with noise and blur. Ink spread, glossy film, curved packs and scan angles were left out.
- Other symbologies. No UPC-E, ITF-14, GS1-128 or 2D codes. The generator's Code 128 is plain Code 128, not GS1-128 with application identifiers.
- Your printer's driver. Some drivers resample images anyway. Check the printed bars with a loupe if a label won't scan.
The FindUtils Barcode Generator runs in your browser. The generator guide covers each export option, and the formats cheat sheet lists what each format accepts. For more free tools, see findutils.com.
Need help with a project? The team behind FindUtils takes on freelance work on Upwork, including a fast REST API or back-end service built in Go, for example a small service that validates product numbers and returns print-ready barcode files at the right size.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.