LEADTOOLS Support
Imaging
Imaging SDK Examples
How-To: Encoding Binary Data in a Barcode
#1
Posted
:
Tuesday, September 12, 2017 8:41:37 AM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 163
Was thanked: 9 time(s) in 9 post(s)
Typically, Barcodes are used to encode ASCII information. However, binary data can also be encoded by using the SetData() and GetData() methods.
BarCodeData.SetDataBarCodeData.GetDataThe following code snippet shows how to load an image, save it into a byte array, then encode the byte array into a QR code.
Code:
private static void WriteBarcodeToImage(string inputFile, string outputBarcode)
{
// load the input image and save it to a memory streasm
RasterCodecs codecs = new RasterCodecs();
RasterImage input = codecs.Load(inputFile);
MemoryStream ms = new MemoryStream();
codecs.Save(input, ms, input.OriginalFormat, input.BitsPerPixel);
// use SetData() to specify what data to encode in the barcode
QRBarcodeData qrd = new QRBarcodeData();
qrd.Symbology = BarcodeSymbology.QR;
qrd.SetData(ms.ToArray());
// create a new rasterimage to hold the generated barcode
RasterImage barcode = RasterImage.Create(1000, 1000, 1, 150, RasterColor.White);
// write the barcode to the rasterimage and save it to disk
BarcodeEngine engine = new BarcodeEngine();
engine.Writer.WriteBarcode(barcode, qrd, null);
codecs.Save(barcode, outputBarcode, input.OriginalFormat, 0);
codecs.Dispose();
}
The process can be reversed to retrieve the original information.
Code:
private static RasterImage ReadImageFromBarcode(RasterImage barcode)
{
RasterCodecs codecs = new RasterCodecs();
BarcodeEngine engine = new BarcodeEngine();
// read the binary data in the barcode
BarcodeData readBarcode = engine.Reader.ReadBarcodes(barcode, LogicalRectangle.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.QR })[0];
// pack the binary data into a memorystream, then load as an image
MemoryStream outStream = new MemoryStream(readBarcode.GetData());
RasterImage decodedImage = codecs.Load(outStream);
return decodedImage;
}
Note that barcodes have a finite upper limit of data which can be encoded dependent on symbology. Attempting to encode more binary data than a barcode symbology can support will cause an exception your application will need to handle. I've attached both the input image and the generated barcode.
Edited by user Monday, June 18, 2018 9:39:54 AM(UTC)
| Reason: Updating links to v20
Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Imaging
Imaging SDK Examples
How-To: Encoding Binary Data in a Barcode
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.