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

QR Code Colors and Transparent Background

See more QR Code Examples

Demonstrates colored QR codes. render.foreground is the color of the dark modules and render.background the color behind them, each as #RRGGBB. Either can instead be an object with a color and an opacity from 0 to 1, and a background opacity of 0 gives a PNG with a transparent background to place over a page or a design. Readers need contrast: keep the modules clearly darker than whatever ends up behind them.

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.

    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

    ;  Dark blue modules on a pale yellow background.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateInt(json,"render.moduleSize",6)
    CkJsonObject::ckUpdateString(json,"render.foreground","#1B3A6B")
    CkJsonObject::ckUpdateString(json,"render.background","#FFF8DC")
    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_blue.png")
    CkJsonObject::ckUpdateBool(json,"output.overwrite",1)
    CkJsonObject::ckUpdateString(json,"output.return","none")
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug "colored: " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].path") + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].byteCount")) + " bytes"

    ;  The image written to qa_output/qr_blue.png:

    ;  image

    ;  The same modules on a fully transparent background.
    json2.i = CkJsonObject::ckCreate()
    If json2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json2,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateInt(json2,"render.moduleSize",6)
    CkJsonObject::ckUpdateString(json2,"render.foreground","#1B3A6B")
    CkJsonObject::ckUpdateString(json2,"render.background.color","#FFFFFF")
    CkJsonObject::ckUpdateNumber(json2,"render.background.opacity","0")
    CkJsonObject::ckUpdateString(json2,"output.path","qa_output/qr_transparent.png")
    CkJsonObject::ckUpdateBool(json2,"output.overwrite",1)
    CkJsonObject::ckUpdateString(json2,"output.return","none")
    success = CkFormats::ckQrCode(fmt,"generate",json2,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        ProcedureReturn
    EndIf

    Debug "transparent: " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].path") + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].byteCount")) + " bytes"

    ;  qa_output/qr_transparent.png, placed here over a checkerboard and over a colored page
    ;  to show that whatever is behind the image shows through:

    ;  image

    ;  Sample output:

    ;  colored: qa_output/qr_blue.png, 1345 bytes
    ;  transparent: qa_output/qr_transparent.png, 1377 bytes


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


    ProcedureReturn
EndProcedure