Programming with LEADTOOLS Barcode

LEADTOOLS Barcode toolkits are the most comprehensive barcode toolkits on the market, supporting reading and writing of over 100 different 1D and 2D barcode symbologies (including sub-types):

The barcodes can be read and written from/to any of the hundreds of image file formats supported by LEADTOOLS. For a complete list of image file formats, refer to Summary of All Supported File Formats.

For descriptions and visual samples of the barcode symbologies supported by LEADTOOLS, refer to Supported Barcode Symbologies.

For a chart showing which platforms support the Leadtools.Barcode assembly, refer to Supported Environments. Note that HTML5/JavaScipt supports writing barcodes in the Leadtools.Document assembly.

Overview of LEADTOOLS Barcode SDK Technology

For more information, refer to:

Programming Prerequisites

The LEADTOOLS Barcode support is accessed through the Leadtools.Barcode.dll assembly. The actual barcode read/write code is included in separate assemblies that are loaded dynamically and accessed at runtime. Dynamic loading minimizes the runtime footprint when certain barcode symbologies and features are not needed by your application. The following table lists the .NET Barcode support assemblies and their functionality:

Assembly Description
Leadtools.Barcode.OneD.dll 1D Barcodes read/write support
Leadtools.Barcode.DatamatrixRead.dll Datamatrix barcodes read support
Leadtools.Barcode.DatamatrixWrite.dll Datamatrix barcodes write support
Leadtools.Barcode.PdfRead.dll PDF417 and MicroPDF417 barcodes read support
Leadtools.Barcode.PdfWrite.dll PDF417 and MicroPDF417 barcodes write support
Leadtools.Barcode.QrRead.dll QR barcodes read support
Leadtools.Barcode.QrWrite.dll QR barcodes write support
Leadtools.Barcode.Lead2DRead.dll Aztec, Maxi and MicroQR barcodes read support
Leadtools.Barcode.Lead2DWrite.dll Aztec, Maxi and MicroQR barcodes write support

For details about Redistributables Paths, Dependencies, NuGet Packages, Sample Programs, and supported Barcode Formats for the .NET, Java/Android, iOS, macOS, JavaScript, Linux, Xamarin, UWP, or Objective C platforms, refer to Getting Started.

For more information, refer to Files to be Included in Your Application.

Refer to the Reading Barcodes and Writing Barcodes tutorials for a step-by-step instruction on how to create a C# barcode application using LEADTOOLS.

The BarcodeEngine class is the main entry to barcode support in Leadtools.Barcode. Simply create a new instance of this class and then obtain a reference to the BarcodeReader object used to read barcode objects though the BarcodeEngine.Reader property or the BarcodeWriter object used to write barcode objects through the BarcodeEngine.Writer property.

Reading Barcodes

To read barcodes, get an instance to the BarcodeReader object stored in the BarcodeEngine.Reader property, then call any of the BarcodeReader.ReadBarcode or BarcodeReader.ReadBarcodes methods passing the RasterImage object containing the image data, an optional search rectangle, optional maximum number of barcodes, optional barcode symbologies (types) and optional extra options. These methods return one or an array of the BarcodeData object containing the barcode data found. Here is an example:

// Read all barcodes in the image    
// 1st parameter: theImage is the RasterImage object     
// 2nd parameter: A LeadRect.Empty means all images  
// 3rd parameter: A 0 means all barcodes found   
// 4th parameter: A null array of BarcodeSymbology means all     
BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes( 
   theImage,                
   LeadRect.Empty,  
   0, 
   null); 

The BarcodeReader.ReadSymbology event is triggered whenever a barcode is found (or when an error occurs). Subscribe to this event to get information about the barcodes being read and to control whether the whole operation should be aborted or continued in case of an error.

The supported barcode types (symbologies) are defined in the BarcodeSymbology enumeration. The following snippet will search and try to read a single QR barcode from the image:

