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

Fit a QR Code within a Maximum Size

See more QR Code Examples

Demonstrates limiting the size of the image. render.maxWidth and render.maxHeight are the largest image allowed. With render.sizing set to shrinkToFit, QrCode starts from the module size asked for and reduces it, a whole pixel at a time, until the image fits, but never below render.minModuleSize. If it cannot fit, the call fails with noFit rather than returning a code too small to scan.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

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

Dim json As New Chilkat.JsonObject
success = json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp")

//  Ask for 10-pixel modules, but accept anything down to 3 pixels to stay within 300 x 300.
success = json.UpdateInt("render.moduleSize",10)
success = json.UpdateString("render.sizing","shrinkToFit")
success = json.UpdateInt("render.minModuleSize",3)
success = json.UpdateInt("render.maxWidth",300)
success = json.UpdateInt("render.maxHeight",300)

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

Dim fmt As New Chilkat.Formats
Dim jsonOut As New Chilkat.JsonObject
success = fmt.QrCode("generate",json,jsonOut)
If (success = False) Then
    System.DebugLog(fmt.LastErrorText)
    Return
End If

System.DebugLog("module asked for: " + jsonOut.StringOf("measurements[0].requestedPitch") + " pixels")
System.DebugLog("module used: " + Str(jsonOut.IntOf("measurements[0].pitch")) + " pixels")
System.DebugLog("image: " + Str(jsonOut.IntOf("measurements[0].canvasWidth")) + " x " + Str(jsonOut.IntOf("measurements[0].canvasHeight")) + " pixels")

//  The image written to qa_output/qr_fit_300.png:

//  image

//  The same code cannot fit in 100 x 100 with modules of at least 3 pixels.
success = json.UpdateInt("render.maxWidth",100)
success = json.UpdateInt("render.maxHeight",100)
success = fmt.QrCode("generate",json,jsonOut)
If (success = False) Then
    System.DebugLog("100 x 100: " + jsonOut.StringOf("errors[0].code") + " (" + jsonOut.StringOf("errors[0].message") + ")")
End If

//  Sample output:

//  module asked for: 10 pixels
//  module used: 7 pixels
//  image: 287 x 287 pixels
//  100 x 100: noFit (the symbol does not fit the render constraints: minModuleSize)