[Ruby] [Ruby] Generate concatenated QR code with rqrcode (Knowledge)

4 minute read

What is a consolidated QR code?

  • QR code has a “concatenation function” that divides long data into up to 16 QR codes and combines them when reading. (For details, refer to QR Code.com)
  • In Japan, there are many occasions when you see a prescription from a hospital/clinic.
  • Output functions are supported by third-party form output libraries, but it is difficult to find it in the OSS library, so I used it from the past rqrcode so that it can be output.
    • This time, the target is to output the concatenated QR code recorded in the above “prescription”, so only the QR code in the 8bit_byte mode is targeted.

How can I get a consolidated QR code?

  • In the case of normal QR code, the header part of the data to be written has the following structure.

    • There are versions of QR code (1 to 40) (=QR code size), which changes the data size that can be recorded in one QR code. (See here)
    • It seems that the version specification for the QR code is written in a place different from the header part, so it is omitted here.
    • SS length is variable between 8 and 16 bits depending on the QR code version and encoding mode.
- Length What to record
E 4bit Encoding mode (see table below)
SS (Variable) Record the length of data to be recorded in QR code
Encoding mode Value that represents the mode
numeric 1 (0b0001)
alpha_numeric 2 (0b0010)
8bit_byte 4 (0b0100)
  • In case of concatenated QR code in 8bit_byte mode, record the header part as follows.
    • I referred to here.
- Length What to record
3 4bit Mode that indicates concatenated QR code (0b0011)
N 4bit Sequence number of QR code to connect (serial number from 0 to 15)
T 4bit Last sequence number of QR code (serial number from 0 to 15)
PP 8bit Parity value of all data (XOR value of all bytes)
E 4bit Encoding mode (8bit_byte = 4)
SS (Variable) Records the length of data recorded in one QR code

Therefore, the procedure to issue a combined QR code is as follows.

  • Determine the version (size) of the concatenated QR code. -Up to 16 concatenations. -In addition, with the connected QR code, the amount of data that can be recorded seems to be 2 bytes less than the normal QR code. (I think the amount of data has increased by 20 bits, but for some reason -2 bytes can be recorded) -It is necessary to decide the version by taking the above two points into consideration. -Normally, all QR codes are made with the same version (size). (I have never tried to connect QR codes with different versions, but it is safe to avoid it because some readers are unreadable.)
  • Calculate the parity from the data stored in the QR code. (XOR value of all bytes = 8bit)
  • Divide the data to be contained in the QR code into one QR code. (Capacity of each QR code-Try to fit within 2 bytes)
  • Generate the required number of QR codes and render each. -Specify the same “parity value” and “last sequence number” for all QR codes. -Specify the “sequence number” and the data to be stored for each QR code.

  • The output of the concatenated QR code by actually modifying rqrcode is explained in Practice.

Because the behavior differs for each reader, be careful.

  • In business such as pharmacies, laser barcode scanners are used. Since these are quite expensive, it is a little difficult to arrange equipment for operation verification. (I was also interested in it personally, so I used Yahoo Auction to get a used item at a low price. It seems that it is exhibited frequently unexpectedly.)
  • So, until the final confirmation using the barcode scanner, I used a smartphone app that supports concatenated QR code scanning to check the operation. (For iOS apps, IconitPomeraQRCodeReader・(QRdeCODE](https://apps.apple.com/jp/app/qrdecode/id365849329)only3typeswerefound)
  • However, the behavior of both the reader app and the barcode scanner may vary depending on the product. -It seems that some have not confirmed whether the parity values match. (This is not a big problem in actual operation) -If there is only one QR code (not divided) even though it is a concatenated QR code, it seems that there are some that cannot be read. (If the amount of data is small, it would be better to output with a normal QR code) (Currently, we are reconfirming details) -There is something that causes garbled characters when the upper byte and lower byte of Kanji (Shift JIS) straddle two QR codes. (This behavior is often seen in apps)

  • From the above, especially for business use, it is safer to assume an actually used device and prepare an operation check environment. -For bar code scanners, it may be better to prepare several types, including older models.


This article was written with reference to the following information.