This tutorial shows how to create a barcode by encoding and then decoding raw binary data in a C# .NET Core application using the LEADTOOLS SDK. This can be useful when Barcode Symbologies use extended character sets.
Overview | |
---|---|
Summary | This tutorial covers how to encode and decode raw binary data in barcodes in a C# .NET Core Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core 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 Read and Write Bytes in a Barcode - C# .NET Core tutorial.
For a visual representation of the barcode symbologies supported by LEADTOOLS, refer to Supported Barcode Symbologies.
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:
Leadtools.Barcode
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
This tutorial uses QR Barcodes. For a complete list of which DLLs are required for other barcode symbologies, refer to Barcode 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 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.
In the Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top of Program.cs
.
using System;
using System.Text;
using Leadtools;
using Leadtools.Barcode;
Add two new methods named CreateBarcode(string data)
that returns a RasterImage object and ReadBarcode(RasterImage barcodeImage)
that returns a string value. Call both of these methods inside the Main()
method, as shown below.
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
// Create barcode and write barcode to image
RasterImage barcodeImage = CreateBarcode("Unicode-encoded data");
// Read barcode on image and display to console
string decodedText = ReadBarcode(barcodeImage);
// Console output
Console.WriteLine(decodedText);
}
Add the below code to the CreateBarcode(string data)
method to create a new QR barcode and encode raw binary data to it.
// Add these two methods
private static RasterImage CreateBarcode(string data)
{
byte[] chars = Encoding.Unicode.GetBytes(data);
RasterImage image = RasterImage.Create(2200, 3300, 24, 300, RasterColor.White);
BarcodeEngine eng = new BarcodeEngine();
QRBarcodeData qrBarcodeData = new QRBarcodeData();
qrBarcodeData.SetData(chars);
eng.Writer.WriteBarcode(image, qrBarcodeData, null);
return image;
}
Add the code below to the ReadBarcode(RasterImage barcodeImage)
method to decode the binary data from the newly created QR barcode.
private static string ReadBarcode(RasterImage barcodeImage)
{
BarcodeEngine eng = new BarcodeEngine();
BarcodeData[] data = eng.Reader.ReadBarcodes(barcodeImage, LeadRect.Empty, 0, eng.Reader.GetAvailableSymbologies());
if (data.Length > 0)
{
byte[] chars = data[0].GetData();
string value = Encoding.Unicode.GetString(chars);
return value;
}
return string.Empty;
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application creates a new barcode using raw byte data, then decodes that barcode and converts the raw byte data back to readable text, which is then displayed on the command line.
This tutorial showed how to create a barcode from raw byte data, then decode that barcode back into the raw byte data it was created from. This raw byte data can be either binary information or a string converted from a different character set. Also, we covered how to use the BarcodeEngine
and BarcodeData
classes.