5

Very few QR encoders/decoders have (explicit) support for so-called GS1 encoding. Zint is one of the exceptions (under QR select GS-1 Data Mode), but its license prevents me from using it. Commercial offers, mainly from Tec-It, are costly, especially because I'm not interested in all other kinds of barcodes they support.

Is there a way to add GS1 support to any QR encoder/decoder without changing its source? For example, could I apply some algorithm to transform textual GTIN AI data into compatible binary? I think it should be possible, because after all, it's still QR. Please note that I am not a data coding expert - I'm just looking for a way to deal with this standard without paying a small fortune. So far, I found postscriptbarcode which does have support for it, and seems to use its own QR engine, but output quality is so-so and my PostScript skills are far too limited to figure out the algorithm.

Terry Burton
  • 2,801
  • 1
  • 29
  • 41
Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48
  • GS1 means a lot of things. Which format, and what contents, are you interested in? zxing supports a lot of what has been rebadged "GS1 barcodes" already. – Sean Owen Nov 29 '12 at 15:57
  • @SeanOwen I don't mean the special GS1 DataBars. I'm talking "GS1 QR." To see what I mean, open up Zint and under QR select GS-1 Data Mode. – Thijs van Dien Nov 29 '12 at 15:58
  • Late in the game, but I would be interest to understand what is meant by postscriptbarcode's "output quality is so-so". The library generates vector graphics that maintain perfect quality when scaled appropriately. – Terry Burton Sep 24 '13 at 11:16

1 Answers1

6

As long as the library supports decoding of the FNC1 special character, it can be used to read GS1 codes. The FNC1 character is not a byte in the data-stream, but more of a formatting symbol.

The specification says that a leading FNC1-character is used to identify GS1 barcodes, and should be decoded as "]d2" (GS1 DataMatrix), "]C1" (GS1-128), "]e0" (GS1 DataBar Omnidirectional) or "]Q3" (GS1 QR Code). Any other FNC1-characters should be decoded as ASCII GS-characters (byte value 29).

Depending on the library, the leading FNC1 might be missing, or decoded as GS (not critical), or the embedded FNC1-characters might be missing (critical). The embedded FNC1-characters are used to delimit variable-length fields.

You can read the full specification here (pdf). The algorithm for decoding the data can be found under heading 7.9 Processing of Data from a GS1 Symbology using GS1 Application Identifiers (page 426).

The algorithm goes something like this:

Peek at the first character.
If it is ']',
    If string does not start with ']C1' or ']e0' or ']d2' or ']Q3',
        Not a GS1 barcode.
        Stop.
    Consume the caracters.
Else if it is <GS>,
    Consume character.
Else,
    No symbology identifier, assume GS1.
While not end of input,
    Read the first two digits.
    If they are in the table of valid codes,
        Look up the length of the AI-code.
        Read the rest of the code.
        Look up the length of the field.
        If it is variable-length,
            Read until the next <FNC1> or <GS>.
        Else,
            Read the rest if the field.
        Peek at the next character.
        If it is <FNC1> or <GS>, consume it.
        Save the read field.
    Else,
        Error: Invalid AI

The binary data in the QR Code is encoded as 4-bit tokens, with embedded data.

0111 -> Start Extended Channel Interpretation (ECI) Mode (special encodings).
0001, 0010, 0100, 1000 -> start numeric, alphanumeric, raw 8-bit, kanji encoded data.
0011 -> structured append (combine two or more QR Codes to one data-stream).
0101 -> FNC1 initial position.
1001 -> FNC1 other positions.
0000 -> End of stream (can be omitted if not enough space).

After an encoding specification comes the data-length, followed by the actual data. The meanings of the data bits depends on the encoding used. In between the data-blocks, you can squeeze FNC1 characters.

The QR Code specification (ISO/IEC 18004) unfortunately costs money (210 Franc). You might find some pirate version online though.

To create GS1 QR Codes, you need to be able to specify the FNC1-characters in the data. The library should either recognize the "]Q3" prefix and GS-characters, or allow you to write FNC1 tokens via some other method.

If you have some way to write the FNC1-characters, you can encode GS1 data as follows:

Write initial FNC1.
For each field,
    Write the AI-code as decimal digits.
    Write field data.
    If the code is a variable-length field,
        If not the last field,
            Write FNC1 to terminate the field.

If possible, you should order the fields such that a variable-length field comes last.


As noted by Terry Burton in the comments; The FNC1 symbol in a GS1 QR Code can be encoded as % in alphanumeric data, and as GS in byte mode. To encode an actual percent symbol, you write it as %%.

To encode (01) 04912345123459 (15) 970331 (30) 128 (10) ABC123, you first combine it into the data string 01049123451234591597033130128%10ABC123 (% indicator is the encoded FNC1 symbol). This string is then written as

0101 - Initial FNC1, GS1 mode indicator
0001 - QR numeric mode
0000011101 - Data length (29)
<data bits for "01049123451234591597033130128">
0010 - QR alphanumeric mode
000001001 - Data length (9)
<data bits for "%10ABC123">

(Example from the ISO 18004:2006 specification)

Terry Burton
  • 2,801
  • 1
  • 29
  • 41
Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
  • So basically GS1 is just alphanumeric QR with a lot of formatting rules, rather than its own fifth encoding? I will take some time to study that document. However, I'm more interested in generating GS1 QR codes, so if you could give an example of how to use a "regular" QR encoder with no specific GS1 mode to output GS1, that would secure an accepted answer. – Thijs van Dien Nov 29 '12 at 20:22
  • @ThijsvanDien were you able to get a regular qr reader working for gs1 datamatrix? – vinnybad Jun 18 '13 at 16:42
  • @vinnybad I was only interested in GS1 QR. – Thijs van Dien Jun 22 '13 at 23:07
  • An additional note regarding whether encoding GS characters for FNC1 characters is harmful, GS1 QR Code is unusual in its implementation of the GS1 Application Identifier Standard Format in that it specified that the usual FNC1 field separators should be encoded as "%" characters and used to terminate both variable-length *and* fixed-length fields. – Terry Burton May 20 '15 at 07:22
  • That is not true. `%` is a valid, and common data character. The separators should always be encoded as FNC1. When decoding, you can replace it with GS, if your library does not have another method of signaling FNC1. – Markus Jarderot May 20 '15 at 07:54
  • 1
    It certainly is true. Please read §6.4.8.1 of the ISO/IEC 18004:2006 specification for QR Code 2005 to with the GS1 General Specifications for GS1 QR Code refer: "Where the EAN/UCC specifications call for the FNC1 character (in other symbologies which use this special character) to be used as a data field separator (i.e. at the end of a variable-length data field), QR Code 2005 symbols shall use the % character in Alphanumeric mode or character GS (byte value 1D HEX) in Byte mode to perform this function. If the % character occurs as part of the data it shall be encoded as %%." – Terry Burton Jul 09 '15 at 23:41
  • It is certainly a little frustrating how different the GS1 QR Code is compared with other GS1-adopted symbologies with regard to AI-formatted data. The use of an ASCII `%` character to represent FNC1 Application Identifier separators is a compromise that was first introduced when the original QR Code definition was amended to form ISO/IEC 18004:2000 as QR Code has no "out-of-band" formatting characters that could be adopted for this purpose, so an in-data approach had to be used. – Terry Burton Jul 09 '15 at 23:49