QR Codes : A-Z on QR Codes and how the work
By: Sid
Introduction
firstly, holy shit, it hit me now that we're in a new year after i put in the date for this blog
HAPPY NEW YEARRRR GANG!!!! 🥳🥳🥳🥳🥳
okay, lets begin xD
so we all use qr's everyday.
i was in a cafe today,
asked for the password, they gave me a qr code
asked for the menu, it was a qr
was paying, it was a qr.
being the engineer i am, i was lowk curious as to how these things were engineered
and, if you think about it, theyre actually super resilient too, a couple of scratches and they still work????
half rotated and they still work ??
saw some pretty cool stuff as i was reading about them, wrote a blog on it :)
How QR codes store information
at its core, a QR code is just a set of tiny lil squares together.
each square is essentially a bit of information.
- the black lil squares respresent a 1
- the white lil squares respresent a 0
if you're from a CS background, you know where this is heading xD
they're basically using bits
so what can they store? -> essentially anything that can be written as numbers.
they convert text/info into these numbers i.e. represent them as 0/1's
but, instead of random bits, it follows a very smart design so phones can read it even if it’s dirty, tilted, or scratched.
the smart design of a QR code
if you look closely, QR codes always have three big squares in the corners.
those are called Finder Patterns.
they help your phone quickly detect:
- where the QR code is,
- how it’s rotated,
- how big it is.
other parts include:
- Alignment patterns – fix tilted images
- Timing patterns – help measure spacing
- Data area – where your message lives
- Error correction area – backup data
this design makes QR codes extremely reliable.
how text becomes a QR code
this is how we essentially convert any text to a 0/1 representation :
let’s say we want to store:
HELLO
here’s what happens step-by-step.
Step 1 — Convert Text → Numbers
computers store letters as numbers.
For example:
H = 72
E = 69
this is called ASCII encoding.
Step 2 — Numbers → Binary
computers only understand 0 and 1.
so 72 becomes:
01001000
Now “HELLO” becomes a long string of bits.
Step 3 — Add Error Correction — the actual cool part
this is the magic behind why QR codes still work even if scratched.
QR codes use an algorithm called Reed–Solomon error correction.
it adds extra mathematical data (essentially convert the ASCII numbers to become coefficients of a polynomial) so missing pieces can be rebuilt (think of it like sending a message with backup copies).
here's the example from before :
step 1 : convert data to numbers
HELLO → numbers → [72, 69, 76, 76, 79]
step 2 : now, from these numbers, build a polynomial
f(x)=72+69x+76x^2+...
step 3 : evaluate polynomial at extra points
instead of sending only the original numbers, we evaluate the polynomial at extra x-values.
These extra values are parity symbols.
even if some points are missing or wrong, the receiver can reconstruct the original polynomial.
from that, it recovers the original message.
and this is how even if coffee spills on the QR code, your phone can recover the original message.
this same math is used in:
- CDs and DVDs
- Deep-space communication
- Satellite signals
(lmao, satellite signals, deep-space commn. and qrs be using the same algo)
but yes, it’s incredibly powerful.
Step 4 — Arrange Bits Into the Grid
now those bits are placed into the QR pattern.
butttttt, when data is placed into the QR grid, it may create bad visual patterns.
- too many black squares in a row
- large solid blocks
- patterns that accidentally look like finder squares
so, QR codes intentionally scramble the pattern a little. not as encryption but rather to make it more readable for the scanning device and this is done using the masking algorithm.
Step 5 — Masking Algorithm
each mask is a simple math rule.
after the QR has placed all data bits, it tries 8 mask patterns.
for every square at position (row, col):
- If rule is true → flip color
- If false → keep color
there are essentially 8 mask rules that are listed below:
| Mask | Rule |
|---|---|
| 0 | (row + col) % 2 == 0 |
| 1 | row % 2 == 0 |
| 2 | col % 3 == 0 |
| 3 | (row + col) % 3 == 0 |
| 4 | (row//2 + col//3) % 2 == 0 |
| 5 | (rowcol) % 2 + (rowcol) % 3 == 0 |
| 6 | ((rowcol)%2 + (rowcol)%3) % 2 == 0 |
| 7 | ((row+col)%2 + (row*col)%3) % 2 == 0 |
all the 8 masks are applied the qr off these 8 masks are individually graded and the best one is the one that is generated.
the QR stores which mask was used so the phone can undo it later.
how our phones read a QR code
when you scan a QR code, your phone runs a mini computer-vision pipeline. (so cool how so much happens behind the scenes by just pointing your camera at the qr)
1. detect "Finder Squares"
using edge detection and pattern recognition.
2. straighten the image
QR might be tilted, so the phone uses perspective math to reshape it into a perfect square.
3. read each tiny square
camera checks brightness → converts to 0 or 1.
4. removes the mask
using stored mask info.
5. fix errors
Reed–Solomon reconstructs missing bits.
6. decode binary → text
now your link or message appears.
all this happens in a fraction of a second!!!!!! 0o0
how much can a QR code store?
QR codes have 40 sizes called “versions.”
Small QR: about 25 characters
Large QR: thousands of digits
Final Thoughts
after i read up on all of this i realised that a QR code looks simple, but is basically:
- a Binary encoding
- a Computer vision pipeline
- an optimized Error-correcting code
it’s lowk a beautiful mix of mathematics, engineering, and design — hiding inside that one square we see everyday.
next time you scan one, remember:
You’re using deep-space communication math...to buy samosas xDDDD
see y'all :D