Get Barcode Type and Encoded Data
Decode Barcode Type and Data
To get the data encoded in a barcode and identify its type, class BarCodeResult provides the two most important fields, CodeText and CodeType, respectively. The other parameter CodeTypeName represents the text name of a barcode symbology.
The following code sample explains how to get the data encoded in a barcode and its type for the sample barcode image provided below (in this case, a QR Code label).
//create barcode
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR, "Åspóse.Barcóde©"))
{
gen.Parameters.Barcode.XDimension.Pixels = 4;
gen.Save($"{path}QRCodetext.png", BarCodeImageFormat.Png);
}
//recognize image
Console.WriteLine("ReadExtCodetext:");
using (BarCodeReader read = new BarCodeReader($"{path}QRCodetext.png", DecodeType.QR))
{
foreach (BarCodeResult result in read.ReadBarCodes())
{
Console.WriteLine($"CodeText:{result.CodeText}");
Console.WriteLine($"CodeType:{result.CodeType.ToString()}");
Console.WriteLine($"CodeTypeName:{result.CodeTypeName}");
}
}
Read Barcode Data as Byte Stream
In cases when it is necessary to get the data encoded in a barcode in the form of a byte stream, it can be read from the specific field of class BarCodeResult that is called CodeBytes.
The following code snippet illustrates how to obtain barcode data as a stream of bytes for the sample PDF417 barcode image provided below.
byte[] encodedArr = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9 };
//encode array to string
StringBuilder strBld = new StringBuilder(encodedArr.Length);
foreach (byte bval in encodedArr)
strBld.Append((char)bval);
//encode array of bytes
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.Pdf417, strBld.ToString()))
{
gen.Parameters.Barcode.XDimension.Pixels = 2;
gen.Parameters.Barcode.Pdf417.Pdf417CompactionMode = Pdf417CompactionMode.Binary;
gen.Parameters.Barcode.Pdf417.Columns = 2;
gen.Parameters.Barcode.CodeTextParameters.TwoDDisplayText = "Bytes mode";
gen.Save($"{path}ExtCodeBytes.png", BarCodeImageFormat.Png);
}
//attempt to recognize array of bytes
Console.WriteLine("ReadExtCodeBytes:");
using (BarCodeReader read = new BarCodeReader($"{path}ExtCodeBytes.png", DecodeType.Pdf417))
foreach (BarCodeResult result in read.ReadBarCodes())
{
Console.WriteLine($"CodeTypeName:{result.CodeTypeName}");
Console.WriteLine($"CodeBytes:{BitConverter.ToString(result.CodeBytes)}");
}
Decode Barcode Data in Unicode Format
In cases when the barcode data is encoded using a particular Unicode encoding, it can be read by calling the GetCodeText method and specifying the required encoding.
The following code snippet shows how to get the data encoded in UTF8 as a result of reading a sample Data Matrix barcode given below.
//create encoded Unicode codetext
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.DataMatrix, "Aspose常に先を行く"))
{
gen.Parameters.Barcode.XDimension.Pixels = 4;
gen.Parameters.Barcode.DataMatrix.CodeTextEncoding = Encoding.UTF8;
gen.Save($"{path}ExtUnicodeCodeText.png", BarCodeImageFormat.Png);
}
//try to recognize Unicode codetext
Console.WriteLine("ReadExtUnicodeCodeText:");
using (BarCodeReader read = new BarCodeReader($"{path}ExtUnicodeCodeText.png", DecodeType.DataMatrix))
foreach (BarCodeResult result in read.ReadBarCodes())
{
Console.WriteLine($"CodeTypeName:{result.CodeTypeName}");
Console.WriteLine($"GetCodeText:{result.GetCodeText(Encoding.UTF8)}");
}