You ask
How can a QR code still work when part of it is destroyed?
- YouHow can a damaged code still open the link?
A QR code holds more than your link. It also holds spare information, so a scanner can rebuild what is missing.
Alongside the link, the code stores extra repair data. Damage to part of the pattern can then be undone, as long as enough of the code is left.
QR codes append error-correction codewords to the data. There are four levels (L, M, Q and H); at the highest, H, about 30% of the codewords can be restored.
- HardwareHow does a phone read the squares?
The camera takes a picture, and the phone looks for the three big squares in the corners.
Those corner squares tell the phone where the code is, how big it is and which way up it is. Then it reads the grid one small square at a time.
Three finder patterns give position and orientation. Timing and alignment patterns help the decoder sample the grid of modules accurately despite tilt and distortion.
- DataHow do squares become a message?
Each small square is dark or light, a 1 or a 0. Groups of them spell out the link.
Eight squares make a byte, and bytes stand for letters and numbers. The link is stored first, and the repair bytes follow.
The data is encoded into 8-bit codewords, split into blocks, and each block gets Reed–Solomon check codewords. The blocks are interleaved, so a scratch damages many blocks a little instead of one block badly.
- SoftwareHow can a scanner rebuild missing bytes?
It checks the bytes against the repair bytes, finds the ones that are wrong, and works out what they should be.
The repair bytes are made from the data by a maths recipe. If the bytes no longer fit the recipe, the scanner can work backwards to find which are wrong and fix them.
A Reed–Solomon decoder computes syndromes from the received codeword, locates the errors and corrects them. With 2t check codewords it can repair up to t unknown errors, or up to 2t erasures at known positions.
- The principleHow can extra data repair lost data?
That is redundancy: a little extra, arranged cleverly, lets you recover what was lost.
The repair bytes are not copies. Each one is calculated from all the data in its block, so the pattern of the whole survives even when some of it does not.
Reed–Solomon codes work over the finite field GF(256). Valid codewords are polynomials that share a fixed set of roots, so a damaged word can be mapped back to the nearest valid one.