Getting Started with Dynamsoft Barcode Reader SDK .NET Edition
In this guide, you will learn step by step on how to build a barcode reading application with Dynamsoft Barcode Reader SDK using C#.
Read more on Dynamsoft Barcode Reader Features
- Getting Started with Dynamsoft Barcode Reader SDK .NET Edition
- System Requirements
- Build Your First Application
- Process Multiple Images
- Import the Additional Namespace.
- Create an ImageSource as the Input
- Implement a CapturedResultReceiver as the Output Listener
- Handle Capture Stoppage by Implementing a ImageSource State Listener
- Register the Input, Output Listener and ImageSource State Listener to the CaptureVisionRouter Instance
- Start the Capturing Process
- Build and Run the Project Again
System Requirements
To find out whether your environment is supported, please read the System Requirements.
Build Your First Application
Let’s start by creating a console application which demonstrates how to use the minimum code to read barcodes from an image file.
Create a New Project
Start Visual Studio or your preferred C# IDE.
Create a new C# Console Application project. Let’s name it DBRCSharpSample
.
Install the NuGet Package
In the Solution Explorer, right-click on your project and select Manage NuGet Packages.
Search for and install the Dynamsoft.DotNet.BarcodeReader.Bundle
nuget package.
If you prefer to use the offline packages, please
Download the
.NET Package
now and extract it into a directory of your choice.Add the extracted
.\Dynamsoft\Packages
directory as a new package sourceInstall following packages from the added package source:
- Dynamsoft.DotNet.BarcodeReader
- Dynamsoft.DotNet.CaptureVisionRouter
- Dynamsoft.DotNet.Core
- Dynamsoft.DotNet.ImageProcessing
- Dynamsoft.DotNet.License
- Dynamsoft.DotNet.Utility
Import the Namespace
Open the Program.cs
file and add the following namespaces.
using Dynamsoft.DBR;
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.License;
Initialize the License Key
Open the Program.cs
file and add the following code inside the Main
method to initialize the license for using the SDK in the application:
int errorCode = 1;
string errorMsg;
errorCode = LicenseManager.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK && errorCode != (int)EnumErrorCode.EC_LICENSE_CACHE_USED)
Console.WriteLine("License initialization error: " + errorMsg);
The string “DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9” here is a free public trial license. Note that network connection is required for this license to work. When it expires, you can request a 30-day free trial license from the Customer Portal.
Create a CaptureVisionRouter Instance
using (CaptureVisionRouter cvr = new CaptureVisionRouter())
{
//code for invoking the barcode capturing
}
Invoke the Barcode Capturing
using (CaptureVisionRouter cvr = new CaptureVisionRouter())
{
string imageFile = "[PATH-TO-THE-IMAGE-FILE]";
CapturedResult? result = cvr.Capture(imageFile, PresetTemplate.PT_READ_BARCODES);
//code for filtering and getting barcode details
}
Please change the
[PATH-TO-THE-IMAGE-FILE]
to a real barcode image file path.Sample images are also available in the
\Dynamsoft\Resources\BarcodeReader\Images
directory within the downloaded.NET package
.
Filter and Get Barcode Details
if (result == null)
{
Console.WriteLine("No barcode detected.");
}
else if(result.GetErrorCode() != 0)
{
Console.WriteLine("Error: " + result.GetErrorCode() + ", " + result.GetErrorString());
}
else
{
DecodedBarcodesResult barcodesResult = result.GetDecodedBarcodesResult();
if (barcodesResult != null)
{
BarcodeResultItem[] items = barcodesResult.GetItems();
Console.WriteLine("Decoded " + items.Length + " barcodes");
foreach (BarcodeResultItem barcodeItem in items)
{
Console.WriteLine("Result " + (Array.IndexOf(items, barcodeItem) + 1));
Console.WriteLine("Barcode Format: " + barcodeItem.GetFormatString());
Console.WriteLine("Barcode Text: " + barcodeItem.GetText());
}
}
}
Build and Run the Project
Save the Program.cs
file and then compile and run the program using your IDE (such as Visual Studio). You will see the output message in the console like
Decoded 2 barcodes
Result 1
Barcode Format: XXX
Barcode Text: XXX
Result 2
Barcode Format: XXX
Barcode Text: XXX
Process Multiple Images
If, instead of processing one single image, you need to process many images at once, you can follow these steps:
These steps follow the step Create a CaptureVisionRouter Instance mentioned above.
Import the Additional Namespace.
using Dynamsoft.Utility;
Create an ImageSource as the Input
An ImageSource
is required when crreating a multi-image processing application. You can either utilize ready-made image sources such as FileFetcher
and DirectoryFetcher
, or customize your own image source based on the base class ImageSourceAdapter
.
In this sample, we will use the DirectoryFetcher
to retrieve images from a local directory.
DirectoryFetcher fetcher = new DirectoryFetcher();
fetcher.SetDirectory("[THE DIRECTORY THAT HOLDS THE IMAGES]");
Please change the
[THE DIRECTORY THAT HOLDS THE IMAGES]
to full path of the directory holding barcode image files.Sample images are also available in the
\Dynamsoft\Resources\BarcodeReader\Images
directory within the downloaded.NET package
.
Implement a CapturedResultReceiver as the Output Listener
Create a class MyCapturedResultReceiver
to implement the CapturedResultReceiver
interface, and get the barocde results in OnDecodedBarcodesReceived
callback function.
class MyCapturedResultReceiver : CapturedResultReceiver
{
public override void OnDecodedBarcodesReceived(DecodedBarcodesResult result)
{
FileImageTag? tag = (FileImageTag?)result.GetOriginalImageTag();
Console.WriteLine("File: " + tag.GetFilePath());
if (result.GetErrorCode() != (int)EnumErrorCode.EC_OK)
{
Console.WriteLine("Error: " + result.GetErrorString());
}
else
{
BarcodeResultItem[] items = result.GetItems();
Console.WriteLine("Decoded " + items.Length + " barcodes");
foreach (BarcodeResultItem item in items)
{
Console.WriteLine("Result " + (Array.IndexOf(items, item) + 1));
Console.WriteLine("Barcode Format: " + item.GetFormatString());
Console.WriteLine("Barcode Text: " + item.GetText());
}
}
Console.WriteLine();
}
}
Handle Capture Stoppage by Implementing a ImageSource State Listener
Create a class MyImageSourceStateListener
to implement the IImageSourceStateListener
interface, and call StopCapturing in OnImageSourceStateReceived
callback function when the state is ISS_EXHAUSTED
.
class MyImageSourceStateListener : IImageSourceStateListener
{
private CaptureVisionRouter? cvr = null;
public MyImageSourceStateListener(CaptureVisionRouter cvr)
{
this.cvr = cvr;
}
public void OnImageSourceStateReceived(EnumImageSourceState state)
{
if (state == EnumImageSourceState.ISS_EXHAUSTED)
{
if (cvr != null)
{
cvr.StopCapturing();
}
}
}
}
Register the Input, Output Listener and ImageSource State Listener to the CaptureVisionRouter Instance
cvr.SetInput(fetcher);
CapturedResultReceiver receiver = new MyCapturedResultReceiver();
cvr.AddResultReceiver(receiver);
MyImageSourceStateListener listener = new MyImageSourceStateListener(cvr);
cvr.AddImageSourceStateListener(listener);
Start the Capturing Process
Call the method StartCapturing()
to start processing all the images in the specified folder.
errorCode = cvr.StartCapturing(PresetTemplate.PT_READ_BARCODES, true, out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK)
{
Console.WriteLine("error: " + errorMsg);
}
During the process, the callback function OnDecodedBarcodesReceived()
is triggered each time processing of an image is finished. After all images are processed, the listener function OnImageSourceStateReceived()
will be triggered while the image source state is ISS_EXHAUSTED
and the process is stopped with the method StopCapturing()
.
Build and Run the Project Again
Please refer to Build and Run the Project.