How it works

How QR Codes Work: Reading a Small One by Hand

A coffee ring, a torn corner or a logo in the middle, and the code still opens. A few specks can stop it. Here is why.

A QR code made of dark paper squares on layered pastel paper, with a torn corner and a coffee ring.

A QR code on a café table has a coffee ring across one corner. It still opens the menu. Put a thumb over the middle and it often still opens. Put the thumb over one of the three big squares in the corners and it stops.

That odd pattern of what survives and what does not is the whole design. This post takes a real, tiny QR code apart square by square, reads it by hand, and then breaks it on purpose to see what the repair data can and cannot save.

Short version:

  • A QR code is a grid of small dark and light squares. Three big squares in the corners let a scanner find it at any angle. A strip next to them says how to read the rest. The data runs in a zigzag, eight squares per byte.
  • Each code carries extra “repair bytes” (Reed-Solomon codes, the same family of maths used on music CDs). They fix whole bad bytes, not bad squares. A solid blotch is much kinder than the same number of scattered specks.
  • The four levels L, M, Q and H trade space for repair. In my test, a solid patch could cover about half of what the headline percentages suggest.

A code you can read by hand

I made a version 1 code, the smallest size, that holds the text LOOMLOOPS.COM. I used error level M, which is the middle setting and the one Denso Wave, the company that invented the code, says is chosen most often. It is 21 squares wide and 21 high. Each square is called a module.

Then I wrote a short decoder of my own that reads the code the way a scanner does, and checked every step against the library that made it. All the numbers below come from that run.

A real version 1 QR code holding LOOMLOOPS.COM, taken apart: finder patterns, timing lines, format information (error level M, mask 6) and a 26 byte data and repair area. A 4 module white margin is needed.

Finder patterns. The three big squares are each 7 modules wide: a dark ring, a light ring, then a dark 3 x 3 block. Walk a line through the middle of one in any direction and you cross dark, light, dark, light, dark in the proportion 1:1:3:1:1. That ratio is easy to spot at any angle, which is why a scanner can find the code lying sideways or upside down. Denso Wave says it picked 1:1:3:1:1 because it was the ratio least likely to turn up in ordinary printed matter, and that this is how the square marks came about. There are three of them, not four. The empty fourth corner tells the scanner which way is up.

Timing lines. Row 6 and column 6 alternate dark, light, dark, light between the finders. They give the scanner a ruler, so it knows where each module sits even if the print is a little stretched.

Format information. Every code carries a 15-bit label, written twice (once around the top left finder and once split between the other two). It holds the error level, the mask number (next section), and 10 bits of repair data for the label itself. Here is the label from my code, read from the picture:

bits as printed               100111110010111
XOR with a fixed pattern      101010000010010
result                        001101110000101

split into                    00    110    1110000101
                              |     |      '-- 10 repair bits for the label
                              |     '-- mask number 6
                              '-- error level M (the standard writes M as 00)

The fixed pattern is part of the standard. The two copies were identical in my code, as they should be.

The quiet zone. The code also needs an empty white margin, at least 4 modules wide, on every side. Denso Wave says standard QR needs “a margin of 4 module-worth at least”. A tight crop can break a code that is otherwise perfect.

The mask

If you wrote the data straight into the grid, some messages would look awful. A message of all zeros makes one huge white area, and a scanner has nothing to lock on to. Some data would also happen to draw a fake finder pattern.

The fix is a mask: one of eight fixed patterns that flips selected data modules, dark to light and light to dark. The encoder tries all eight and keeps the one that scores best on four rules: long runs of one colour, 2 x 2 blocks of one colour, finder-like patterns, and a dark share far from 50% (Nayuki’s walkthrough has the scoring). The mask number goes into the format label, so the scanner knows which one to undo.

A code of 34 zeros drawn without and with a mask, and the text LOOMLOOPS.COM drawn with each of the 8 masks. Mask 6 is highlighted yellow because this encoder picked it.

Each mask is a short rule on the row and column of a module. Mask 6, the one in my code, flips a module when ((row * column) mod 2 + (row * column) mod 3) mod 2 is 0. The full list, from the Thonky tutorial:

