Getting strange double DataMatrix code when encoding <GS> character

Skip to first unread message

Karl Mårtensson

Nov 4, 2015, 7:08:30 AM11/4/15
to zxing

I get my DataMatrix code using the following line:

BitMatrix bitMatrix = new DataMatrixWriter().encode(dmie.preEncodeBarcode(dataToEncode), BarcodeFormat.DATA_MATRIX, 50, 50, null);

The input string I get contains "FNC1" (just as plain characters in the string) to mark the end of a dynamic field, and in total the string has 4 different GS1 DataMatrix Application Identifiers and their respective values.

What preEncodeBarcode() does is to replace "FNC1" with <GS>, like so:

input = input.replaceAll("FNC1", new String(new byte[] {0x1d}));

Since otherwise I simply get "FNC1910005FNC1230202[...]" encoded in the DataMatrix, while what I want is of course <GS> instead of the text "FNC1".

However, when replacing FNC1 with <GS> (I've tried using '\u001c' as well), I get the attached very strange double-DM-code instead of a normal one. Only if I skip replacing FNC1 with <GS> do I get a proper one.

Any idea to get a proper DataMatrix code based on my <GS>-containing String?

(PS. I am using the code found here: to scale the DataMatrix to the size I want, but it's zxing that gives the wrong output from the start.)


Karl Mårtensson

Nov 4, 2015, 9:07:51 AM11/4/15
to zxing
I'm not sure, but I might be on to some strange sort of bug here. This is what I'm sending to the DataMatrixWriter once I've preprocessed the input string (spaces are <GS>):

[d29100001 21000000049347037 24000163718 390300000002990

What I find rather obscure is that if I (at the time of writing) send input.substring(2, input.length()); or input.substring(0, input.length()-3); then it works just fine, while if I instead remove only one (or less) character from the beginning or 2 or less from the end then I get this strange DataMatrix. What's even stranger is that this behaviour is not even consistent - if I add say 6 some random numbers at the end then it works fine, but if I then remove three of those numbers then I again I get the problem. And didn't work an hour ago now works.

In other words, I'm utterly perplexed.

Lachezar Dobrev

Nov 4, 2015, 10:19:22 AM11/4/15
to Karl Mårtensson, zxing
  Hello Karl,
  Do you have an example of how the Data Matrix with the specified content should look like?
  Data Matrix supports rectangular matrices, and there is some logic when choosing the best available fit for a particular content. If you insist on having a square shape you can request that by using a hint when encoding:

  EncodeHintType.DATA_MATRIX_SHAPE = SymbolShapeHint.FORCE_SQUARE

  When is comes to GS1 Data Matrix… Well it might be supported less than ideal.
  I'm trying some things to help support GS1 Data Matrix format, but I'm at a loss.
  I'm attaching an example Data Matrix that should contain (sans spaces):
    FNC1 9100001 FNC1 21000000049347037 FNC1 24000163718 FNC1 390300000002990

  Is this what you expect?

You received this message because you are subscribed to the Google Groups "zxing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit


Karl Mårtensson

Nov 5, 2015, 2:51:06 AM11/5/15
to zxing,
Hi Lachezar,

This indeed, seems to have been the missing piece of the puzzle, as I now get exactly what I expect.

However, on the topic of GS1 and FNC1:

According to the GS1 standard, as far as I'm able to understand it, a first or second position FNC1 should be coded as "[d2" (although I am still uncertain if it should be encoded as these three characters or if it should encoded as another strange ASCII symbol?), while any consecutive ones should be encoded as the ASCII character <GS>.

Some libraries I've tried have automatically converted and encoded a string with parentheses around the application identifiers, as such: (91)00001(23)738494[...]. If I scan that with my phone I can't see any "[d2" (although it could still be there?), but the <GS> show up as spaces.

Seemingly irrespectively of how much I read about it, I can't seem to find a straight answer concerning how to handle the initial and consecutive FNC1's when encoding DataMatrix with zxing. Would you be able to enlighten me?

Karl Mårtensson

Nov 10, 2015, 5:12:04 AM11/10/15
to zxing,
I just wanted to mention that I went on to trying out Okapi Barcode which can generate GS1 compatible DataMatrix codes that work perfectly. So if you want an "easy" solution maybe you could simply use their DataMatrix encoder instead?

Sean Owen

Nov 11, 2015, 10:32:05 AM11/11/15
to zxing,
Yeah the handling of FNC1 has always confused me, since it varies and is sometimes not clear how and when to apply what rule.

However I think that generally FNC1 is translated as ASCII 29. I have never see "[d2" that I remember. You can of course apply whatever logic you want in the caller to further translate it, but it's there.

"FNC1" is not how you'd specify this character in input. That's a four-character string. Does writing in ASCII 29 work?

A "double DataMatrix" is just a rectangular one. It's perfectly valid, but I think you can tell the encoder to only render a square one.
Reply all
Reply to author
0 new messages