intermediatecat/misc~2 min read

Damaged QR Code & Barcode Reconstruction

Repair corrupted, inverted, cropped, or partially obscured QR codes using Reed-Solomon error correction and image restoration.

// prerequisite reading

Anatomy of a QR Code

QR (Quick Response) codes contain precise structural alignment patterns required by scanners:

+---------------+---------------+---------------+
| Finder Pattern|               | Finder Pattern|
|  [■■■]        | Timing Pattern|  [■■■]        |
|  [■ □ ■]      | ■ □ ■ □ ■ □ ■ |  [■ □ ■]      |
|  [■■■]        |               |  [■■■]        |
+---------------+---------------+---------------+
|               |  Format Info  |               |
+---------------+---------------+---------------+
| Finder Pattern|               | Alignment     |
|  [■■■]        |               | Pattern       |
|  [■ □ ■]      |               |  [■]          |
|  [■■■]        |               |               |
+---------------+---------------+---------------+
  1. Finder Patterns (Three Big Corner Squares): Located at top-left, top-right, and bottom-left corners. Essential for orientation and scaling.
  2. Timing Patterns: Alternating black/white modules connecting the finder patterns horizontally and vertically.
  3. Format Information: Stores Error Correction Level (L, M, Q, H) and Mask Pattern.

Common QR Code Challenges & Repairs

1. Inverted Colors

Most mobile QR scanners expect black modules on a white background. If an image has white modules on a black background, invert colors in GIMP/Photoshop or CyberChef (Invert Image operation).

2. Missing Finder Pattern

If one corner finder pattern is cropped or smudged out:

  • Copy a clean finder pattern square from another corner and paste it onto the missing position.

3. Reed-Solomon Error Correction Capacity

QR codes use Reed-Solomon error correction to restore data if up to 30% (Level H) of the symbol is damaged or covered!

If the finder patterns and format info are intact, you can obscure missing modules with solid black or white, and standard readers will still decode the payload correctly.


Decoding via CLI Tools

# Decode QR code image from command line
zbarimg qr_code.png

# Alternative python library (pyzbar)
python3 -c "from pyzbar.pyzbar import decode; from PIL import Image; print(decode(Image.open('qr_code.png')))"