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

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

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 json [new_CkJsonObject]

CkJsonObject_UpdateString $json "payload.text" "https://www.chilkatsoft.com/"
CkJsonObject_UpdateInt $json "render.moduleSize" 6

set fmt [new_CkFormats]

set jsonOut [new_CkJsonObject]

set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    exit
}

puts "format: [CkJsonObject_stringOf $jsonOut {outputs[0].format}]"
puts "MIME type: [CkJsonObject_stringOf $jsonOut {outputs[0].mimeType}]"
puts "image: [CkJsonObject_IntOf $jsonOut {outputs[0].width}] x [CkJsonObject_IntOf $jsonOut {outputs[0].height}] pixels"

#  outputs[0].base64 is the PNG file, base64 encoded.
set bdPng [new_CkBinData]

set success [CkBinData_AppendEncoded $bdPng [CkJsonObject_stringOf $jsonOut "outputs[0].base64"] "base64"]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bdPng]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkBinData $bdPng
    exit
}

puts "PNG bytes: [CkBinData_get_NumBytes $bdPng]"

#  The BinData holds the image.  Here it is simply saved.
set success [CkBinData_WriteFile $bdPng "qa_output/qr_in_memory.png"]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bdPng]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkBinData $bdPng
    exit
}

#  The PNG held in bdPng:

#  image

#  Sample output:

#  format: png
#  MIME type: image/png
#  image: 222 x 222 pixels
#  PNG bytes: 877

delete_CkJsonObject $json
delete_CkFormats $fmt
delete_CkJsonObject $jsonOut
delete_CkBinData $bdPng