MaskFlip the module when
0(row + column) mod 2 = 0
1row mod 2 = 0
2column mod 3 = 0
3(row + column) mod 3 = 0
4(floor(row / 2) + floor(column / 3)) mod 2 = 0
5(row * column) mod 2 + (row * column) mod 3 = 0
6((row * column) mod 2 + (row * column) mod 3) mod 2 = 0
7(((row + column) mod 2) + ((row * column) mod 3)) mod 2 = 0

I scored all eight masks myself with the four rules and got mask 1 as the winner, not mask 6. The library picked 6. My scorer counted the format label modules, and the standard scores the symbol without the format and version information, which the library follows. It does not matter for scanning, because the label always says which mask was used. Only the data and repair modules are masked. The finders, timing lines and format label are left alone.

The zigzag

The data modules are read in strips two modules wide. The path starts in the bottom right corner, goes up one strip, turns, goes down the next, and so on. It jumps over the timing column and the corner patterns.

The reading path on the version 1 code: from the bottom right, up and down strips 2 modules wide. The first 8 modules read 00100000 after undoing mask 6, and 0010 means letters and digits mode.

Take the first eight modules in that order. Some are printed dark, some light. Mask 6 flips some of them. After undoing the mask, the bits are 00100000, which is the number 32. A scanner now has its first byte.

The first four bits, 0010, name the mode. Mode 0010 means alphanumeric: digits, capital letters and a few symbols (space and $ % * + - . / :). The next 9 bits, 000001101, are the length: 13 characters, and LOOMLOOPS.COM is 13 characters. Then the text, two characters at a time in 11 bits. Give each character a number (0 to 9 are themselves, A is 10, L is 21, O is 24) and combine a pair as first * 45 + second:

"LO"  ->  21 * 45 + 24  =  969  ->  01111001001

Those 11 bits are exactly the next 11 modules after the length. Two characters in 11 bits is 5.5 bits per character, against 8 for plain text. After the last character come 0000 (an end marker), a few zeros to fill the byte, and then padding bytes that alternate 11101100 and 00010001 (236 and 17) until the data area is full.

My code holds 26 bytes in total. The first 16 are data. The mode (4 bits), the length (9 bits), the text (72 bits) and the end marker make 89 bits. Zeros fill that out to 12 bytes, and 4 padding bytes complete the 16. The last 10 bytes are the repair bytes.

Repair bytes: Reed-Solomon in plain words

Here is the idea with a school-maths picture. Two points fix a straight line. Three points fix a parabola. Now say the message is three numbers, and you send them as three points on a parabola. If you also send two more points on the same parabola, the receiver gets five points. Lose any two of them and the three left still fix the one parabola that passes through them all. The two missing points can be worked out again.

Reed-Solomon codes do this with bytes instead of points on a drawing. The data bytes are treated as the numbers of a polynomial (the curve), and the repair bytes are extra points, worked out by polynomial division in a small number system with exactly 256 values (one value for each possible byte). The division remainder is the repair bytes. Irving Reed and Gustave Solomon described these codes in a short paper in 1960. Denso Wave’s own page says the repair data needs twice as many codewords as the number it can correct, and that the same maths is used for music CDs.

A JPEG goes the other way: it throws detail away on purpose to save bytes, and re-saving one shows what that costs.

So the maths lets 10 repair bytes fix up to 5 wrong bytes when nobody says which bytes are wrong. The standard promises a little less for the smallest codes (4 for this one, more on that below). I tried it on the 16 data bytes of my code, with the reedsolo library set up the way the QR standard sets it up (the number system’s constants 0x11D and 2, which Thonky’s tutorial lists):

import reedsolo

data = bytes([32, 107, 201, 137, 207, 38, 40, 209, 100, 104, 176, 0, 236, 17, 236, 17])
rs = reedsolo.RSCodec(10, fcr=0, prim=0x11D, generator=2)

packet = bytearray(rs.encode(data))
print(list(packet[16:]))            # the repair bytes

for i in (0, 3, 6, 9, 12):          # wreck 5 bytes
    packet[i] ^= 0xFF
print(bytes(rs.decode(bytes(packet))[0]) == data)

packet[15] ^= 0xFF                  # wreck a 6th
try:
    rs.decode(bytes(packet))
except reedsolo.ReedSolomonError as e:
    print("failed:", e)

It printed:

[155, 45, 21, 124, 34, 233, 128, 65, 215, 165]
True
failed: Too many errors to correct

The ten repair bytes match the last ten bytes my decoder read out of the printed code. My decoder fixed five wrecked bytes and the text came back. A sixth stopped it.

