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

Tcl

load ./chilkat.dll

set success 0

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

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "payload.text" "CHILKAT"
CkJsonObject_UpdateBool $json "output.includeModuleData" 1

set fmt [new_CkFormats]

set jsonOut [new_CkJsonObject]

set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    exit
}

set width [CkJsonObject_IntOf $jsonOut "symbols[0].moduleData.width"]
set height [CkJsonObject_IntOf $jsonOut "symbols[0].moduleData.height"]
set rowBytes [CkJsonObject_IntOf $jsonOut "symbols[0].moduleData.rowBytes"]
puts "kind: [CkJsonObject_stringOf $jsonOut {symbols[0].moduleData.kind}]"
puts "modules: $width x $height, $rowBytes bytes per row"

#  The matrix is height rows of rowBytes bytes.
set bd [new_CkBinData]

set success [CkBinData_AppendEncoded $bd [CkJsonObject_stringOf $jsonOut "symbols[0].moduleData.bytesBase64"] "base64"]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bd]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkBinData $bd
    exit
}

set sbRow [new_CkStringBuilder]

set mask 128
for {set row 0} {$row <= [expr $height - 1]} {incr row} {
    CkStringBuilder_Clear $sbRow
    for {set col 0} {$col <= [expr $width - 1]} {incr 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 {[expr $col % 8] == 0} then {
            set mask 128
        }

        set b [CkBinData_GetByte $bd [expr [expr $row * $rowBytes] + [expr $col / 8]]]
        if {[expr [expr $b / $mask] % 2] == 1} then {
            CkStringBuilder_Append $sbRow "##"
        }         else {
            CkStringBuilder_Append $sbRow "  "
        }

        set mask [expr $mask / 2]
    }
    puts [CkStringBuilder_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
#  ##############              ##############
#  ##          ##      ####    ##          ##
#  ##  ######  ##  ##  ####    ##  ######  ##
#  ##  ######  ##  ##          ##  ######  ##
#  ##  ######  ##  ##  ##  ##  ##  ######  ##
#  ##          ##  ####  ##    ##          ##
#  ##############  ##  ##  ##  ##############
#                  ####
#  ##  ##########      ####    ##########
#      ####          ##  ########    ######
#  ####  ########    ####  ##  ####      ####
#        ##  ##      ############    ####  ##
#    ######  ####  ######  ##    ##  ##
#                  ##  ##  ##    ##  ##    ##
#  ##############        ##  ##    ##      ##
#  ##          ##  ##  ##        ######  ####
#  ##  ######  ##  ####  ##  ##    ##
#  ##  ######  ##  ##  ############    ##
#  ##  ######  ##  ##      ##  ####  ##
#  ##          ##    ##  ########          ##
#  ##############  ##      ##      ######

delete_CkJsonObject $json
delete_CkFormats $fmt
delete_CkJsonObject $jsonOut
delete_CkBinData $bd
delete_CkStringBuilder $sbRow