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

QR Code with a Physical Size in Millimeters at a Printer DPI

See more QR Code Examples

Demonstrates sizing a QR code for print. With render.units set to mm (or in, or pt) the module size is a physical length, and render.dpi says how many pixels the printer or the image has per inch. A module is always a whole number of pixels, because modules of uneven width scan badly, so the size asked for is rounded to the nearest pixel and the response reports the size actually used. The PNG records the dpi, so software that honors it prints the image at that physical size.

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/"

#  Modules of 0.5 mm, for a 300 dpi printer.
CkJsonObject_UpdateString $json "render.units" "mm"
CkJsonObject_UpdateNumber $json "render.moduleSize" "0.5"
CkJsonObject_UpdateInt $json "render.dpi" 300

CkJsonObject_UpdateString $json "output.path" "qa_output/qr_300dpi.png"
CkJsonObject_UpdateBool $json "output.overwrite" 1
CkJsonObject_UpdateString $json "output.return" "none"

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
}

#  0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.
puts "requested module: [CkJsonObject_stringOf $jsonOut {measurements[0].requestedPitch}] pixels"
puts "module drawn: [CkJsonObject_IntOf $jsonOut {measurements[0].pitch}] pixels = [CkJsonObject_stringOf $jsonOut {measurements[0].moduleSize}] mm"
puts "size was rounded: [CkJsonObject_BoolOf $jsonOut {measurements[0].pitchAdjusted}]"
puts "image: [CkJsonObject_IntOf $jsonOut {measurements[0].canvasWidth}] x [CkJsonObject_IntOf $jsonOut {measurements[0].canvasHeight}] pixels"
puts "printed size: [CkJsonObject_stringOf $jsonOut {measurements[0].physicalWidth}] x [CkJsonObject_stringOf $jsonOut {measurements[0].physicalHeight}] mm"

#  The image.  A web page shows it pixel for pixel.  Printed at 300 dpi it is 18.8 mm wide:

#  image

#  When the exact size matters more than getting an image, requireExactPitch makes a size
#  that is not a whole number of pixels a failure (noFit) instead of rounding it.
CkJsonObject_UpdateBool $json "render.requireExactPitch" 1
set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
if {$success == 0} then {
    puts "requireExactPitch: [CkJsonObject_stringOf $jsonOut {errors[0].code}] ([CkJsonObject_stringOf $jsonOut {errors[0].message}])"
}

#  Sample output:

#  requested module: 5.905512 pixels
#  module drawn: 6 pixels = 0.508 mm
#  size was rounded: True
#  image: 222 x 222 pixels
#  printed size: 18.796 x 18.796 mm
#  requireExactPitch: noFit (the symbol does not fit the render constraints: pitchAdjusted)

delete_CkJsonObject $json
delete_CkFormats $fmt
delete_CkJsonObject $jsonOut