How-To create Barcodes in C/AL using the ZXing.NET library

In a recent project, I had the requirement to create barcodes in a simple and straightforward way. In this context I came across the Open-Source component ZXing. For this component, which is originally implemented in Java, there is fortunately also a C# port on GitHub available.

With its help, you can read and write barcodes of the following formats with just a few lines of code using DotNet. Alternatively, it should be easy to implement this as a microservice in Azure Functions and get your barcode via http call.

Encode:
UPC-A, EAN-8, EAN-13, Code 39, Code 128, ITF, Codabar, Plessey, MSI, QR Code, PDF-417, Aztec, Data Matrix

Decode:
UPC-A, UPC-E, EAN-8, EAN-13, Code 39, Code 93, Code 128, ITF, Codabar, MSI, RSS-14 (all variants), QR Code, Data Matrix, Aztec and PDF-417

Create Barcodes

The initial task was to retrieve decoded information through a webservice and then re-encode it into a barcode. The resulting barcode could then be shown on a special page which will be scanned through the user for further processing.

The implementation is straight forward C/AL (Dynamics NAV or Business Central OnPrem) using old fashioned DotNet. I create a Data Matrix barcode on-the-fly using Rec.GenerateBarcode() in a page. The barcode is then saved in a Blob field of type image and could be shown i.e. in a factbox.

But keep in mind that blob fields might impact performance…

LOCAL GenerateBarcode(TextToEncode : Text)
//Barcodes in C/AL
//MemoryStream Instance
MemoryStream := MemoryStream.MemoryStream;

//Barcode Instance
BarcodeWriter := BarcodeWriter.BarcodeWriter;

//Barcode Format
BarcodeWriter.Format := BarcodeFormat.DATA_MATRIX;

//Barcode Options
BarcodeEncodingOptions := BarcodeEncodingOptions.EncodingOptions;
BarcodeEncodingOptions.Height := 200;
BarcodeEncodingOptions.Width := 200;

BarcodeWriter.Options := BarcodeEncodingOptions;

//Create barcode and save image to MemoryStream
Bitmap := BarcodeWriter.Write(TextToEncode);
Bitmap.Save(MemoryStream, ImageFormat.Gif);

//Write to blob
Rec.Barcode.CREATEOUTSTREAM(OStream);
MemoryStream.WriteTo(OStream);
Rec.MODIFY;

Variables used for Barcodes in C/AL

NameTypeSubtype
BarcodeWriterDotNetZXing.BarcodeWriter
BarcodeFormatDotNetZXing.BarcodeFormat
BarcodeEncodingOptionsDotNetZXing.Common.EncodingOptions
MemoryStreamDotNetSystem.IO.MemoryStream
BitmapDotNetSystem.Drawing.Bitmap
ImageFormatDotNetSystem.Drawing.Imaging.ImageFormat
OStreamOutStream

The Result

Barcodes in C/AL

Read Barcodes

Reading barcodes in ZXing.NET is as easy as creating them. The component will automatically detect the format and return the content.

LOCAL ReadBarcode(VAR pInStream : InStream)

//MemoryStream Instance
MemoryStream := MemoryStream.MemoryStream;

//Copy the NAV InStream to MemoryStream
CopyStream(MemoryStream, pInStream);

//Rewind MemoryStream
MemoryStream.Position := 0;

//Map Barcode to Bitmap
Bitmap := Bitmap.FromStream(MemoryStream);

//Barcode Instance
BarcodeReader := BarcodeReader.BarcodeReader;

//Decode the Barcode
BarcodeResult := BarcodeReader.Decode(Bitmap);

//Get the Result
MESSAGE('Format: %1 - Content: %2', BarcodeResult.BarcodeFormat, BarcodeResult.Text);

Variables

NameTypeSubtype
BarcodeResultDotNetZXing.Result
BarcodeReaderDotNetZXing.BarcodeReader
MemoryStreamDotNetSystem.IO.MemoryStream
StreamDotNetSystem.IO.Stream
BitmapDotNetSystem.Drawing.Bitmap

Quite an easy way to read and create Barcodes in C/AL.

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published.