walterhuang
walterhuang

Reputation: 622

Generate GS1-128 using ZXing.Net

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.

Upvotes: 3

Views: 6005

Answers (2)

StuS
StuS

Reputation: 857

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

Upvotes: 0

Michael
Michael

Reputation: 2486

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

Upvotes: 4

Related Questions