Detect and Extract Barcodes - C# .NET 6

This tutorial shows how to read barcodes from an image and display their data on the console in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use the BarcodeReader Class in a C# .NET 6 Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
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 this 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. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

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:

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 System; 
using System.Text; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 
using System.IO; 

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

C#
string filename = @"C:\LEADTOOLS23\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(); 
} 

Handling Streams

To load a file using memory stream add the following code into the Main(string[] args) method under the string filename declaration:

C#
byte[] bytes = File.ReadAllBytes(filename);  
using (MemoryStream ms = new MemoryStream(bytes))  
{  
   ms.Position = 0;  
   // codecs.Load(ms); 
   RasterImage image = codecs.Load(ms); 
   ReadBarcode(image); 
} 

Run the Project

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

If the steps were followed correctly, the application 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 6 Application console using the LEADTOOLS SDK. It also showed how to use the BarcodeEngine and BarcodeReader classes.

See Also

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

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