Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Read Barcodes from Images using LEADTOOLS

7 Jan 2013 1  
In this article, we will create a simple application that loads an image and reads its barcodes. Try it out for yourself by downloading a fully functional evaluation SDK from the links provided below the tutorial.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

Barcodes have a very prevalent role in today’s world.  From retail stores to mobile devices, hundreds of varieties of barcodes are in use day to day.  LEADTOOLS supports reading and writing nearly every barcode symbology and can be used in C++, .Net, Silverlight and Windows Phone.  Its high level programming interfaces can read and write barcodes in just a few lines of code.

On top of its expansive barcode support, LEADTOOLS is indispensible for image-based barcode applications because of its myriad of additional imaging technology including document cleanup, forms recognition and processing, OCR, TWAIN scanning, and support for over 150 formats.

In this article, we will create a simple application that loads an image and reads its barcodes.  Try it out for yourself by downloading a fully functional evaluation SDK from the links provided below the tutorial. 

Key Barcode Features in LEADTOOLS SDKs

  • Detect, Read and Write 1D and 2D Barcodes such as UPC, EAN, Code 128, Data Matrix, QR Code, PDF417
  • High level, programmer friendly .NET interface
    • Detect and read all barcode types with a single function call
    • Configuration options to read only barcodes of a certain type, color, granularity, orientation, region of interest and more
    • Automated image cleanup for 2D barcodes - ideal for poor quality faxes and scanned documents
  • Load, save and convert over 150 formats including JPEG, JPEG2000, TIFF, FAX, CMP & many more
  • Document Cleanup
  • Fast TWAIN and WIA image capture
  • High level image display controls for .NET, WPF & ASP.NET

Additional features include:

  • PDF Read/Write/Edit
  • OCR, MICR & OMR
  • Forms Recognition and Processing
  • Annotation and Markup
  • DICOM & PACS
  • Medical 3D Volume Reconstruction
  • Multimedia codecs and processing
  • MPEG-2 Transport Stream

Environment

This example requires Visual Studio 2008 or later and LEADTOOLS’ .NET 2.0 libraries.  Support for .NET 4.0 and Silverlight are also available.

Using the Barcode SDK

First, load an image with the RasterCodecs object.  This class handles all of the file I/O and image metadata features.  You may notice some of the references for individual codec files such as Leadtools.Codecs.Tif.dll, Leadtools.Codecs.Bmp.dll, etc.  These libraries add support for those file formats, so if you desire to load any additional files you will need to add a reference to the appropriate codec or you will get an “Invalid File Format” error.

using (OpenFileDialog ofd = new OpenFileDialog())
{
   ofd.Filter = "TIFF|*.tif|JPEG|*.jpg|PNG|*.png|BMP|*.bmp|All Files|*.*";
   if (ofd.ShowDialog() == DialogResult.OK)
   {
      using (RasterCodecs codecs = new RasterCodecs())
      {
         // Load first page into the viewer
         rasterImageViewer1.Image = codecs.Load(ofd.FileName, 0, 
             CodecsLoadByteOrder.BgrOrGray, 1, 1);
      }
   }
}

Next, read the barcodes with the BarcodeEngine object.  This example uses the default options to read all barcodes horizontally oriented barcodes.  Additional options exist for narrowing the search area, barcode type and barcode direction.

// Create a Barcode engine
BarcodeEngine engine = new BarcodeEngine();
 
// Ignore errors in case there are corrupted ones on the image.
engine.Reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;
 
// Read all barcodes with default options.
BarcodeData[] barcodes = engine.Reader.ReadBarcodes(rasterImageViewer1.Image, 
    LogicalRectangle.Empty, 0, null);
 
// Print out the barcodes we found
StringBuilder results = new StringBuilder();
results.AppendFormat("{0} barcodes found.\r\n\r\n", barcodes.Length);
for (int i = 0; i < barcodes.Length; i++)
{
   BarcodeData barcode = barcodes[i];
   results.AppendFormat("  {0} - {1} - {2}\r\n", i + 1, barcode.Symbology, 
       barcode.Value);
}

When running the sample, the image will be displayed in the viewer and all of the detected barcodes written in the textbox:

How-to-Read-Barcodes/image001.jpg

Conclusion

LEADTOOLS provides developers with access to the world’s best performing and most stable imaging libraries in an easy-to-use, high-level programming interface enabling rapid development of business-critical applications. 

Barcode is only one of the many technologies LEADTOOLS has to offer.  For more information on our other products, be sure to visit our home page, download a free fully functioning evaluation SDK, and take advantage of our free technical support during your evaluation.

Download the Barcode Example

You can download a fully functional demo which includes the features discussed above.  To run this example you will need the following:

Support

Need help getting this sample up and going?  Contact our support team for free technical support!  For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here