C Requires Chilkat v11.7.0+
C
Get the Modules of a QR Code (Bit Matrix)
See more QR Code Examples
Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>
#include <C_CkBinData.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject json;
HCkFormats fmt;
HCkJsonObject jsonOut;
int width;
int height;
int rowBytes;
HCkBinData bd;
HCkStringBuilder sbRow;
int row;
int col;
int b;
int mask;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"payload.text","CHILKAT");
CkJsonObject_UpdateBool(json,"output.includeModuleData",TRUE);
fmt = CkFormats_Create();
jsonOut = CkJsonObject_Create();
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
return;
}
width = CkJsonObject_IntOf(jsonOut,"symbols[0].moduleData.width");
height = CkJsonObject_IntOf(jsonOut,"symbols[0].moduleData.height");
rowBytes = CkJsonObject_IntOf(jsonOut,"symbols[0].moduleData.rowBytes");
printf("kind: %s\n",CkJsonObject_stringOf(jsonOut,"symbols[0].moduleData.kind"));
printf("modules: %d x %d, %d bytes per row\n",width,height,rowBytes);
// The matrix is height rows of rowBytes bytes.
bd = CkBinData_Create();
success = CkBinData_AppendEncoded(bd,CkJsonObject_stringOf(jsonOut,"symbols[0].moduleData.bytesBase64"),"base64");
if (success == FALSE) {
printf("%s\n",CkBinData_lastErrorText(bd));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkBinData_Dispose(bd);
return;
}
sbRow = CkStringBuilder_Create();
mask = 128;
for (row = 0; row <= height - 1; row++) {
CkStringBuilder_Clear(sbRow);
for (col = 0; col <= width - 1; col++) {
// The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
// The leftmost module of each byte is its most significant bit.
if ((col % 8) == 0) {
mask = 128;
}
b = CkBinData_GetByte(bd,(row * rowBytes) + (col / 8));
if (((b / mask) % 2) == 1) {
CkStringBuilder_Append(sbRow,"##");
}
else {
CkStringBuilder_Append(sbRow," ");
}
mask = mask / 2;
}
printf("%s\n",CkStringBuilder_getAsString(sbRow));
}
// The same symbol as the PNG that the call also returned:
//
// Sample output:
// kind: bitMatrix
// modules: 21 x 21, 3 bytes per row
// ############## ##############
// ## ## #### ## ##
// ## ###### ## ## #### ## ###### ##
// ## ###### ## ## ## ###### ##
// ## ###### ## ## ## ## ## ###### ##
// ## ## #### ## ## ##
// ############## ## ## ## ##############
// ####
// ## ########## #### ##########
// #### ## ######## ######
// #### ######## #### ## #### ####
// ## ## ############ #### ##
// ###### #### ###### ## ## ##
// ## ## ## ## ## ##
// ############## ## ## ## ##
// ## ## ## ## ###### ####
// ## ###### ## #### ## ## ##
// ## ###### ## ## ############ ##
// ## ###### ## ## ## #### ##
// ## ## ## ######## ##
// ############## ## ## ######
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkBinData_Dispose(bd);
CkStringBuilder_Dispose(sbRow);
}