Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oFmt
LOCAL oJsonOut
LOCAL nWidth
LOCAL nHeight
LOCAL nRowBytes
LOCAL oBd
LOCAL oSbRow
LOCAL nRow
LOCAL nCol
LOCAL b
LOCAL nMask

nSuccess := 0

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("payload.text", "CHILKAT")
oJson:UpdateBool("output.includeModuleData", 1)

oFmt := CreateObject("Chilkat.Formats")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oFmt:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

nWidth := oJsonOut:IntOf("symbols[0].moduleData.width")
nHeight := oJsonOut:IntOf("symbols[0].moduleData.height")
nRowBytes := oJsonOut:IntOf("symbols[0].moduleData.rowBytes")
? "kind: " + oJsonOut:StringOf("symbols[0].moduleData.kind")
? "modules: " + Str(nWidth) + " x " + Str(nHeight) + ", " + Str(nRowBytes) + " bytes per row"

//  The matrix is height rows of rowBytes bytes.
oBd := CreateObject("Chilkat.BinData")
nSuccess := oBd:AppendEncoded(oJsonOut:StringOf("symbols[0].moduleData.bytesBase64"), "base64")
IF (nSuccess == 0)
    ? oBd:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    oBd:destroy()
    RETURN
ENDIF

oSbRow := CreateObject("Chilkat.StringBuilder")

nMask := 128
FOR nRow := 0 TO nHeight - 1
    oSbRow:Clear()
    FOR nCol := 0 TO nWidth - 1
        //  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 ((nCol % 8) == 0)
            nMask := 128
        ENDIF

        b := oBd:GetByte((nRow * nRowBytes) + (nCol / 8))
        IF (((b / nMask) % 2) == 1)
            oSbRow:Append("##")
        ELSE
            oSbRow:Append("  ")
        ENDIF

        nMask := nMask / 2
    NEXT
    ? oSbRow:GetAsString()
NEXT

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

//  image

//  Sample output:

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

oJson:destroy()
oFmt:destroy()
oJsonOut:destroy()
oBd:destroy()
oSbRow:destroy()