Sample code for 30+ languages & platforms
Tcl 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 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 fmt [new_CkFormats]

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

CkJsonObject_UpdateString $json "payload.text" "https://www.chilkatsoft.com/barcode_overview.asp"

#  Encode the same text at each level.
set levels [new_CkJsonArray]

CkJsonArray_Load $levels "[\"L\",\"M\",\"Q\",\"H\"]"

for {set i 0} {$i <= 3} {incr i} {
    CkJsonObject_UpdateString $json "encoding.qr.errorCorrection" [CkJsonArray_stringAt $levels $i]
    set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
    if {$success == 0} then {
        puts [CkFormats_lastErrorText $fmt]
        delete_CkFormats $fmt
        delete_CkJsonObject $json
        delete_CkJsonObject $jsonOut
        delete_CkJsonArray $levels
        exit
    }

    puts "level [CkJsonArray_stringAt $levels $i]: version [CkJsonObject_IntOf $jsonOut {symbols[0].selectedEncoding.version}], [CkJsonObject_IntOf $jsonOut {symbols[0].dimensions.modulesWide}] modules wide"
}

#  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.
set json2 [new_CkJsonObject]

CkJsonObject_UpdateString $json2 "payload.text" "https://chilkatsoft.com"
CkJsonObject_UpdateString $json2 "encoding.qr.errorCorrection" "L"
set success [CkFormats_QrCode $fmt "generate" $json2 $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkFormats $fmt
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonArray $levels
    delete_CkJsonObject $json2
    exit
}

puts "level L requested: version [CkJsonObject_IntOf $jsonOut {symbols[0].selectedEncoding.version}], level [CkJsonObject_stringOf $jsonOut {symbols[0].selectedEncoding.errorCorrection}]"

CkJsonObject_UpdateBool $json2 "encoding.qr.boostErrorCorrection" 1
set success [CkFormats_QrCode $fmt "generate" $json2 $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkFormats $fmt
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonArray $levels
    delete_CkJsonObject $json2
    exit
}

puts "level L with boost: version [CkJsonObject_IntOf $jsonOut {symbols[0].selectedEncoding.version}], level [CkJsonObject_stringOf $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

delete_CkFormats $fmt
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkJsonArray $levels
delete_CkJsonObject $json2