
In the previous post, you have seen how to generate and read different types of barcodes using C#. However, in certain cases, you have to deal with non-English characters. For example, when you work with Arabic, Latin, Greek, or similar languages. In such cases, you may need to encode the characters into Unicode standards i.e. UTF-8. In accordance with that, this article covers how to generate and read barcodes using UTF-8 encoding in C#.
- C# API to Generate Barcodes using UTF-8 Encoding
- Generate Barcode with UTF-8 Encoding in C#
- Read Barcode with UTF-8 Encoding in C#
- Get a Free API License
C# API to Generate Barcodes using UTF-8 Encoding
Aspose.BarCode for .NET is a powerful C# API for barcode generation and recognition. Using the API, you can work with a wide range of barcode symbologies. In addition, the API supports generating barcodes using UTF-8 encoding. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.BarCode
Generate Barcode using UTF-8 Encoding in C#
The following are the steps to generate barcode using UTF-8 encoding.
- First, create an instance of BarcodeGenerator class and specify the barcode type using EncodeTypes.
- Set barcode’s text using BarcodeGenerator.CodeText property.
- Set UTF-8 text encoding using BarcodeGenerator.Parameters.Barcode.QR.CodeTextEncoding property (replace QR according to the barcode type you have specified in BarcodeGenerator constructor).
- Generate barcode using BarcodeGenerator.GenerateBarCodeImage() method and save returned image into a Bitmap object.
- Finally, save the barcode image as a file using Bitmap.Save(String) method.
The following code sample shows how to generate a barcode using UTF-8 encoding in C#.
// Create a barcode generator | |
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Pdf417); | |
// Set barcode text | |
generator.CodeText = "منحة"; | |
// Set resolution | |
generator.Parameters.Resolution = 400; | |
// Set encoding type | |
generator.Parameters.Barcode.Pdf417.CodeTextEncoding = Encoding.UTF8; | |
// Generate barcode | |
Bitmap imgBarcode = generator.GenerateBarCodeImage(); | |
// Save barcode image | |
imgBarcode.Save("generate-barcode.png"); |
Read UTF-8 Encoded Barcode using C#
The following are the steps to recognize UTF-8 encoded barcode using C#.
- Use BarCodeReader class to load the barcode image.
- Loop through each BarCodeResult returned by BarCodeReader.ReadBarCodes() method.
- Create an object of the Encoding class and set encoding to UTF-8.
- Get the char array from the bytes returned by the BarCodeResult object.
- Build Unicode string from the characters to get the barcode text.
- Finally, print the retrieved barcode text.
The following code sample shows how to recognize barcodes using UTF-8 encoding in C#.
// Recognize the above barcode | |
using (BarCodeReader reader = new BarCodeReader("generate-barcode.png")) | |
{ | |
// Read barcodes | |
foreach (BarCodeResult result in reader.ReadBarCodes()) | |
{ | |
// Set encoding | |
Encoding unicode = Encoding.UTF8; | |
// Get the characters array from the bytes | |
char[] unicodeChars = new char[unicode.GetCharCount(result.CodeBytes, 0, result.CodeBytes.Length)]; | |
unicode.GetChars(result.CodeBytes, 0, result.CodeBytes.Length, unicodeChars, 0); | |
// Build unicode string | |
string strCodeText = new string(unicodeChars); | |
Console.WriteLine(strCodeText); | |
} | |
} |
Get a Free API License
In case you are interested in trying and using the API, you can get a free temporary license.
Conclusion
In this article, you have learned how to generate barcodes using UTF-8 encoding in C#. Furthermore, you have seen how to recognize and read a UTF-8 encoded barcode. You can explore more about the C# barcode API using the documentation.