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

Choose the QR Code Version and Mask Pattern

See more QR Code Examples

Demonstrates fixing the QR version and the mask pattern. The version (1 to 40) is the size of the symbol: version 1 is 21 modules wide and each version adds 4. By default QrCode picks the smallest version that holds the data and the mask pattern that scores best under the QR standard. Fixing the version is useful when a series of codes must all be the same size, and minVersion and maxVersion bound the automatic choice instead.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFormats.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    fmt.i = CkFormats::ckCreate()
    If fmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/")

    ;  Automatic: the smallest version that holds the text, and the best-scoring mask.
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "automatic: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].dimensions.modulesWide")) + " modules wide, mask " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.mask"))

    ;  A fixed version.  The call fails with capacityExceeded if the text does not fit it.
    CkJsonObject::ckUpdateInt(json,"encoding.qr.version",8)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "version 8: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].dimensions.modulesWide")) + " modules wide, mask " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.mask"))

    ;  A fixed mask pattern, 0 to 7.  Every mask gives a valid symbol holding the same data.
    CkJsonObject::ckUpdateInt(json,"encoding.qr.mask",5)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "version 8, mask 5: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", mask " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.mask"))

    ;  Bounds for the automatic choice: no smaller than version 4, no larger than version 10.
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json2,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateInt(json2,"encoding.qr.minVersion",4)
    CkJsonObject::ckUpdateInt(json2,"encoding.qr.maxVersion",10)
    success = CkFormats::ckQrCode(fmt,"generate",json2,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "minVersion 4: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  The four symbols, in the order they were made.  The second and the third hold the same data
    ;  in the same version and differ only in the mask:

    ;  image

    ;  Sample output:

    ;  automatic: version 3, 29 modules wide, mask 2
    ;  version 8: version 8, 49 modules wide, mask 2
    ;  version 8, mask 5: version 8, mask 5
    ;  minVersion 4: version 4


    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkJsonObject::ckDispose(json2)


    ProcedureReturn
EndProcedure