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

QR Code with Binary Data

See more QR Code Examples

Demonstrates a QR code that holds arbitrary bytes rather than text. The payload type is binary and the bytes are given as base64 in payload.base64. Any byte value may be used, including zero. Keep in mind that many phone camera apps expect text, so binary payloads are for readers that you control.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.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 bytes to encode.  Here they come from hex, but they could be loaded from a file with LoadFile.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckAppendEncoded(bd,"000102030405FF00FE4368696C6B6174","hex")
    If success = 0
        Debug CkBinData::ckLastErrorText(bd)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckUpdateString(json,"payload.type","binary")
    CkJsonObject::ckUpdateString(json,"payload.base64",CkBinData::ckGetEncoded(bd,"base64"))
    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_binary.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)
        CkBinData::ckDispose(bd)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "bytes given: " + Str(CkBinData::ckNumBytes(bd))
    Debug "bytes encoded: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].payloadBytes"))
    Debug "mode: " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].selectedEncoding.mode")
    Debug "QR version: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))

    ;  The image written to qa_output/qr_binary.png:

    ;  image

    ;  Sample output:

    ;  bytes given: 16
    ;  bytes encoded: 16
    ;  mode: byte
    ;  QR version: 2


    CkBinData::ckDispose(bd)
    CkJsonObject::ckDispose(json)
    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure