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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Handle hoFmt
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1
    String sTemp2
    Integer iTemp1
    Integer iTemp2

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "payload.text" "https://www.chilkatsoft.com/barcode_overview.asp" To iSuccess

    //  Ask for 10-pixel modules, but accept anything down to 3 pixels to stay within 300 x 300.
    Get ComUpdateInt Of hoJson "render.moduleSize" 10 To iSuccess
    Get ComUpdateString Of hoJson "render.sizing" "shrinkToFit" To iSuccess
    Get ComUpdateInt Of hoJson "render.minModuleSize" 3 To iSuccess
    Get ComUpdateInt Of hoJson "render.maxWidth" 300 To iSuccess
    Get ComUpdateInt Of hoJson "render.maxHeight" 300 To iSuccess

    Get ComUpdateString Of hoJson "output.path" "qa_output/qr_fit_300.png" To iSuccess
    Get ComUpdateBool Of hoJson "output.overwrite" True To iSuccess
    Get ComUpdateString Of hoJson "output.return" "none" To iSuccess

    Get Create (RefClass(cComChilkatFormats)) To hoFmt
    If (Not(IsComObjectCreated(hoFmt))) Begin
        Send CreateComObject of hoFmt
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComQrCode Of hoFmt "generate" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFmt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "measurements[0].requestedPitch" To sTemp1
    Showln "module asked for: " sTemp1 " pixels"
    Get ComIntOf Of hoJsonOut "measurements[0].pitch" To iTemp1
    Showln "module used: " iTemp1 " pixels"
    Get ComIntOf Of hoJsonOut "measurements[0].canvasWidth" To iTemp1
    Get ComIntOf Of hoJsonOut "measurements[0].canvasHeight" To iTemp2
    Showln "image: " iTemp1 " x " iTemp2 " 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.
    Get ComUpdateInt Of hoJson "render.maxWidth" 100 To iSuccess
    Get ComUpdateInt Of hoJson "render.maxHeight" 100 To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComQrCode Of hoFmt "generate" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComStringOf Of hoJsonOut "errors[0].code" To sTemp1
        Get ComStringOf Of hoJsonOut "errors[0].message" To sTemp2
        Showln "100 x 100: " sTemp1 " (" sTemp2 ")"
    End

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


End_Procedure