Detect and Extract Barcodes - Console C#

This tutorial shows how to create a C# Windows Console application that uses the BarcodeEngine and BarcodeReader classes to read barcodes from an image and display their data on the console.

Overview  
Summary This tutorial covers how to use the BarcodeReader Class in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Detect and Extract Barcodes - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the below necessary LEADTOOLS references.

If NuGet references are being used, this tutorial requires the following NuGet package:

If local references are being used, this tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64

Note

The Add References and Set a License tutorial provides detailed instructions on using either method of adding LEADTOOLS references.

For a complete list of which Codec DLLs are required for specific formats, refer to Files to be Included in Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Barcode Reader Code

With the project created, the references added, and the license set, coding can begin.

Open the Program.cs in the Solution Explorer. In the Program class, add a new method called ReadBarcode(RasterImage image).

Then load the barcode image using its filename, and call ReadBarcode(RasterImage image).

C#
// Using block at the top 
using System; 
using System.Text; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    string filename = @"C:\Users\Public\Documents\LEADTOOLS Images\barcode1.tif"; 
    RasterImage image = LoadImage(filename); 
    ReadBarcode(image); 
} 
 
static RasterImage LoadImage(string filename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
         return codecs.Load(filename); 
} 
C#
static void ReadBarcode(RasterImage image) 
{ 
    BarcodeEngine barcodeEngineInstance = new BarcodeEngine(); 
 
    try 
    { 
        BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(image, LeadRect.Empty, 0, null); 
 
        StringBuilder sb = new StringBuilder(); 
        sb.AppendFormat("{0} barcode(s) found", dataArray.Length); 
        sb.AppendLine(); 
 
        for (int i = 0; i < dataArray.Length; i++) 
        { 
            BarcodeData data = dataArray[i]; 
 
            sb.AppendFormat("Symbology: {0}, Location: {1}, Data: {2}", data.Symbology.ToString(), data.Bounds.ToString(), data.Value); 
            sb.AppendLine(); 
        } 
        Console.WriteLine(sb.ToString()); 
    } 
    catch (Exception ex) 
    { 
        Console.WriteLine(ex); 
    } 
    Console.ReadLine(); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug then Start Debugging.

If the steps were followed correctly, the app should run properly and the console should output all relevant barcode information from the LEADTOOLS test file, barcode1.tif.

The application runs and the console detects the barcodes and displays the extracted information

Wrap-up

This tutorial showed how to read barcode information into the console using the LEADTOOLS SDK.

See Also

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