Read and Write Bytes in a Barcode - C# .NET Core

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

Required Knowledge

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.

Barcode Symbologies

For a visual representation of the barcode symbologies supported by LEADTOOLS, refer to Supported Barcode Symbologies.

Create the Project and Add LEADTOOLS References

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:

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.

Set the License File

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.

Add the Barcode Encoding and Decoding Code

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.

C#
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.

C#
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.

C#
// 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.

C#
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

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.

Decoded barcode data displayed to the user.

Wrap-up

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.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.