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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Handle hoFmt
    Variant vJsonOut
    Handle hoJsonOut
    Integer iWidth
    Integer iHeight
    Integer iRowBytes
    Handle hoBd
    Handle hoSbRow
    Integer iRow
    Integer iCol
    Integer b
    Integer iMask
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "payload.text" "CHILKAT" To iSuccess
    Get ComUpdateBool Of hoJson "output.includeModuleData" True To iSuccess

    Get Create (RefClass(cComChilkatFormats)) To hoFmt
    If (Not(IsComObjectCreated(hoFmt))) Begin
        Send CreateComObject of hoFmt
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComQrCode Of hoFmt "generate" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFmt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "symbols[0].moduleData.width" To iWidth
    Get ComIntOf Of hoJsonOut "symbols[0].moduleData.height" To iHeight
    Get ComIntOf Of hoJsonOut "symbols[0].moduleData.rowBytes" To iRowBytes
    Get ComStringOf Of hoJsonOut "symbols[0].moduleData.kind" To sTemp1
    Showln "kind: " sTemp1
    Showln "modules: " iWidth " x " iHeight ", " iRowBytes " bytes per row"

    //  The matrix is height rows of rowBytes bytes.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComStringOf Of hoJsonOut "symbols[0].moduleData.bytesBase64" To sTemp1
    Get ComAppendEncoded Of hoBd sTemp1 "base64" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBd To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRow
    If (Not(IsComObjectCreated(hoSbRow))) Begin
        Send CreateComObject of hoSbRow
    End

    Move 128 To iMask
    For iRow From 0 To (iHeight - 1)
        Send ComClear To hoSbRow
        For iCol From 0 To (iWidth - 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 ((Mod(iCol,8)) = 0) Begin
                Move 128 To iMask
            End

            Get ComGetByte Of hoBd ((iRow * iRowBytes) + (iCol / 8)) To b
            If ((Mod((b / iMask),2)) = 1) Begin
                Get ComAppend Of hoSbRow "##" To iSuccess
            End
            Else Begin
                Get ComAppend Of hoSbRow "  " To iSuccess
            End

            Move (iMask / 2) To iMask
        Loop

        Get ComGetAsString Of hoSbRow To sTemp1
        Showln sTemp1
    Loop

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

    //  image

    //  Sample output:

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


End_Procedure