How are Error Correcetion Levels Encrypted in QR Code?

407 Views Asked by At

I'm trying to create my own QR Code Generator library and algorithms. I made a little research about the steps of creating a QR Code, but got stuck with this ambiguous point.

Error Correction Level are listed as following:

  • Level L (Low) 7% of data bytes can be restored.
  • Level M (Medium) 15% of data bytes can be restored.
  • Level Q (Quartile) 25% of data bytes can be restored.
  • Level H (High) 30% of data bytes can be restored.

In QR Codes Black = 1 & White = 0

According to Wikipedia this how levels are coded

  • L : 3 (11 binary) -> (2 Black squares)
  • M : 2 (10 binary) -> (1 Black & 1 White)
  • Q : 1 (01 binary) -> (1 White & 1 Black)
  • H : 0 (00 binary) -> (2 White squares)

According to lots of other Websites & Tutorials this how levels are coded

  • L : 1 (01 binary) -> (1 White & 1 Black)
  • M : 0 (00 binary) -> (2 White squares)
  • Q : 3 (11 binary) -> (2 Black squares)
  • H : 2 (10 binary) -> (1 Black & 1 White)

Which one do I trust or I have missed a critical step or something to identify that

1

There are 1 best solutions below

0
K-Galalem On

The Answer is BOTH, yes both are correct but are a different phases:

  • The one in Wikipedia show how the Error Correction Level is displayed on a READY QR CODE (final preview)

  • The others show how the Error Correction Level should look like Before MASK

In QR Codes not only Data and error correction are masked, also format strings (15 bits) are masked too but with a particular mask:

The 15 bits format string are XOR-ed with the following 15 bits 101010000010010

So as Error Correction Level is always represented in the first 2 bits it will always be masked (XOR-ed) with these 2 bits 10

So the final result would look like the following:

  • L : 1 (01 binary) -> 01^10 = 11 -> Becomes 3 (11 binary)
  • M : 0 (00 binary) -> 00^10 = 10 -> Becomes 2 (10 binary)
  • Q : 3 (11 binary) -> 11^10 = 01 -> Becomes 1 (01 binary)
  • H : 2 (10 binary) -> 10^10 = 00 -> Becomes 0 (00 binary)