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

Tcl

load ./chilkat.dll

set success 0

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

set fmt [new_CkFormats]

set jsonOut [new_CkJsonObject]

#  Dark blue modules on a pale yellow background.
set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "payload.text" "https://www.chilkatsoft.com/"
CkJsonObject_UpdateInt $json "render.moduleSize" 6
CkJsonObject_UpdateString $json "render.foreground" "#1B3A6B"
CkJsonObject_UpdateString $json "render.background" "#FFF8DC"
CkJsonObject_UpdateString $json "output.path" "qa_output/qr_blue.png"
CkJsonObject_UpdateBool $json "output.overwrite" 1
CkJsonObject_UpdateString $json "output.return" "none"
set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json
    exit
}

puts "colored: [CkJsonObject_stringOf $jsonOut {outputs[0].path}], [CkJsonObject_IntOf $jsonOut {outputs[0].byteCount}] bytes"

#  The image written to qa_output/qr_blue.png:

#  image

#  The same modules on a fully transparent background.
set json2 [new_CkJsonObject]

CkJsonObject_UpdateString $json2 "payload.text" "https://www.chilkatsoft.com/"
CkJsonObject_UpdateInt $json2 "render.moduleSize" 6
CkJsonObject_UpdateString $json2 "render.foreground" "#1B3A6B"
CkJsonObject_UpdateString $json2 "render.background.color" "#FFFFFF"
CkJsonObject_UpdateNumber $json2 "render.background.opacity" "0"
CkJsonObject_UpdateString $json2 "output.path" "qa_output/qr_transparent.png"
CkJsonObject_UpdateBool $json2 "output.overwrite" 1
CkJsonObject_UpdateString $json2 "output.return" "none"
set success [CkFormats_QrCode $fmt "generate" $json2 $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json
    delete_CkJsonObject $json2
    exit
}

puts "transparent: [CkJsonObject_stringOf $jsonOut {outputs[0].path}], [CkJsonObject_IntOf $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

delete_CkFormats $fmt
delete_CkJsonObject $jsonOut
delete_CkJsonObject $json
delete_CkJsonObject $json2