QR Code Scanning (3D desk)

A phone camera reads a printed QR code: distance, tilt, focus, light and damage decide whether the Reed-Solomon decoder can still recover the text.

Switch to 2D

Presets

Playback

Scanner pose

Printed card

Camera and light

Camera

Layers

Quality

Model check

The science

What a phone does when it reads a QR code. The camera forms an image through a (nearly) pinhole lens; the app locates the symbol, samples one brightness value per module, decides black or white, and then lets an error-correcting code repair whatever the camera, the print and the dirt damaged. This page runs the last three stages for real on frames rendered from the 3D desk scene.

Equations used by the page

  • Pinhole projection: a module of width s at distance d covers p = f_px s / d pixels, with f_px = (H/2) / tan(FOV/2) (here FOV 80 deg, 320 px tall). Reading needs roughly p >= 2 pixels per module (Nyquist); foreshortening multiplies the short side by cos(angle) between the card normal and the viewing ray.
  • Thin-lens defocus: blur circle on the sensor c = A f |d - d_f| / (d (d_f - f)), f = 5.6 mm, A = f / 1.8; it is applied as a box blur of that width in pixels. Autofocus sets d_f = d but not closer than 7 cm.
  • Sensor: auto-exposure scales the 98th percentile to 0.9; shot noise has relative size sigma = sigma0 / sqrt(L) where L is the raw white level, so a dim room is noisier. Black/white decision: Otsu threshold, argmax w_B w_F (mu_B - mu_F)^2, over the module samples.
  • Reed-Solomon over GF(256), primitive polynomial x^8+x^4+x^3+x^2+1 (0x11D): codeword c(x) = d(x) x^n + (d(x) x^n mod g(x)), g(x) = prod_{i=0}^{n-1} (x - a^i). Decoding: syndromes S_j = r(a^j), Berlekamp-Massey for the error-locator, Chien search for the positions, magnitudes from a linear system, then all syndromes are re-checked. A code with n check codewords corrects up to t = floor(n/2) wrong codewords.
  • Level L/M/Q/H check codewords: version 1 has 7/10/13/17, version 2 has 10/16/22/28. If a fraction p of codewords is corrupted at random the decode probability is P(Binomial(N, p) <= t) (graph 1).

Honest simplifications

Real scanners find the three finder patterns blindly and estimate the version and the perspective from them; here the page projects the known module centres through the exact camera (the pose is known), so the finder search is skipped. Only versions 1 and 2, byte mode and a single Reed-Solomon block are implemented. Damage is painted as random blobs, which is not how scratches or creases look; the camera has no lens distortion, rolling shutter or demosaicing. Graph 1 uses the idealised random-codeword model, while real damage is spatially clustered, so the dots (an actual decoder run) and the curve agree only for random errors.

Checks run in the page

(1) The generator polynomial of the 7-check code and the 15-bit format words against the ISO 18004 values. (2) Encode, build the matrix, read it back and decode 96 random strings across all four levels; then exactly t and t+1 random codeword errors. (3) Pixels per module from projected module centres against the closed form f_px s / d.

Loading model...
Loading Blender model...
Decode probability versus corrupted codewords (curves: theory; dots: simulated decoder)
Pixels per module versus distance (pinhole), for four card sizes
Brightness histogram of the module samples and the Otsu threshold
Made with 🟠 Blender · Three.js