Ch.20: Algorithms: Why Damaged QR Codes Still Scan

Outline

Transcript

0:00 Imagine a cashier waiting while a customer's payment link won't open. A damaged QR code can lose part of its message and still return the exact link. In a controlled digital test, a six-square-wide center wipe still decoded. A much smaller mark in a corner stopped both readers. How can a reader repair the message without knowing what was there? It needs more than a checksum saying something broke. Reed–Solomon error correction adds information that can identify a wrong value and rebuild it. We'll walk through repairing one, then come back to those two marks.

0:32 My first thought was that the code had another copy of the link tucked away. Finding the bad value sounds harder than filling a blank. It is. The reader may see a perfectly ordinary number and have no label saying it's wrong. First, those wrong numbers start as black and white squares. A square is called a module. Once the reader has located the grid, it samples modules into bits; 8 bits make 1 byte-sized codeword. The repair works on those values, not on a photograph of the damaged patch. Right. Several damaged squares can belong to the same codeword.

1:06 Our center wipe changed 20 dark squares across seven codewords, so seven values needed repair. The visible area doesn't tell you that count. Yeah, seven wrong values are too many to juggle aloud. Can we shrink the same idea to one error and show how the reader finds it? Let's use a tiny version with four stored values. What information should the sender put into them? Next, let's say we encode a two-number message: a starting value of two and a step of three. Both are the message. We'll store four samples of the rule instead of storing just those two numbers.

1:43 The receiver knows the sample positions and the kind of rule, but not our start or step. To keep each value small, our arithmetic wraps at seven, like a clock with labels zero through six. Two plus three gives five. Add three again: eight wraps back to one. The next two samples are four and zero. So the stored row is five, one, four, zero, at positions one through four. So it has four clues for two unknowns. What happens if the 3rd stored value becomes six? Now take a corrupted row: five, one, six, zero.

2:19 I don't trust any particular entry yet. Our small decoder tries pairs of positions, builds the constant-step rule fitting each pair, and checks that rule against the whole row. It accepts a rule only if at least three positions agree. Try the first two entries. Five to one is a step of three in our wraparound arithmetic. That rule predicts four at the 3rd position, but we received six. It predicts zero at the 4th, which does agree. Right. Three agreements and one mismatch. The rule says the 3rd value should be four.

2:53 Follow it backward from five and you recover the starting value 2. Our message was two followed by three. Yeah, we found the position and the replacement. But that worked because our first pair happened to be good. What if the pair includes the corrupted entry? But a bad entry can give us a convincing candidate. For example, try the second and 3rd positions: one followed by six. That suggests a step of five. It fits both entries we used. Wait, that six still fits a consistent rule? Does that candidate survive the other two positions?

3:27 No. It predicts three in the first slot, where we received five. The last slot disagrees too. Two agreements, two mismatches. Reject it. So checking only the pair would have fooled us. Checking the other entries exposes it. The decoder needs a rule matching at least three entries. And that requirement gives us one answer. Our candidate matches positions one, two and four. Imagine a rival matching one, three and four. Both would have to fit the same values at positions one and four. Two shared positions.

3:59 Those already pin down the start and step in our seven-number arithmetic. The supposed rival would be the same rule. Yes. Any two sets of three positions out of four share at least two. So a genuinely different rule can't also pass. We've identified the wrong entry and recovered what we sent, without a spare copy of it. That makes the extra values more interesting. They don't just give us something to compare against. They rule out every other message close enough to be a one-error repair. Now suppose the last two entries arrive explicitly marked missing, while positions one and two are known good.

4:36 Five and one already determine our start and step, so we can fill both blanks. Two known blanks, versus one unmarked mistake. It's like two empty boxes on a four-entry form: they tell you where to work. One plausible typo makes you find the bad box first. That convincing six looked just as ordinary as the others. Exactly. This small code can recover two known missing entries or one unknown wrong entry. But a scratch on a QR doesn't automatically tell the decoder which byte values are unreliable.

5:09 Now return to the center wipe. QR uses polynomial rules, which can include squared and higher powers rather than only a start and step. Its arithmetic works on bytes. Our pair search showed recovery; production readers solve equations to find several errors and their replacements. Our test symbol stores 16 data codewords and 28 check codewords in one repair group. Its guaranteed budget is 14 unknown wrong codewords, or up to 28 known missing ones. That's the same two-for-one distinction, scaled up.

5:41 And the six-by-six wipe changed seven codewords in that group. Both software readers recovered the exact test link. Widen the wipe to 10 by 10 and 15 codewords changed; neither reader returned the link. So seven fit inside this group's 14-error budget; 15 exceeded it. Those limits belong to this symbol. Larger QR codes can have several independently repaired groups, and their budgets aren't one shared pool. But the tiny corner mark changed zero data codewords. It sounds like it should be the easiest repair of all.

6:14 It never got that far. OpenCV reported no detection and ZXing found no candidate. Those three nested-square corner targets help readers locate and orient the grid. The reader needs usable data before the correction algorithm can start. Ah, so the two marks attacked different stages. Central damage left values the decoder could repair. The corner mark stopped these readers from obtaining those values at all. Yes. And beyond the correction budget, failure isn't guaranteed to be clean either. A decoder can reject the data or return a wrong valid answer.

6:48 More damage doesn't come with a reliable warning light. Now imagine printing that checkout sign. My instinct is to select H, the high-correction setting, for the biggest repair margin. What does that cost? More checks consume space. For the same link, a stronger setting may require more squares. Fit that larger grid onto the same small label and each square gets smaller, potentially making the camera's job harder. Then I'd shorten the link or give the code more physical room. If a sticker covers a corner target, moving it addresses the locating problem directly.

7:21 Extra checks can't fix unreadable input. And test the actual-size print from the customer's position, with a clear blank margin around the grid. Our digital experiment doesn't certify a phone camera or a printed sign. The math can recover a message; the camera still has to read it. That four becoming six looked like ordinary data. The extra constraints let us find it, reject the wrong candidate, and put the intended value back. Thanks for listening to Learning Podcasts.