Reed-Solomon repair on the version 1 code’s 26 bytes: 16 data, 10 repair. With 5 bytes overwritten the decoder rebuilds the text; with 6 it reports an error. The standard promises 4 for level M.

Four levels: L, M, Q and H

The error level sets how many repair bytes a code gets. The names stand for Low, Medium, Quartile and High, and the headline figures are about 7%, 15%, 25% and 30% of the bytes. Denso Wave says higher levels correct more but make the code bigger, level L suits clean places with a lot of data, and Q or H suit dirty places.

In my version 1 code, the same 26-byte frame is split like this:

LevelData bytesRepair bytesMaths allowsStandard promises
L19732
M161054
Q131366
H91788

Data and repair counts are from the Thonky error correction table. “Maths allows” is half the repair bytes, rounded down: the number of wrong bytes that can be fixed when nobody says which bytes are wrong. The smallest versions keep a byte or two of the repair data back to catch a wrong repair, so the standard promises a little less for L and M. The right-hand column is second-hand: the standard is paywalled and I did not read it. My decoder was not bound by it and fixed 5 bytes at level M.

Bigger codes do not use one long Reed-Solomon word. They split the data into several blocks, each with its own repair bytes, and then interleave the bytes of the blocks. A version 7 code at level H, for example, has five blocks that can each fix 13 bytes. Interleaving is what makes a solid blotch survivable: the damaged bytes sit next to each other in the printed code, but they come from different blocks, so each block only sees a few of them.

Versions and capacity

There are 40 versions. Version 1 is 21 x 21 modules, and each version adds 4 modules per side, so the side is 4 x version + 17. Version 40 is 177 x 177.

Four QR codes at one scale, level M: version 1 (21 by 21 modules) holds 14 bytes, version 2 holds 26, version 7 holds 122, version 40 (177 by 177) holds 2,331. Side is 4 times version plus 17.

Two things appear as a code grows. From version 2, small alignment patterns (a 5 x 5 square) help the scanner follow a bent or tilted print. From version 7, the version number itself is written twice near the finders, with repair bits. Version 1 does not need either.

How much fits depends on the version, the level and the mode. Here is the capacity in byte mode (plain 8-bit text, the mode most URLs use), from every version and level:

Line chart of QR capacity in bytes against version 1 to 40, one line per error level. At version 40: L 2,953, M 2,331, Q 1,663, H 1,273. Each level up costs capacity.
VersionSideLMQH
121 x 211714117
1057 x 57271213151119
25117 x 1171,273997715535
40177 x 1772,9532,3311,6631,273

I took these from the tables published at thonky.com, then re-derived all 160 numbers from the standard’s codeword counts with the segno library. They matched. The standard is ISO/IEC 18004. The 2024 edition is the current one. It replaced the 2015 edition, which ISO lists as withdrawn since August 2024. Both are paywalled and I read neither. The capacity numbers I used are the 2015 ones as published, and they are the same in the 2024 edition.

Digits-only text uses numeric mode, which packs three digits into 10 bits. The same version 40 level L code that holds 2,953 bytes holds 7,089 digits.

A practical effect: fewer bytes means a smaller version, which means bigger modules at the same print size, which scan more easily. My page’s address, https://loomloops.com/post/how-qr-codes-work/, is 45 characters. At level M it needs version 4 (33 x 33). Written in capitals it can use alphanumeric mode, and the same address fits in version 3 (29 x 29). The catch: the host name does not care about case, but the path on many servers does, so only do this when you know the path survives it.

When it breaks

A solid patch against scattered specks

The repair bytes count bytes, and a byte is 8 modules. Flip one module and its whole byte is wrong. So 34 flipped modules scattered over a version 7 code (196 bytes in total) hit about 31 different bytes. The same 34 modules in one blotch would hit only a handful.

I tested it. I took a version 7 code that holds loomloops.com/qr, flipped random data modules, and tried to read each result with OpenCV’s QR reader, 20 tries per setting:

Modules flippedLMQH
34 (2% of the 1,719 I allowed)0 of 203 of 2017 of 2020 of 20
68 (4%)0 of 200 of 200 of 205 of 20
6% or more0 of 200 of 200 of 200 of 20

I let it flip any module outside the corner patterns, the timing lines and the centre pattern. A speckle of 34 squares in a 2,025-square code is nothing you would notice by eye. At level L it was enough to stop every read.

