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

QR Code Error Correction Levels

See more QR Code Examples

Demonstrates the four QR error-correction levels. L, M, Q and H allow a reader to recover the data when about 7%, 15%, 25% and 30% of the symbol is damaged or covered. More error correction leaves less room for data, so the same text needs a larger symbol at a higher level. The default is M. The example also shows boostErrorCorrection, which raises the level for free when the chosen version has room to spare.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonArray.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFormats.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/barcode_overview.asp")

    ;  Encode the same text at each level.
    levels.i = CkJsonArray::ckCreate()
    If levels.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckLoad(levels,"[" + Chr(34) + "L" + Chr(34) + "," + Chr(34) + "M" + Chr(34) + "," + Chr(34) + "Q" + Chr(34) + "," + Chr(34) + "H" + Chr(34) + "]")
    i.i
    For i = 0 To 3
        CkJsonObject::ckUpdateString(json,"encoding.qr.errorCorrection",CkJsonArray::ckStringAt(levels,i))
        success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
        If success = 0
            Debug CkFormats::ckLastErrorText(fmt)
            CkFormats::ckDispose(fmt)
            CkJsonObject::ckDispose(json)
            CkJsonObject::ckDispose(jsonOut)
            CkJsonArray::ckDispose(levels)
            ProcedureReturn
        EndIf

        Debug "level " + CkJsonArray::ckStringAt(levels,i) + ": version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].dimensions.modulesWide")) + " modules wide"
    Next

    ;  The four symbols.  They hold the same text, and the higher the level, the larger the symbol:

    ;  image

    ;  A symbol often has room left over.  boostErrorCorrection uses it: the level is raised
    ;  as far as the same version allows, so the symbol is no larger but is more robust.
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json2,"payload.text","https://chilkatsoft.com")
    CkJsonObject::ckUpdateString(json2,"encoding.qr.errorCorrection","L")
    success = CkFormats::ckQrCode(fmt,"generate",json2,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonArray::ckDispose(levels)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "level L requested: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", level " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.errorCorrection")

    CkJsonObject::ckUpdateBool(json2,"encoding.qr.boostErrorCorrection",1)
    success = CkFormats::ckQrCode(fmt,"generate",json2,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonArray::ckDispose(levels)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "level L with boost: version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version")) + ", level " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.errorCorrection")

    ;  Level L, and level M reached by the boost.  The two symbols are the same size:

    ;  image

    ;  Sample output:

    ;  level L: version 3, 29 modules wide
    ;  level M: version 4, 33 modules wide
    ;  level Q: version 5, 37 modules wide
    ;  level H: version 6, 41 modules wide
    ;  level L requested: version 2, level L
    ;  level L with boost: version 2, level M


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


    ProcedureReturn
EndProcedure