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 (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

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.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

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>\LEADTOOLS22\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 File Format Support.

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 file name, and call ReadBarcode(RasterImage image).

C#
// Using block at the top 
using System; 
using System.Text; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using System.IO; 
C#
static void Main(string[] args) 
{ 
    SetLicense(); 
    string filename = @"C:\LEADTOOLS22\Resources\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(); 
} 

Handling Streams

To utilize MemoryStream, replace the existing code in the LoadImage() method with the following:

C#
static RasterImage LoadImage(string filename) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
    { 
        byte[] bytes = File.ReadAllBytes(filename); 
        MemoryStream ms = new MemoryStream(bytes); 
        return codecs.Load(ms); 
    } 
} 

Run the Project

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

If the steps were followed correctly, the application 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 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

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