Level 1 QR Encode

The Key Stages related to Creating a QR Code

This is a step by step overview of the processes involved in creating a QR code containing a given series of characters.


  • An encoding mode is chosen to determine how the characters will be encoded.
  • Binary data can contain 7-bit ASCII characters or unicode characters that are encoded as multibyte characters using the UTF-8 encoding.
  • Numeric data takes the least space, followed by alphanumeric and then binary
  • If the data can be encoded in more that one way, then the one that takes the least space is chosen
  • An error level determines the number of bytes that can be corrupted, detected and correntec.
  • The four error levels are high, quartile, medium and low.
  • The high error level requires the most space followed by quartile, medium and then low.
  • The error level is determined accoring to whether it is more important to be able to correct data or to keep the size of the QR code low.
  • With the input data, the encoding and the error level, the number of data bytes can be calculated..
  • The number of data bytes determines the minimum QR code version that can be used..
  • The QR Code grid can then be set up with the number of module based on the verison
  • The static elements of the QR code are the positioning patterns, the timing patterns and the spacing. These can be added to the grid.
  • There are eight different mask patterns.
  • Any of these mask patterns can be applied to the data to create a valid QR code.
  • Certain mask patterns create a better result with fewer blocks of white and black data.
  • The result when each pattern is applied is rated, and the mask with the lowest ranking is chosen and applied to the data.
  • The format information consists of fifteen bits.
  • Two bits are used for the error level and three for the mask pattern. The rest are error correction bits.
  • A mask is applied to the bits and the resulting bits are added to the QR code.
  • The information bits are placed near to the two left positioning pattern
  • A copy of the fifteen bits is also added to the QR code.

Terminology

QR Code

A QR code (Quick Response Code) contains data that can be scanned in using a device which is able to capture and decode the information stored within the data.

Alphanumeric Encoding

Data made up entirely of the the numeric characters, upper case letter from 1-Z, space and the characters: $ % * + - , /

Binary or Byte Encoding

Data made up of byte size blocks. An ASCII-7 character can be represented by one byte and all other characters can be expressed using UTF-8 encoding in two or more bytes.

Bit

A binary value equal to 0 or 1 that is represented by a single black or white module in the QR code.

Character Count

The number of characters encoded in the QR Code with the exception of multibyte characters that counts each byte as a character.

Data Block

Variable bit blocks of data containing information relating to one of these:

  • Encoding mode
  • Character count
  • Encoded data
  • Padding
  • Error Codes

Data Byte

8-bit blocks of data that store the encoded data and the error codes.

Error Level

The error level determines the extent to which the QR code can be corrupted or not correctly scanned and the data can still be corrected.

Encoding Mode

Determines how the data is encoded. The standard encoding modes are numeric, alphanumeric, binary and kanji. The value is stored in a 4-bit block.

Error Bits

Additional bits that are added to the data to enable error correction.

Galoid Field Arithmetic

Calculations performed on a finite number of elements. QR codes use GF arithmetic over 2 and 256 elements. Calculations use the XOR operation and polynomial division.

Indicator Blocks

Two blocks of data used to store the encoding mode and character count.

Information Bits

Fifteen bits of encoded data containing the error level value and the mask pattern number.

Mask

A pattern applied to the QR code to optimise on the distribution of the modules.

Modules

Modules are the smallest component of a QR code.

  • Modules are typically square and either black or white.
  • A black module represents the binary value 0.
  • A white module represents the binary value 1.

Numeric Encoding

Data made up entirely of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9.

Positioning Patterns

Patterns that help to establish the orientation when scanning the QR code.

Quiet Zone

Blank space that is added around the QR code to enable correct scanning.

Reed-Solomon Error Codes

Additional codes that are added to the data to enable error detection and correction of multiple errors.

Timing Pattens

Alternating black and white modules determine the QR code version. The pattern contains (4n + 1) modules for a version n QR code.


Components of a QR Code

A QR code consists of a number of static elements that help determine the orientation and size of a QR code when it is being scanned.

The rest of the QR code is used to store the data relating to the information that is encoded within it.

  • This QR code structure is for a version 1 QR code.
  • The information in the QR code relates to Hello World with Binary encoding, Low error level and mask pattern 0.

