2

We use ZXing.Net to generate code 128 barcode and everything works fine.

BarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.CODE_128 };
writer.Write("01950123456789031000012317150801").Save("code128.png", ImageFormat.Png);

enter image description here

Recently we have been asked to change the format to GS1-128 and after some research we found this solution using FNC1 (ASCII 29) to write DataMatrix GS1. https://stackoverflow.com/a/43575418/823247

BarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.DATA_MATRIX };
writer.Write($"{(char)29}019501234567890310000123{(char)29}17150801").Save("gs1datamatrix.png", ImageFormat.Png);

enter image description here

However, if we change the barcode format to BarcodeFormat.CODE_128, the program will throw exception showing Bad character in input:

BarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.CODE_128 };
writer.Write($"{(char)29}019501234567890310000123{(char)29}17150801").Save("gs1code128.png", ImageFormat.Png);

Any advice will be appreciated.

Terry Burton
  • 2,801
  • 1
  • 29
  • 41
walterhuang
  • 574
  • 13
  • 24

2 Answers2

3

Instead of "(char)29" you have to use the value "(char)0x00F1" as group separator.

Michael
  • 2,361
  • 1
  • 15
  • 15
  • Thanks! Work like a charm! Does this mean every barcode format has its own group separator? – walterhuang Sep 20 '17 at 18:46
  • 1
    Yes and no. I think 29 is the most common one. Not sure why there was chosen another one for GS1-128. But this two barcode types are the only ones which are supported by zxing for GS1. – Michael Sep 20 '17 at 19:08
0

There is an easier approach to generate GS1-128 barcode via Zxing.Net without manually adding character FNC1 to content.

You should only set the flag GS1Format = true

barcodeWriterSvg.Format = BarcodeFormat.CODE_128;
barcodeWriterSvg.Options.GS1Format = true;
barcodeWriterSvg.Options.Margin = 64;

var svgImage = barcodeWriterSvg.Write(content);
return svgImage.Content;

But the original solution based on manually adding FNC1 character to the beginning of string https://github.com/micjahn/ZXing.Net/wiki/Generating-GS1-DataMatrix-and-GS1-Code128-barcodes

StuS
  • 817
  • 9
  • 14
  • At the moment the option GS1Format is only used by the QR code encoder. It only adds the GS1 format identificator FNC1 at the first position. There are not group separators or similar control characters added. – Michael Mar 17 '21 at 06:29