What’s inside The QR code?

Albert Assaad
Analytics Vidhya
Published in
6 min readDec 2, 2019

--

In our last article, we discussed how the EAN13 barcode works and how the information is stored in it, now we will discuss the basics of a new type of scannable image called QR code(matrix barcode or two-dimensional barcode)

QR Code abbreviation is (quick response code), introduced in the automotive industry in Japan in 1994, it gained its reputation outside the industry due to its fast readability and its storage capacity compared to other barcodes.

1- Types of Qr Code:

There are many types of Qr codes we will mention some :

a- QR Code Model 1 and 2: Model 1 is the original Qr code it can store as long as 1,167 numerals. Model 2 is the upgrade for model 1 it’s capable to store 7089 numerals, Today the term QR code refers to Qr code Model 2.

We have so many Models of Qr Codes each model contains number of modules(a module is the basic unit of QR Code, it’s represented by a black or white block, it stores the data of the Qr Code and form its rows and columns which called data matrix) find below example of some Versions:

QrCode Versions

How to determine the version of the QRCode Model 2 is by counting the number of modules by one column and doing this simple math: if we have 25 modules we subtract from 17 and divide by 4 (25–17)/4 = 2

b- Micro Qr Code: This is a small version of Qr code it can preserve up to 35 numerals, in contrary to the QR code version 2, it must be scanned in one orientation.

Micro Qr Code contains text “Medium”

c- IQR Code: This type can be generated as a Square module or rectangle, the maximum version can store up to 40000 numerals, this type can be printed as a dot pattern barcode (for example on pipes).

IQR Code Rectangle
IQR Code square

d- Frame Qr: This type allows to insert of a canvas area to the Qr it can be used for promotion.

When scanned it will give the text “Medium”

e- SQRC code: Or Secure Qr Code, this type can be used to store private data only the scanners that have the decryption keys can read it, its appearance is the same as the normal QR Code version 2.

2- How the QR Code is been decoded:

For simplicity, we will take Qr Code Version 2 as an example and we will not talk about the error correction in this post (we might include it in the future)

I — Some Parts of the Qr Code:

a- The Quiet Zone: it is is the region that surrounds the symbol on 4 sides in our example the quiet zone is 4X(4 modules)

b-Finder Patterns: Are the three similar structures placed on every corner (except the bottom right corner), each one consists a 3x3 matrix of black modules surrounded by white modules that are again surrounded by black modules, the Finder Patterns allows the decoder software to recognize the QR Code and determine the correct orientation.

c-Alignment Pattern: They exist in QR Code Version 2 and greater, they are positioned in the lower right-hand corner. This pattern allows the QR reader to correct for distortion when the code is bent or curved.

d- Timing Pattern: Alternating black and white modules in the Timing Pattern enable the decoder software to determine the width of a single module.

e- Format Info: stores information about the error correction level of the QR Code and the chosen masking pattern.

II — How to decode a QR Code Manually:

We will take the following QR code for example as I mentioned before I will not include all the feature in the QR code since our purpose is to know its concept:

The first step is by taking the data in the format pattern from the upper left or bottom left one and XOR it with 10101:

As we see the data in the upper and bottom are identical it contains Black, Black, Black, White, White translated to 11100, take the calculator and XOR is with 10101(you can use the programmer mode in the windows calculator)we got 01001 as an answer the first 2 bits are for the error correction level(we will ignore it for this post) the following 3 bits 001 defines how the QR code is masked, see below masks’ table:

i represents the column number j represents the line number

So as per the above table MASK 001 has 1 even and 1 odd line the even number the Module content must be inverted if it’s 1 will be turned to 0 and vise versa , we start counting from the bottom the first line number is 1(1 Modulo 2 = 1) so line data must be inverted and so on…

In red the lines that must be inverted results of Mask 001

The second step is to start from the bottom right of the code and you read it the following way :

The first 4 modules contain the encoding mode of the Qr code, the encoding determines if it’s Numeric, Alphanumeric, Byte, Kanji(Japenese) and it will define the chunk of modules that we need to read at a time.

0001 Numeric 10Modules(Bits)

0010 Alphanumeric 11Modules(Bits)

0100 Byte 8Modules(Bits)

1000 Kanji 13Modules(Bits)

So let’s begin to read it :

First, 4 Bits from 1 to 4 contains 1000 since modules 1 and 2 are in Line no 1(1 Modulo 2 =1) so we must invert them so the result is 0100 so we have a Byte encoding so we need to read the next 8 Modules in order to define the length of the content.

We read from Module number 5 to number 12 result = 11001010 now we have to invert the modules in line number 3 and number 5 we got 00000110 in Binary as a result which is 6 in Decimal so the content length is 6 Byte.

We know now that we have a word from 6 letters we start from module number 13 till 20 (10000001) we invert line 7 and 9 so we got 01001101 Binary which is 77 in Decimal now we have to match the number in the ASCII table, 77 = M (see below ASCII Table)

We continue with the next 8 modules from number 21 till number 28 10101001 we invert line number 11 and 13 we get 01100101 in Binary which is 101 in Decimal refer to the ASCII table 101 = e

Now from module number 29 till 36 10100111, we invert line number 15 we get 01100100 in Binary equal to 100 refer to the ASCII table 100 = d

Next from module number 37 till module number 44 01011010 we invert line 11 and 13 we get 01101001 Binary= 105 Decimal = i

Next from module number 45 till 52: 01000110 we invert lines 7 and 9 we get 01110101 Binary = 117 Decimal = u

Now from module 53 till 60: 01011110 we invert lines 3 and 5 we get 01101101 Binary=109 Decimal = m

So our final word is Medium.

The next 4 blocks from 61 till 64 contain 0011 we invert line 1 so we got 0000 which means End of message.

That’s all for this article, note that for the purpose of brevity I didn’t mention other parts of the QR Code as like the Error correction part, for any further clarification please mention it in the comments.

--

--

Albert Assaad
Analytics Vidhya

Multi-platform developer (Android,IOS,Dynamics Nav,C#.net,Vb.net,Python) interested in everything technology.