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

PureBasic
IncludeFile "CkFormats.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"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.
    CkJsonObject::ckUpdateInt(json,"render.moduleSize",10)
    CkJsonObject::ckUpdateString(json,"render.sizing","shrinkToFit")
    CkJsonObject::ckUpdateInt(json,"render.minModuleSize",3)
    CkJsonObject::ckUpdateInt(json,"render.maxWidth",300)
    CkJsonObject::ckUpdateInt(json,"render.maxHeight",300)

    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_fit_300.png")
    CkJsonObject::ckUpdateBool(json,"output.overwrite",1)
    CkJsonObject::ckUpdateString(json,"output.return","none")

    fmt.i = CkFormats::ckCreate()
    If fmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "module asked for: " + CkJsonObject::ckStringOf(jsonOut,"measurements[0].requestedPitch") + " pixels"
    Debug "module used: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].pitch")) + " pixels"
    Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasWidth")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"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.
    CkJsonObject::ckUpdateInt(json,"render.maxWidth",100)
    CkJsonObject::ckUpdateInt(json,"render.maxHeight",100)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug "100 x 100: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].code") + " (" + CkJsonObject::ckStringOf(jsonOut,"errors[0].message") + ")"
    EndIf

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


    CkJsonObject::ckDispose(json)
    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure