Detect and Extract Barcodes - .NET Core

This tutorial shows how to create a C# .NET Core 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# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Detect and Extract Barcodes - .NET Core tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License Tutorial tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License Tutorial tutorial. If you do not have that project, follow the steps in that tutorial to create it.

This tutorial requires the following NuGet package:

For a complete list of which DLLs are required for specific barcode features, refer to Barcode 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

How to properly add LEADTOOLS NuGet references is covered 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.

In the Solution Explorer, open Program.cs and add the below using statements to the block at the top:

C#
// Using block at the top 
using System; 
using System.Text; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 

In the Main() method add the below code to load the image that contains the barcodes.

C#
static void Main(string[] args) 
{ 
   if (!SetLicense()) 
   { 
      Console.WriteLine("Error setting license"); 
      return; 
   } 
   string filename = @"C:\LEADTOOLS21\Resources\Images\barcode1.tif"; 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      RasterImage image = codecs.Load(filename); 
      ReadBarcode(image); 
   } 
} 

In the Program class, add a new method called ReadBarcode(RasterImage image). This method will be called in the Main() method as shown above. Add the below code to detect all barcodes in the image and output their data to the console.

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 -> Start Debugging.

If the steps were followed correctly, the application should runs and outputs 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 from an image and display them into the .NET Core Application console using the LEADTOOLS SDK.

See Also

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