BarcodeData data = barcodeEngineInstance.Reader.ReadBarcode( 
   theImage,               \\ The RasterImage object 
   LeadRect.Empty, \\ Search rectangle, Empty means all image 
   BarcodeSymbology.QR);   \\ Symbology, only QR 

Fine-tune the barcode reading operation by passing one or more objects derived from BarcodeReadOptions. For example, to change the search direction to both horizontal and vertical barcodes for PDF417, change the default options used by the barcode reader like this:

// Get the default PDF417 read options 
PDF417BarcodeReadOptions options = barcodeEngineInstance.Reader.GetDefaultOptions(BarcodeSymbology.PDF417); 
// Change the search direction 
options.SearchDirection = BarcodeSearchDirection.HorizontalAndVertical; 
// Read a PDF417 barcode from the image 
BarcodeData data = barcodeEngineInstance.Reader.ReadBarcode(theImage, LeadRect.Empty,  BarcodeSymbology.PDF417); 

Or alternatively, pass the options directly to this overload method, which only applies these options to this one read operation and not to any subsequent operations:

BarcodeData data = barcodeEngineInstance.Reader.ReadBarcode(theImage, LeadRect.Empty, BarcodeSymbology.PDF417, options);

The BarcodeData object(s) returned from these methods contain the barcode data found. The BarcodeData.Symbology property will contain the symbology (type) of the barcode found; the BarcodeData.GetData method will return the raw byte array of the barcode data read (with BarcodeData.Value as the string representation of the data). The BarcodeData.Bounds and BarcodeData.RotationAngle will contain the location in pixels and any rotation angle of the barcode found on the image.

When reading some barcode types, extra information may be stored in the barcode other than the data, such as the symbol size of a Datamatrix barcode or the group number of a PDF417 barcode. For these, LEADTOOLS defines derived classes from BarcodeData (DatamatrixBarcodeData and PDF417BarcodeData in the cases mentioned) that contains this extra data. Check if the symbology is of the correct type and then cast the returned BarcodeData object to the derived class type.

Writing Barcodes

To write a barcode to an image, get an instance to the BarcodeWriter object stored in the BarcodeEngine.Writer property. Then, call BarcodeWriter.WriterBarcode passing the RasterImage object that contains the image data to write the barcode, and a BarcodeData object that contains the data and bounds of the desired barcode. Here is an example:

// Create a UPC A barcode 
BarcodeData data = new BarcodeData(); 
data.Symbology = BarcodeSymbology.UPCA; 
data.Value = "01234567890"; 
data.Bounds = new LeadRect(10, 10, 600, 200); 
// Write it with default options 
barcodeEngineInstance.Writer.WriteBarcode(theImage, data, null); 

Use one or more of the BarcodeWriteOptions-derived classes to add extra functionality to the write operation. For example, the following code will cause all subsequent write operations to show the barcode text on the bottom when 1D barcodes are written:

OneDBarcodeWriteOptions options = barcodeEngineInstance.Writer.GetDefaultOptions(BarcodeSymbology.UPCA) as OneDBarcodeWriteOptions; 
options.TextPosition = OneDBarcodeTextPosition.Default; 
// Write it with default options 
barcodeEngineInstance.Writer.WriteBarcode(theImage, data, null); 

Or you can write just this barcode with these options using this:

barcodeEngineInstance.Writer.WriteBarcode(theImage, data, options);

When writing barcodes, you may want to calculate the barcode size in pixels and fine tune the options before committing (for example, change the XModule spacing). Use the BarcodeWriter.CalculateBarcodeDataBounds method to fill the BarcodeData.Bounds property with the size in pixels of the requested barcode with the requested options.

Supported Barcode Types (Symbologies)

LEADTOOLS supports the reading and writing of several major types of barcodes. These types include:

The individual supported symbologies are defined by the BarcodeSymbology enumeration.

For each major type of barcode, one or more subtypes are supported for both reading and writing. These subtypes are:

Linear (1D) Barcodes:

2D

For descriptions and visual samples of the barcode symbologies supported by LEADTOOLS, refer to Supported Barcode Symbologies.

Help Version 23.0.2024.3.4
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.