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

QR Code with a Text Caption

See more QR Code Examples

Demonstrates printing a line of text under a QR code, such as the order number or the address the code holds, so that a person can read it too. render.text turns the caption on and gives its value, which is printable ASCII and is drawn in a built-in font that needs no font files. The caption can go above or below the symbol, be aligned left, center or right, and be scaled.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFormats.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/order/12345")
    CkJsonObject::ckUpdateInt(json,"render.moduleSize",6)

    ;  The caption.  It need not be the same as the text in the QR code.
    CkJsonObject::ckUpdateBool(json,"render.text.enabled",1)
    CkJsonObject::ckUpdateString(json,"render.text.value","ORDER 12345")
    ;  position is "below" (the default) or "above".  alignment is "center" (the default), "left" or "right".
    CkJsonObject::ckUpdateString(json,"render.text.position","below")
    ;  Each dot of the caption's font is drawn this many pixels wide.
    CkJsonObject::ckUpdateInt(json,"render.text.scale",3)

    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_caption.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)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "caption: " + CkJsonObject::ckStringOf(jsonOut,"measurements[0].text")
    Debug "caption scale: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].textScale"))
    Debug "height added for the caption: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].textBandPx")) + " pixels"
    Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasWidth")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasHeight")) + " pixels"

    ;  The image written to qa_output/qr_caption.png:

    ;  image

    ;  Sample output:

    ;  caption: ORDER 12345
    ;  caption scale: 3
    ;  height added for the caption: 21 pixels
    ;  image: 222 x 243 pixels


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


    ProcedureReturn
EndProcedure