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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

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

set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("payload.text","https://www.chilkatsoft.com/")

'  Modules of 0.5 mm, for a 300 dpi printer.
success = json.UpdateString("render.units","mm")
success = json.UpdateNumber("render.moduleSize","0.5")
success = json.UpdateInt("render.dpi",300)

success = json.UpdateString("output.path","qa_output/qr_300dpi.png")
success = json.UpdateBool("output.overwrite",1)
success = json.UpdateString("output.return","none")

set fmt = Server.CreateObject("Chilkat.Formats")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = fmt.QrCode("generate",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( fmt.LastErrorText) & "</pre>"
    Response.End
End If

'  0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.
Response.Write "<pre>" & Server.HTMLEncode( "requested module: " & jsonOut.StringOf("measurements[0].requestedPitch") & " pixels") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "module drawn: " & jsonOut.IntOf("measurements[0].pitch") & " pixels = " & jsonOut.StringOf("measurements[0].moduleSize") _
     & " mm") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "size was rounded: " & jsonOut.BoolOf("measurements[0].pitchAdjusted")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "image: " & jsonOut.IntOf("measurements[0].canvasWidth") & " x " & jsonOut.IntOf("measurements[0].canvasHeight") & " pixels") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "printed size: " & jsonOut.StringOf("measurements[0].physicalWidth") & " x " & jsonOut.StringOf("measurements[0].physicalHeight") _
     & " mm") & "</pre>"

'  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.
success = json.UpdateBool("render.requireExactPitch",1)
success = fmt.QrCode("generate",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "requireExactPitch: " & jsonOut.StringOf("errors[0].code") & " (" & jsonOut.StringOf("errors[0].message") & ")") & "</pre>"
End If

'  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)

%>
</body>
</html>