Introduction
This step-by-step tutorial will guide you through how to build a complete C# application that analyzes 1D barcodes using the command line and a text editor, like Visual Studio Code.
Steps
-
Create a new console application project from the command line by creating a directory for your project and initializing it with the dotnet new command.
mkdir MyProject dotnet new console
-
Add the necessary dependencies to the generated project file MyProjectcsproj: We can add both required dependencies for this project through the command line:
dotnet add package Accusoft.BarcodeXpress.NetCore dotnet add package System.Drawing.Common
System.Drawing.Bitmap is required to load an image and pass it to Barcode Xpress for processing. That class is available in the System.Drawing.Common nuget package.
If you would like to use a local copy of Barcode Xpress instead of fetching one from Nuget, you can use an ItemGroup containing a reference instead of the PackageReference created by adding the packages via NuGet:
<ItemGroup> <Reference Include="Accusoft.BarcodeXpress.NetCore"> <HintPath>..\BarcodeXpress\Accusoft.BarcodeXpress13.NetCore.dll</HintPath> </Reference> </ItemGroup>
Additionally, add the using statements for System.Drawing and BarcodeXpressSdk to the top of your generated Program class:
using System; using System.Drawing; using Accusoft.BarcodeXpressSdk; namespace MyProject {
-
Create an instance of the BarcodeXpress class:
namespace MyProject { class Program { static void Main(string[] args) { BarcodeXpress barcodeXpress = new BarcodeXpress(); } } }
-
Acquire a System.Drawing.Bitmap object and pass it to the SDK’s Analyze method:
namespace MyProject { class Program { static void Main(string[] args) { BarcodeXpress barcodeXpress = new BarcodeXpress(); Bitmap bitmap = new Bitmap("barcode.bmp"); Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(bitmap); } } }
The simplest way to acquire a Bitmap object is to load one from a file. The Bitmap(string path) constructor loads an image from path and creates a new Bitmap object containing the image.
-
Access the fields of returned result objects to get data about barcodes detected in the image:
namespace MyProject { class Program { static void Main(string[] args) { BarcodeXpress barcodeXpress = new BarcodeXpress(); Bitmap bitmap = new Bitmap("barcode.bmp"); Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(bitmap); if (results.Length > 0) { foreach (Accusoft.BarcodeXpressSdk.Result result in results) { Console.WriteLine("{0} : {1}", result.BarcodeType.ToString(), result.BarcodeValue); } } else { Console.WriteLine("No Barcodes Found."); } } } }
BarcodeXpress.reader.Analyze returns an array of Result objects which contain information about all barcodes detected on the page. You can iterate over that array to access all known properties of each barcode.
-
Finally, clean up the Barcode Xpress object by releasing unmanaged resources using Dispose() method:
namespace MyProject { class Program { static void Main(string[] args) { BarcodeXpress barcodeXpress = new BarcodeXpress(); Bitmap bitmap = new Bitmap("barcode.bmp"); Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(bitmap); if (results.Length > 0) { foreach (Accusoft.BarcodeXpressSdk.Result result in results) { Console.WriteLine("{0} : {1}", result.BarcodeType.ToString(), result.BarcodeValue); } } else { Console.WriteLine("No Barcodes Found."); } barcodeXpress.Dispose(); } } }