18 18 17 17
18 18 17 17
18 18 17 17
18 18 17 17
19 19 16 16
19 19 16 16
19 19 16 16
19 19 16 16
26 26 25 25 24 24 23 23 20 20 15 15 10 10 9 9 4 4 3 3
26 26 25 25 24 24 23 23 20 20 15 15 10 10 9 9 4 4 3 3
26 26 25 25 24 24 23 23 20 20 15 15 10 10 9 9 4 4 3 3
26 26 25 25 24 24 23 23 20 20 15 15 10 10 9 9 4 4 3 3
21 21 14 14 11 11 8 8 5 5 2 2
21 21 14 14 11 11 8 8 5 5 2 2
21 21 14 14 11 11 8 8 5 5 2 2
21 21 14 14 11 11 8 8 5 5 2 2
22 22 13 13 12 12 7 7 6 6 1 1
22 22 13 13 12 12 7 7 6 6 1 1
22 22 13 13 12 12 7 7 6 6 1 1
22 22 13 13 12 12 7 7 6 6 1 1




Positioning Patterns

Three patterns to help establish the orientation when scanning the QR code.



Information

Encoded data containing the error level value and the mask pattern number.



Timing Patterns

The five modules indicate that this is a version 1 QR code.

15 15 10 10
15 15 10 10


Data Bytes

The encoded data and the error correction codes. The numbers show the order that the bytes of data are positioned in the QR code.




Space

Space added between the positioning patterns and the rest of the QR code.




Quiet Zone

Blank space that is added around the QR code to enable correct scanning.




Fixed Module

A module that is not used and has been defined to always be black.



Data Position in the QR Code

A version 1 QR code allows for 208 bits of data. This data is entered into the data area of the QR code

  • The first bit is placed in the bottom right of the QR code.
  • The last bit is placed on the far right just above the bottom left positioning pattern.
  • The data goes up and down in a two module width columns, as shown below.
  • If the data sequence reaches a point where it cannot continue, it jumps to the bottom right of the unused data space.

Table showing how data is positioned within the QR code


Start

=>

End


Data bits

The encoded data is then made up of:

  1. The mode indicator for the encoding (4 bits)
  2. The character count indicator block (The number of bits used depends on the encoding and the QR version)
  3. The encoded data blocks
  4. Padding data to complete any unused bytes.
  5. Error Codes

QR Code Encoding

Examples and encoding tool

Encoding describes the method by which the characters are converted into bits of data that can be store in the QR code

  • There are four standard encoding modes used by QR codes: numeric, alphanumeric, binary and kanji.
  • All characters can be encoded using binary encoding although this may not be the most efficient selection.
  • The selected encoding is stored as a 4-bit mode indicator at the beginning of the data.

Table of encoding types for a QR code

Encoding Description 4-bit Mode Indicator
Numeric Numeric characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0001
Alphanumeric Uppercase letters, numeric characters, space or one of the characters $ % * + - , / 0010
Binary Any character. 0100
Kanji Kanji characters 1000


Numeric Encoding

Numeric encoding can be used if the data is made up entirely of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9

To encode the numeric characters:

  • The digits are placed into groups of three from left to right to create values between 0 and 999.
  • The 3-digit values are coverted to 10-bit binary numbers.
  • If there is one remaining digit, it is multiplied by 16 and converted to an 8-bit binary number
  • If there are two remaining digits, 2-digit value is multiplied by 2. The result is converted to an 8-bit binary number

Alphanumeric Encoding

Alphanumeric encoding can be used if the data is made up entirely of the following:

  • Numeric digits
  • Upper case letters
  • Space
  • The characters $ % * + - , / .
Alphanumeric data values
ASCII value 0 1 2 3 4 5 6 7 8
Character 0 1 2 3 4 5 6 7 8
ASCII value 9 A B C D E F G H
Character 9 10 11 12 13 14 15 16 17
ASCII value I J K L M N O P Q
Character 18 19 20 21 22 23 24 25 26
ASCII value R S T U V W X Y Z
Character 27 28 29 30 31 32 33 34 35
ASCII value $ % * + - . / :
Character 36 37 38 39 40 41 42 43 44

To encode the alphanumeric characters:

  • Look up the value of each alphanumeric character.
  • The values are taken in pairs.
  • The first value of each pair is multiplied by 45 and added to the second value to form a value between 1 and 2024.
  • This value is then converted to an 11-bit binary number.
  • If there is one remaining character, its value is multiplied by 4 and converted to an 8-bit binary number.

Binary Encoding

Binary encoding can be used for any character that has a unicode value and is stored using one of more bytes using UTF-8 encoding.

For values between 0 and 127, one byte of data is used and the character is encoded using it's ASCII value.

7-bit ASCII Values (32-127)
ASCII value 33 34 35 36 37 38 39 40 41 42 43 44
Character