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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Json
oleobject loo_Fmt
oleobject loo_JsonOut

li_Success = 0

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

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_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.
loo_Json.UpdateInt("render.moduleSize",10)
loo_Json.UpdateString("render.sizing","shrinkToFit")
loo_Json.UpdateInt("render.minModuleSize",3)
loo_Json.UpdateInt("render.maxWidth",300)
loo_Json.UpdateInt("render.maxHeight",300)

loo_Json.UpdateString("output.path","qa_output/qr_fit_300.png")
loo_Json.UpdateBool("output.overwrite",1)
loo_Json.UpdateString("output.return","none")

loo_Fmt = create oleobject
li_rc = loo_Fmt.ConnectToNewObject("Chilkat.Formats")

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Fmt.QrCode("generate",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Fmt.LastErrorText
    destroy loo_Json
    destroy loo_Fmt
    destroy loo_JsonOut
    return
end if

Write-Debug "module asked for: " + loo_JsonOut.StringOf("measurements[0].requestedPitch") + " pixels"
Write-Debug "module used: " + string(loo_JsonOut.IntOf("measurements[0].pitch")) + " pixels"
Write-Debug "image: " + string(loo_JsonOut.IntOf("measurements[0].canvasWidth")) + " x " + string(loo_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.
loo_Json.UpdateInt("render.maxWidth",100)
loo_Json.UpdateInt("render.maxHeight",100)
li_Success = loo_Fmt.QrCode("generate",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug "100 x 100: " + loo_JsonOut.StringOf("errors[0].code") + " (" + loo_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)


destroy loo_Json
destroy loo_Fmt
destroy loo_JsonOut