Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Tuesday, September 12, 2017 8:41:37 AM(UTC)

Nick  
Nick

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.SetData
BarCodeData.GetData

The 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.

input-rgby.pngimage-bc.png

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.
LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

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.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.043 seconds.