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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFmt
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vJson2
    Handle hoJson2
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Integer iTemp3

    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(cComChilkatFormats)) To hoFmt
    If (Not(IsComObjectCreated(hoFmt))) Begin
        Send CreateComObject of hoFmt
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get ComUpdateString Of hoJson "payload.text" "https://www.chilkatsoft.com/" To iSuccess

    //  Automatic: the smallest version that holds the text, and the best-scoring mask.
    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].selectedEncoding.version" To iTemp1
    Get ComIntOf Of hoJsonOut "symbols[0].dimensions.modulesWide" To iTemp2
    Get ComIntOf Of hoJsonOut "symbols[0].selectedEncoding.mask" To iTemp3
    Showln "automatic: version " iTemp1 ", " iTemp2 " modules wide, mask " iTemp3

    //  A fixed version.  The call fails with capacityExceeded if the text does not fit it.
    Get ComUpdateInt Of hoJson "encoding.qr.version" 8 To iSuccess
    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].selectedEncoding.version" To iTemp1
    Get ComIntOf Of hoJsonOut "symbols[0].dimensions.modulesWide" To iTemp2
    Get ComIntOf Of hoJsonOut "symbols[0].selectedEncoding.mask" To iTemp3
    Showln "version 8: version " iTemp1 ", " iTemp2 " modules wide, mask " iTemp3

    //  A fixed mask pattern, 0 to 7.  Every mask gives a valid symbol holding the same data.
    Get ComUpdateInt Of hoJson "encoding.qr.mask" 5 To iSuccess
    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].selectedEncoding.version" To iTemp1
    Get ComIntOf Of hoJsonOut "symbols[0].selectedEncoding.mask" To iTemp2
    Showln "version 8, mask 5: version " iTemp1 ", mask " iTemp2

    //  Bounds for the automatic choice: no smaller than version 4, no larger than version 10.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson2
    If (Not(IsComObjectCreated(hoJson2))) Begin
        Send CreateComObject of hoJson2
    End
    Get ComUpdateString Of hoJson2 "payload.text" "https://www.chilkatsoft.com/" To iSuccess
    Get ComUpdateInt Of hoJson2 "encoding.qr.minVersion" 4 To iSuccess
    Get ComUpdateInt Of hoJson2 "encoding.qr.maxVersion" 10 To iSuccess
    Get pvComObject of hoJson2 to vJson2
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComQrCode Of hoFmt "generate" vJson2 vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFmt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "symbols[0].selectedEncoding.version" To iTemp1
    Showln "minVersion 4: version " iTemp1

    //  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


End_Procedure