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



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


Groups: Registered, Tech Support, Administrators
Posts: 153

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.


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.


      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;

         // 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);

The process can be reversed to retrieve the original information.


      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.

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-2023, Yet Another Forum.NET
This page was generated in 0.038 seconds.