FAQ TBarCode DLL / TBarCode Library
How To Create a Structured Append QR Code
How To Create a Structured Append QR Code
Example, if you have a base64 string, which you want to encode with several QR-Codes linked together, follow these steps:
- Divide the base64 string into manageable sub-strings. The upper limit for a sub-string size is around 2,000 characters, but it depends on factors like scanner capabilities, available space, and print resolution. Generate a separate QR Code for each sub-string.
- Set the Structured-Append Header in each QR Code, specifying the total number of concatenated codes, the current index, and the parity byte. Calculate the parity byte over the entire string, not per individual code. The Structured-Append Header helps the scanner determine how many QR Codes need to be read and whether the data is correct.
Below is a sample C++ function for saving a QR Code with an Append Header:
ERRCODE SaveStructAppendQRCode(LPCSTR path, LPCSTR dataStr, int dataLen, int count, int index, BYTE parity)
{
ERRCODE eCode = ErrOk;
t_BarCode* pBarCode = NULL; // barcode handle
double const dpi = 300;
RECT rect = { 0,0,255,255 };
if (eCode == ErrOk)
eCode = BCAlloc(&pBarCode);
if (eCode == ErrOk)
eCode = BCSetBCType(pBarCode, eBC_QRCode);
if (eCode == ErrOk)
eCode = BCSet_QR_Append(pBarCode, count, index, parity);
if (eCode == ErrOk)
eCode = BCSetTextA(pBarCode, dataStr, dataLen);
if (eCode == ErrOk)
eCode = BCCreate(pBarCode);
if (eCode == ErrOk)
eCode = BCSetDPI(pBarCode, dpi, dpi);
if (eCode == ErrOk)
eCode = BCSetIgnoreDpiFromDC(pBarCode, TRUE);
if (eCode == ErrOk)
eCode = BCSetSizeMode(pBarCode, e_SizeMode::eSizeMode_FitToBoundingRect);
if (eCode == ErrOk)
eCode = BCGetOptimalBitmapSize(pBarCode, &rect, 3, 3);
if (eCode == ErrOk)
eCode = BCSaveImageA(pBarCode, path, eIMBmp, rect.right, rect.bottom, dpi, dpi);
// use BCSaveImageEx for color bitmap
if(pBarCode)
BCFree(pBarCode);
return eCode;
}
Next, you create individual QR Codes with Structured Append Headers as shown below:
ERRCODE eCode = ErrOk;
const char* base64Str = "Your base64 string goes here...";
int base64Len = strlen(base64Str);
if (base64Len > 400)
{
BYTE parity = BCCalcStructApp_ParityA(base64Str, base64Len);
if (eCode == ErrOk)
eCode = SaveStructAppendQRCode("c:\\temp\\qrcode_3-1.bmp", &base64Str[0], 200, 3, 1, parity);
if (eCode == ErrOk)
eCode = SaveStructAppendQRCode("c:\\temp\\qrcode_3-2.bmp", &base64Str[200], 200, 3, 2, parity);
if (eCode == ErrOk)
eCode = SaveStructAppendQRCode("c:\\temp\\qrcode_3-3.bmp", &base64Str[400], base64Len - 400, 3, 3, parity);
}
if (eCode != ErrOk)
{
char szErr[255];
BCGetErrorText(eCode, szErr, 255);
printf(szErr);
}
In the provided code, a loop for dynamically calculating the number of symbols and iterating through the base64 string is omitted for simplicity. Instead, it uses fixed sub-strings of 200 characters each with a total of 3 symbols in the chain. Make sure to adapt this as needed.
- Adjust the size of the barcode or the size of a matrix point using the ScaleX and ScaleY parameters in the BCGetOptimalBitmapSize(..) function.
- BCSaveImage(..) generates a monochrome BMP. For a color BMP, use BCSaveImageEx(..) or use the BitmapToFile(..) function.
Note that testing Structured Append QR Codes may require a scanner capable of correctly decoding them. Some apps or hardware scanners may not support Structured Append.