Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.7.0+

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 Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkFormatsW.h>
#include <C_CkBinDataW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW json;
    HCkFormatsW fmt;
    HCkJsonObjectW jsonOut;
    int width;
    int height;
    int rowBytes;
    HCkBinDataW bd;
    HCkStringBuilderW 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 = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"payload.text",L"CHILKAT");
    CkJsonObjectW_UpdateBool(json,L"output.includeModuleData",TRUE);

    fmt = CkFormatsW_Create();
    jsonOut = CkJsonObjectW_Create();
    success = CkFormatsW_QrCode(fmt,L"generate",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFormatsW_lastErrorText(fmt));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    width = CkJsonObjectW_IntOf(jsonOut,L"symbols[0].moduleData.width");
    height = CkJsonObjectW_IntOf(jsonOut,L"symbols[0].moduleData.height");
    rowBytes = CkJsonObjectW_IntOf(jsonOut,L"symbols[0].moduleData.rowBytes");
    wprintf(L"kind: %s\n",CkJsonObjectW_stringOf(jsonOut,L"symbols[0].moduleData.kind"));
    wprintf(L"modules: %d x %d, %d bytes per row\n",width,height,rowBytes);

    //  The matrix is height rows of rowBytes bytes.
    bd = CkBinDataW_Create();
    success = CkBinDataW_AppendEncoded(bd,CkJsonObjectW_stringOf(jsonOut,L"symbols[0].moduleData.bytesBase64"),L"base64");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bd));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        CkBinDataW_Dispose(bd);
        return;
    }

    sbRow = CkStringBuilderW_Create();

    mask = 128;
    for (row = 0; row <= height - 1; row++) {
        CkStringBuilderW_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 = CkBinDataW_GetByte(bd,(row * rowBytes) + (col / 8));
            if (((b / mask) % 2) == 1) {
                CkStringBuilderW_Append(sbRow,L"##");
            }
            else {
                CkStringBuilderW_Append(sbRow,L"  ");
            }

            mask = mask / 2;
        }

        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbRow));
    }

    //  The same symbol as the PNG that the call also returned:

    //  image

    //  Sample output:

    //  kind: bitMatrix
    //  modules: 21 x 21, 3 bytes per row
    //  ##############              ##############
    //  ##          ##      ####    ##          ##
    //  ##  ######  ##  ##  ####    ##  ######  ##
    //  ##  ######  ##  ##          ##  ######  ##
    //  ##  ######  ##  ##  ##  ##  ##  ######  ##
    //  ##          ##  ####  ##    ##          ##
    //  ##############  ##  ##  ##  ##############
    //                  ####
    //  ##  ##########      ####    ##########
    //      ####          ##  ########    ######
    //  ####  ########    ####  ##  ####      ####
    //        ##  ##      ############    ####  ##
    //    ######  ####  ######  ##    ##  ##
    //                  ##  ##  ##    ##  ##    ##
    //  ##############        ##  ##    ##      ##
    //  ##          ##  ##  ##        ######  ####
    //  ##  ######  ##  ####  ##  ##    ##
    //  ##  ######  ##  ##  ############    ##
    //  ##  ######  ##  ##      ##  ####  ##
    //  ##          ##    ##  ########          ##
    //  ##############  ##      ##      ######


    CkJsonObjectW_Dispose(json);
    CkFormatsW_Dispose(fmt);
    CkJsonObjectW_Dispose(jsonOut);
    CkBinDataW_Dispose(bd);
    CkStringBuilderW_Dispose(sbRow);

    }