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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Fmt
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Levels
long i
oleobject loo_Json2

li_Success = 0

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

loo_Fmt = create oleobject
li_rc = loo_Fmt.ConnectToNewObject("Chilkat.Formats")
if li_rc < 0 then
    destroy loo_Fmt
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp")

//  Encode the same text at each level.
loo_Levels = create oleobject
li_rc = loo_Levels.ConnectToNewObject("Chilkat.JsonArray")

loo_Levels.Load("[~"L~",~"M~",~"Q~",~"H~"]")

for i = 0 to 3
    loo_Json.UpdateString("encoding.qr.errorCorrection",loo_Levels.StringAt(i))
    li_Success = loo_Fmt.QrCode("generate",loo_Json,loo_JsonOut)
    if li_Success = 0 then
        Write-Debug loo_Fmt.LastErrorText
        destroy loo_Fmt
        destroy loo_Json
        destroy loo_JsonOut
        destroy loo_Levels
        return
    end if

    Write-Debug "level " + loo_Levels.StringAt(i) + ": version " + string(loo_JsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", " + string(loo_JsonOut.IntOf("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.
loo_Json2 = create oleobject
li_rc = loo_Json2.ConnectToNewObject("Chilkat.JsonObject")

loo_Json2.UpdateString("payload.text","https://chilkatsoft.com")
loo_Json2.UpdateString("encoding.qr.errorCorrection","L")
li_Success = loo_Fmt.QrCode("generate",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Fmt.LastErrorText
    destroy loo_Fmt
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Levels
    destroy loo_Json2
    return
end if

Write-Debug "level L requested: version " + string(loo_JsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", level " + loo_JsonOut.StringOf("symbols[0].selectedEncoding.errorCorrection")

loo_Json2.UpdateBool("encoding.qr.boostErrorCorrection",1)
li_Success = loo_Fmt.QrCode("generate",loo_Json2,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Fmt.LastErrorText
    destroy loo_Fmt
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Levels
    destroy loo_Json2
    return
end if

Write-Debug "level L with boost: version " + string(loo_JsonOut.IntOf("symbols[0].selectedEncoding.version")) + ", level " + loo_JsonOut.StringOf("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


destroy loo_Fmt
destroy loo_Json
destroy loo_JsonOut
destroy loo_Levels
destroy loo_Json2