QR Code 인코딩 알고리즘: 단계별 구현

<\/script>\n
'; }, get iframeSnippet() { const domain = 'qrcodefyi.com'; const type = 'guide'; const slug = 'encoding-algorithm'; return ''; }, get activeSnippet() { return this.method === 'script' ? this.scriptSnippet : this.iframeSnippet; }, copySnippet() { navigator.clipboard.writeText(this.activeSnippet).then(() => { this.copied = true; setTimeout(() => { this.copied = false; }, 2000); }); } }" @keydown.escape.window="open = false" @click.outside="open = false">

Embed This Widget

Theme


      
    

Widget powered by . Free, no account required.

Implement a QR code encoder from scratch: data analysis, mode selection, error correction calculation, masking, and module placement.

QR Code Encoding Algorithm: Step-by-Step Implementation

This guide walks through the complete QR code encoding process, from raw data to the final module matrix — everything a library does internally.

Step 1: Data Analysis

Analyse the input to determine the most efficient encoding mode:

  • All digits → Numeric mode (3.33 bits/char)
  • Uppercase + digits + limited symbols → Alphanumeric (5.5 bits/char)
  • General text → Byte mode (8 bits/char)
  • Japanese text → Shift JIS." data-category="Encoding & Data">Kanji mode (13 bits/char)

For mixed content, evaluate segment boundaries for optimal mode switching.

Step 2: Version Selection

Calculate the minimum version that can hold the data at the chosen EC level:

  1. Calculate data bits needed (mode indicators + character counts + data + terminator)
  2. Look up the version, EC level, and mode." data-category="Error Correction">data capacity table for each version at the target EC level
  3. Select the smallest version where capacity >= required bits

Step 3: Data Encoding

Encode the data according to the selected mode:

  • Numeric: Groups of 3 digits → 10 bits, remainder of 2 → 7 bits, remainder of 1 → 4 bits
  • Alphanumeric: Pairs → 11 bits, odd character → 6 bits
  • Byte: Each byte → 8 bits directly

Prepend the encoding mode." data-category="Encoding & Data">mode indicator (4 bits) and character count indicator (variable length by version).

Step 4: Error Correction

  1. Pad the data stream to fill the version's data capacity
  2. Split into blocks per the version/EC specification
  3. Calculate Reed-Solomon EC codewords for each block
  4. Interleave data and EC codewords across blocks

Step 5: Module Placement

Place modules in the matrix:

  1. Draw finder patterns (3 corners), separators, and timing patterns
  2. Place alignment patterns (version 2+)
  3. Reserve mask pattern." data-category="QR Code Structure">format information areas
  4. Place data bits in the two-column zig-zag pattern (right to left, alternating up and down)

Step 6: Masking

Apply all eight mask patterns, calculate penalty scores for each, and select the mask with the lowest penalty.

Step 7: Format and Version Information

Encode the selected EC level and mask pattern into the format information (BCH). For version 7+, encode version information (Golay). Place in the reserved areas.

Key Takeaways

  • The encoding process has 7 main steps from data analysis to final matrix
  • Mode selection significantly impacts data efficiency and version size
  • Reed-Solomon EC codewords are calculated per block and then interleaved
  • Module placement follows a specific two-column zig-zag pattern
  • All 8 masks are evaluated; the lowest penalty score wins