C# SDK - installation
Using the barKoder SDK in a C# .NET Console Application #
The barKoder SDK provides powerful barcode decoding and ID Document decoding (MRZ) capabilities. This guide will help you set up and use our barcode API's in your C# .NET console application.
Installation #
1. Choose the NuGet package: #
-
BarkoderSDK.(VERSION).nupkg: barcodes only. .NET Framework 4.7.2; x86, x64 and ARM64. -
BarkoderSDK.OCR.(VERSION).nupkg: barcodes plus MRZ (ID documents) and OCR text. .NET Framework 4.7.2; x64 and ARM64. -
BarkoderNET.(VERSION).nupkgandBarkoderNET.OCR.(VERSION).nupkg: the same two options for .NET 8 and later; x64 and ARM64.
2. Add it to your project: #
- The packages are part of the Windows SDK download from the barKoder Repository (they are not on nuget.org). Place the package you need in your local NuGet repository.
- Install the package into your C# project from your local repository.
BarkoderSDK (barcodes only).Getting Started #
Follow these steps to use the barKoder SDK in your C# application:
1. Import the barKoder SDK: #
Add the following to your Program.cs file:
using BarkoderNET;
2. Explore Sample Code: #
Review the sample_csharp (.NET Framework) and sample_dotnet (.NET 8) demo applications in the SDK download for common usage patterns.
3. Bitmap Conversion to Grayscale: #
The SDK decodes an 8-bit grayscale image buffer. If you use the ConvertToGrayscale(Bitmap) helper from the sample code, include these imports:
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
4. Initialize with a License Key: #
Pass a valid trial or production license key to Config.InitializeWithLicenseKey. It returns a ConfigResult; check Success and use its Configuration:
ConfigResult configResult = Config.InitializeWithLicenseKey("YourLicenseKeyHere");
if (!configResult.Success)
{
Console.WriteLine($"License error: {configResult.ErrorMessage}");
}
Config config = configResult.Configuration;
5. Configure the Decoder: #
Enable the barcode types you need and adjust settings such as the decoding speed:
config.DecodingSpeed = DecodingSpeed.Slow;
config.MaximumResultsCount = 100;
config.SetEnabledDecoders(new List<DecoderType>
{
DecoderType.QR, DecoderType.Datamatrix, DecoderType.PDF417,
DecoderType.Code128, DecoderType.Ean13, DecoderType.UpcA
});
6. Decode an Image: #
Pass the grayscale buffer to Barkoder.DecodeImageMemory. Each result has BarcodeTypeName and TextualData; extra metadata (for example gs1 or parsed MRZ fields) is available through GetAllKeys and GetStringForKey:
using (var image = new Bitmap(args[0]))
{
byte[] grayscale = ConvertToGrayscale(image);
var results = Barkoder.DecodeImageMemory(config, grayscale,
image.Width, image.Height, BKColorFormat.BKCF_Grayscale);
if (results.Count == 0)
{
Console.WriteLine("No barcode found on image");
}
foreach (var result in results)
{
Console.WriteLine($"{result.BarcodeTypeName}: {result.TextualData}");
foreach (string key in result.GetAllKeys())
{
Console.WriteLine($" {key}: {result.GetStringForKey(key)}");
}
}
}
In the .NET 8 packages (BarkoderNET, BarkoderNET.OCR) the color format value is BKColorFormat.Grayscale.
7. Run Your Application: #
Build and run your application, specifying the image file name as the first (and only) command-line argument.
Using OCR (MRZ) Features #
The OCR packages also decode Machine-Readable Zones (MRZ) in ID documents and general OCR text. Follow these steps to enable OCR:
1. Install an OCR package: #
Use BarkoderSDK.OCR or BarkoderNET.OCR (x64 or ARM64). The barcode-only packages report that the build doesn't support OCR features.
2. Keep the OCR resources next to your application: #
The OCR model files (.onnx, .traineddata and latin_dict.txt) must be in the same folder as your application's .exe. The OCR package copies them to your build output; deploy them together with the .exe.
3. Enable MRZ Decoding: #
config.SetEnabledDecoders(new List<DecoderType> { DecoderType.IDDocument });
4. Enable General OCR Text Decoding: #
config.SetEnabledDecoders(new List<DecoderType> { DecoderType.OCRText });
config.SetCustomOption("enable_ocr_functionality", 1);
5. Use an OCR (MRZ) Enabled License Key: #
Ensure your license key supports OCR features. This is mandatory for OCR functionality.