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 |
Try it in another language |
|
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.
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:
Leadtools.Barcode
If local references are being used, this tutorial requires the following local DLLs, which are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Tif.dll
Leadtools.Codecs.Fax.dll
Leadtools.Barcode.dll
Leadtools.Barcode.OneD.dll
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.
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.
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)
.
// Using block at the top
using System;
using System.Text;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Barcode;
static void Main(string[] args)
{
SetLicense();
string filename = @"C:\LEADTOOLS21\Resources\Images\barcode1.tif";
RasterImage image = LoadImage(filename);
ReadBarcode(image);
}
static RasterImage LoadImage(string filename)
{
using (RasterCodecs codecs = new RasterCodecs())
return codecs.Load(filename);
}
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 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
.
This tutorial showed how to read barcode information into the console using the LEADTOOLS SDK.