A solid patch in the middle

Now the case that matters for logos. I covered a square in the middle with white, made it one module larger each time, and noted the largest square that still read. Five random texts per level, the median, on versions 4, 7 and 10.

Bar chart for a version 7 QR code: the maths can repair 10 percent (L) to 33 percent (H) of bytes, but the biggest centred patch a scanner still read was only 6 to 16 percent of the area.
VersionLevelBytes the maths can fixBiggest patch that still read
4L10%4.5% of the area
4M18%9.2%
4Q26%11.1%
4H32%15.5%
7L10%6.0%
7M18%9.7%
7Q28%12.6%
7H33%16.0%
10L10%6.0%
10M19%13.6%
10Q28%20.8%
10H32%22.4%

The second column is the total repair capacity divided by the total bytes, using the block sizes from the standard’s table. The third is what I measured. In every row the patch is smaller than the maths limit, between about half and three quarters of it. Three reasons I can think of, none of which I isolated:

  • A patch also spoils the bytes along its edge, so it hits more bytes than its area suggests.
  • A reader may not use every repair byte it could.
  • Versions 7 and 10 have an alignment pattern in the centre, and a patch that covers it may confuse a reader that leans on it.

Two cautions about my numbers. They come from one reader, and phone scanners differ. Also, that reader failed on 2 of the 60 undamaged codes I generated (both version 10), which is why I used medians. Treat the table as a picture of the shape, not as a promise.

A separate run with loomloops.com/qr at version 7, level H gave the same picture: a 19 x 19 patch (17.8% of the area) read, a 20 x 20 patch (19.8%) did not, and a 25 x 25 patch (30.9%, the “30%” people quote) did not either.

So “a logo can cover 30%” is the wrong way to read the level H figure. In my test about a sixth of the area was a safe size, and a designer who wants a logo in the middle should pick level H, keep the logo small, and try the result on real phones. Denso Wave’s own answer for pictures is a separate variant, Frame QR, which leaves a canvas area for text or images.

Hiding a finder pattern

I covered the top left finder (with its white edge) on a version 7, level H code. The reader failed. The repair bytes protect the data, not the three squares used to find the code, so no level saves a code that has lost one.

Which level should you use?

Level LLevel MLevel QLevel H
Good forClean screens, lots of dataA default for most printsPrints that get scuffedLogos in the middle, rough surfaces
CostSmallest codeSmallBiggerBiggest. At version 10, 119 bytes against 271 for L
Watch out forA few specks can stop it (0 of 20 reads at 2% in my test)Same, a little more roomA patch of 11 to 21% of the area, depending on version, in my testA bigger code means smaller modules at the same print size

Questions people ask

How do QR codes work? A scanner finds the three corner squares, which give position, rotation and size. It reads the format label to learn the error level and mask. It undoes the mask, reads the modules in a zigzag to get bytes, fixes bad bytes with the repair bytes, and then decodes the result as numbers, letters or bytes.

Why does a QR code still scan when it is torn or has a logo on it? Each code carries repair bytes worked out from the data with Reed-Solomon maths. If some bytes are wrong, the scanner can rebuild them. It only works up to a limit, and the limit counts bytes, not area. In my test, a solid patch of 16% of the area was about the most a version 7 level H code took.

What do error correction levels L, M, Q and H mean? They are four settings for how many repair bytes a code carries. L has the fewest and M is the common default. Q and H add more repair bytes, and the code gets bigger. The usual figures are about 7%, 15%, 25% and 30% of the bytes.

How much data can a QR code hold? At most 2,953 bytes of plain text, or 7,089 digits, in a version 40 code at level L. A higher level holds less: 1,273 bytes at level H. Short messages need a much smaller code, and a smaller code is easier to scan.

Can a QR code be scanned upside down or at an angle? Yes. The three finder squares have the ratio 1:1:3:1:1 along any line through their middle, so a scanner can find them at any angle, and the missing fourth corner tells it which way is up. Denso Wave says one aim was reading from any of 360 degrees.

Cheat sheet

QR code cheat sheet: finder patterns, timing lines, format info, a mask, zigzag reading and Reed-Solomon repair bytes. Version 40 at level L holds 2,953 bytes. My level H test tolerated a 16 percent patch.

Further reading

Written by Rafi Loom

Backend engineer who builds high-volume systems that move money. More about Rafi