Guides Design & Media

How QR Codes and Barcodes Work

QR versions and sizes, the four error-correction levels, exact capacity limits, and how UPC and EAN check digits are computed, verified with the real libraries.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

QR codes and barcodes turn text or a number into a pattern a camera can read. They look unrelated, but both rely on the same two ideas: a fixed layout that a scanner can find and orient, and redundancy so that a smudge does not break the message. Create them with the QR Code Generator and Barcode Generator, and read QR codes with the QR Code Scanner.

How a QR code is built

A QR code is a square grid of dark and light modules. Three large squares in the corners (finder patterns) tell a scanner where the code is and how it is rotated; smaller alignment and timing patterns correct for skew and curvature; a quiet zone of four blank modules around the edge keeps the code separate from its surroundings.

The size is set by the version, from 1 to 40. A version v code is 17 + 4v modules square: version 1 is 21 × 21 and version 40 is 177 × 177. The generator picks the smallest version that fits your data, which the library confirms: HELLO fits version 1 (21 × 21), while a 20-character URL needs version 2 (25 × 25) at level M and version 3 (29 × 29) at level H. Shorter data gives a smaller code with bigger modules, which scans more easily.

Error correction

QR codes store extra data so that a damaged code can still be decoded. You trade capacity for resilience by choosing one of four levels:

Level Damage it can survive Use when
L ≈ 7% The code is clean and large, such as on a screen
M ≈ 15% The default for most printed codes
Q ≈ 25% The code may get dirty or scuffed
H ≈ 30% You overlay a logo, or the code is on a harsh surface

Higher levels make the code denser. A short URL at level L is version 2 while the same URL at level H is version 3.

How much a QR code holds

The maximum is version 40 at level L, and it depends on the kind of data (confirmed by generating each limit):

Data Maximum characters
Digits only 7,089
Letters (upper case), digits and a few symbols 4,296
Any bytes (for example UTF-8 text) 2,953

At level H the same version holds at most 1,273 bytes. Practical advice: keep URLs short, and use a short link rather than a long one with tracking parameters; expand a short link safely with the Short URL Expander.

How a barcode works

A linear barcode encodes digits as bars and gaps of set widths. The common retail types are UPC-A (12 digits, North America) and EAN-13 (13 digits, worldwide); Code 128 encodes full ASCII and is common in shipping. Retail barcodes end with a check digit so a scanner can spot a misread.

The check digit is easy to compute: weight the digits alternately, sum, and pick the digit that rounds the sum up to a multiple of 10.

  • EAN-13 for 590123412345: weights 1 and 3 alternately give a sum of 83, so the check digit is 10 − 3 = 7 and the full code is 5901234123457.
  • UPC-A for 03600029145: weights 3 and 1 alternately give a sum of 58, so the check digit is 10 − 8 = 2 and the full code is 036000291452.

An EAN-13 is 95 modules wide, which is why barcodes need a physical size big enough for the bars to stay readable. Print at 80% of nominal size or larger and keep a quiet zone on both sides.

Common mistakes

  • Low contrast or a coloured background. Use dark on light; inverted codes fail on many scanners.
  • Stretching a code. Keep modules square and the quiet zone intact.
  • Printing too small. A QR module should be at least about 0.5 mm; test with a real phone.
  • Using a tracking link that can break. If the link service goes away, every printed code fails.

Try these tools

See also

  • Glossary QR code
    A QR code is a two-dimensional barcode, a square grid of dark and light modules, that stores text or a link and is read by a phone camera.
  • Glossary Barcode
    A barcode is a pattern of bars and gaps that encodes a number or text so a scanner can read it quickly.
  • Glossary Checksum
    A checksum is a small value calculated from a block of data and used to detect accidental errors when the data is stored or sent.
  • Guide How Image Formats and Compression Work
    JPEG, PNG, GIF, WebP, AVIF, HEIC and SVG compared with measured file sizes, lossy versus lossless, aspect ratio and print maths.
AdSense Placeholder
Slot: footer_leaderboard