How to create 2D barcode that stores text and binary data?
In This Topic
Value of 2D barcode can be divided into value items, i.e. barcode value can store mix of text and binary data.
Here is C#/VB.NET code that demonstrates how to create a 2D barcode with text and binary data.
using System;
using System.Text;
using Vintasoft.Barcode;
using Vintasoft.Barcode.BarcodeInfo;
using Vintasoft.Imaging;
/// <summary>
/// Test that shows how to create a 2D barcode image with text and binary data.
/// </summary>
class ValueItemsExample
{
/// <summary>
/// Runs the test.
/// </summary>
public static void Test()
{
// generate binary data
byte[] data = new byte[16];
for (int i = 0; i < data.Length; i++)
data[i] = (byte)(i * 16);
// create image with PDF417 barcode
using (VintasoftBitmap barcodeImage = CreateBarcodeWithTextAndBinaryData(BarcodeType.PDF417, data))
{
// read barcode from image
ReadBarcode(barcodeImage, BarcodeType.PDF417);
}
// create image with Aztec barcode
using (VintasoftBitmap barcodeImage = CreateBarcodeWithTextAndBinaryData(BarcodeType.Aztec, data))
{
// read barcode from image
ReadBarcode(barcodeImage, BarcodeType.Aztec);
}
// create image with DataMatrix barcode
using (VintasoftBitmap barcodeImage = CreateBarcodeWithTextAndBinaryData(BarcodeType.DataMatrix, data))
{
// read barcode from image
ReadBarcode(barcodeImage, BarcodeType.DataMatrix);
}
// create image with QR barcode
using (VintasoftBitmap barcodeImage = CreateBarcodeWithTextAndBinaryData(BarcodeType.QR, data))
{
// read barcode from image
ReadBarcode(barcodeImage, BarcodeType.QR);
}
}
/// <summary>
/// Creates image with barcode, which contains text and binary data.
/// </summary>
static VintasoftBitmap CreateBarcodeWithTextAndBinaryData(BarcodeType barcodeType, byte[] data)
{
// create text value item
TextValueItem textData = new TextValueItem("BINARY DATA: ");
// create binary value item
BinaryValueItem binaryData = new BinaryValueItem(data);
// create the barcode writer
using (BarcodeWriter writer = new BarcodeWriter())
{
// specify that writer must generate barcode of spefied type
writer.Settings.Barcode = barcodeType;
// set the barcode value items
writer.Settings.ValueItems = new ValueItemBase[] { textData, binaryData };
// create image with barcode
return writer.GetBarcodeAsVintasoftBitmap();
}
}
/// <summary>
/// Reads barcode from image and shows the barcode value items.
/// </summary>
static void ReadBarcode(VintasoftBitmap image, BarcodeType barcode)
{
// create the barcode reader
using (BarcodeReader reader = new BarcodeReader())
{
// specify that reader must search for barcodes of specified types
reader.Settings.ScanBarcodeTypes = barcode;
// read barcode from image
IBarcodeInfo barcodeInfo = reader.ReadBarcodes(image)[0];
// for each found barcode
for (int i = 0; i < barcodeInfo.ValueItems.Length; i++)
{
// show the barcode value tems
StringBuilder itemValue = new StringBuilder();
if (barcodeInfo.ValueItems[i] is BinaryValueItem)
{
byte[] data = ((BinaryValueItem)barcodeInfo.ValueItems[i]).Value;
for (int j = 0; j < data.Length; j++)
itemValue.Append(string.Format("{0} ", data[j]));
}
else
{
itemValue.Append(barcodeInfo.ValueItems[i].ToString());
}
Console.WriteLine(
string.Format("[{0}] {1}", barcodeInfo.ValueItems[i].GetType().Name, itemValue));
}
}
Console.WriteLine();
}
}
Imports System.Text
Imports Vintasoft.Barcode
Imports Vintasoft.Barcode.BarcodeInfo
Imports Vintasoft.Imaging
''' <summary>
''' Test that shows how to create a 2D barcode image with text and binary data.
''' </summary>
Class ValueItemsExample
''' <summary>
''' Runs the test.
''' </summary>
Public Shared Sub Test()
' generate binary data
Dim data As Byte() = New Byte(15) {}
For i As Integer = 0 To data.Length - 1
data(i) = CByte(i * 16)
Next
' create image with PDF417 barcode
Using barcodeImage As VintasoftBitmap = CreateBarcodeWithTextAndBinaryData(BarcodeType.PDF417, data)
' read barcode from image
ReadBarcode(barcodeImage, BarcodeType.PDF417)
End Using
' create image with Aztec barcode
Using barcodeImage As VintasoftBitmap = CreateBarcodeWithTextAndBinaryData(BarcodeType.Aztec, data)
' read barcode from image
ReadBarcode(barcodeImage, BarcodeType.Aztec)
End Using
' create image with DataMatrix barcode
Using barcodeImage As VintasoftBitmap = CreateBarcodeWithTextAndBinaryData(BarcodeType.DataMatrix, data)
' read barcode from image
ReadBarcode(barcodeImage, BarcodeType.DataMatrix)
End Using
' create image with QR barcode
Using barcodeImage As VintasoftBitmap = CreateBarcodeWithTextAndBinaryData(BarcodeType.QR, data)
' read barcode from image
ReadBarcode(barcodeImage, BarcodeType.QR)
End Using
End Sub
''' <summary>
''' Creates image with barcode, which contains text and binary data.
''' </summary>
Private Shared Function CreateBarcodeWithTextAndBinaryData(barcodeType As BarcodeType, data As Byte()) As VintasoftBitmap
' create text value item
Dim textData As New TextValueItem("BINARY DATA: ")
' create binary value item
Dim binaryData As New BinaryValueItem(data)
' create the barcode writer
Using writer As New BarcodeWriter()
' specify that writer must generate barcode of spefied type
writer.Settings.Barcode = barcodeType
' set the barcode value items
writer.Settings.ValueItems = New ValueItemBase() {textData, binaryData}
' create image with barcode
Return writer.GetBarcodeAsVintasoftBitmap()
End Using
End Function
''' <summary>
''' Reads barcode from image and shows the barcode value items.
''' </summary>
Private Shared Sub ReadBarcode(image As VintasoftBitmap, barcode As BarcodeType)
' create the barcode reader
Using reader As New BarcodeReader()
' specify that reader must search for barcodes of specified types
reader.Settings.ScanBarcodeTypes = barcode
' read barcode from image
Dim barcodeInfo As IBarcodeInfo = reader.ReadBarcodes(image)(0)
' for each found barcode
For i As Integer = 0 To barcodeInfo.ValueItems.Length - 1
' show the barcode value tems
Dim itemValue As New StringBuilder()
If TypeOf barcodeInfo.ValueItems(i) Is BinaryValueItem Then
Dim data As Byte() = DirectCast(barcodeInfo.ValueItems(i), BinaryValueItem).Value
For j As Integer = 0 To data.Length - 1
itemValue.Append(String.Format("{0} ", data(j)))
Next
Else
itemValue.Append(barcodeInfo.ValueItems(i).ToString())
End If
Console.WriteLine(String.Format("[{0}] {1}", barcodeInfo.ValueItems(i).[GetType]().Name, itemValue))
Next
End Using
Console.WriteLine()
End Sub
End Class