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

QR Code with Non-English Text (UTF-8 and ECI)

See more QR Code Examples

Demonstrates text outside ASCII. QrCode always stores text as UTF-8 bytes, which is what phones expect. A QR code can also say so explicitly with an ECI header: setting encoding.eci to 26 declares the bytes to be UTF-8, for readers that would otherwise assume another character set. The header costs a little space, and a few older readers do not understand it, so it is off by default.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.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.

    ;  The text is "Gruesse aus Muenchen" written with u-umlaut and sharp s.  It is given here with
    ;  JSON escapes only so that this source is plain ASCII.  Decode turns the escapes into characters.
    sbText.i = CkStringBuilder::ckCreate()
    If sbText.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbText,"Gr\u00fc\u00dfe aus M\u00fcnchen")
    CkStringBuilder::ckDecode(sbText,"json","utf-8")

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

    CkJsonObject::ckUpdateSb(json,"payload.text",sbText)
    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_utf8.png")
    CkJsonObject::ckUpdateBool(json,"output.overwrite",1)
    CkJsonObject::ckUpdateString(json,"output.return","none")

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

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

    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkStringBuilder::ckDispose(sbText)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  17 characters, three of which take two bytes in UTF-8.
    Debug "characters: " + Str(CkStringBuilder::ckLength(sbText))
    Debug "bytes encoded: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].payloadBytes"))
    Debug "ECI header: " + Str(CkJsonObject::ckHasMember(jsonOut,"symbols[0].selectedEncoding.eci"))

    ;  Without an ECI header (qa_output/qr_utf8.png):

    ;  image

    ;  The same text with an ECI header declaring UTF-8.
    CkJsonObject::ckUpdateInt(json,"encoding.eci",26)
    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_utf8_eci.png")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkStringBuilder::ckDispose(sbText)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "with encoding.eci = 26, ECI header: " + Str(CkJsonObject::ckHasMember(jsonOut,"symbols[0].selectedEncoding.eci")) + ", value " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.eci"))

    ;  With the ECI header (qa_output/qr_utf8_eci.png).  Both scan as the same text:

    ;  image

    ;  Sample output:

    ;  characters: 17
    ;  bytes encoded: 20
    ;  ECI header: False
    ;  with encoding.eci = 26, ECI header: True, value 26


    CkStringBuilder::ckDispose(sbText)
    CkJsonObject::ckDispose(json)
    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure