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 <CkJsonObjectW.h>
#include <CkFormatsW.h>
#include <CkBinDataW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    CkJsonObjectW json;
    json.UpdateString(L"payload.text",L"CHILKAT");
    json.UpdateBool(L"output.includeModuleData",true);

    CkFormatsW fmt;
    CkJsonObjectW jsonOut;
    success = fmt.QrCode(L"generate",json,jsonOut);
    if (success == false) {
        wprintf(L"%s\n",fmt.lastErrorText());
        return;
    }

    int width = jsonOut.IntOf(L"symbols[0].moduleData.width");
    int height = jsonOut.IntOf(L"symbols[0].moduleData.height");
    int rowBytes = jsonOut.IntOf(L"symbols[0].moduleData.rowBytes");
    wprintf(L"kind: %s\n",jsonOut.stringOf(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.
    CkBinDataW bd;
    success = bd.AppendEncoded(jsonOut.stringOf(L"symbols[0].moduleData.bytesBase64"),L"base64");
    if (success == false) {
        wprintf(L"%s\n",bd.lastErrorText());
        return;
    }

    CkStringBuilderW sbRow;
    int row;
    int col;
    int b;
    int mask = 128;
    for (row = 0; row <= height - 1; row++) {
        sbRow.Clear();
        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 = bd.GetByte((row * rowBytes) + (col / 8));
            if (((b / mask) % 2) == 1) {
                sbRow.Append(L"##");
            }
            else {
                sbRow.Append(L"  ");
            }

            mask = mask / 2;
        }

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

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

    //  image

    //  Sample output:

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