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

    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/" To iSuccess

    //  Modules of 0.5 mm, for a 300 dpi printer.
    Get ComUpdateString Of hoJson "render.units" "mm" To iSuccess
    Get ComUpdateNumber Of hoJson "render.moduleSize" "0.5" To iSuccess
    Get ComUpdateInt Of hoJson "render.dpi" 300 To iSuccess

    Get ComUpdateString Of hoJson "output.path" "qa_output/qr_300dpi.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

    //  0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.
    Get ComStringOf Of hoJsonOut "measurements[0].requestedPitch" To sTemp1
    Showln "requested module: " sTemp1 " pixels"
    Get ComIntOf Of hoJsonOut "measurements[0].pitch" To iTemp1
    Get ComStringOf Of hoJsonOut "measurements[0].moduleSize" To sTemp1
    Showln "module drawn: " iTemp1 " pixels = " sTemp1 " mm"
    Get ComBoolOf Of hoJsonOut "measurements[0].pitchAdjusted" To bTemp1
    Showln "size was rounded: " bTemp1
    Get ComIntOf Of hoJsonOut "measurements[0].canvasWidth" To iTemp1
    Get ComIntOf Of hoJsonOut "measurements[0].canvasHeight" To iTemp2
    Showln "image: " iTemp1 " x " iTemp2 " pixels"
    Get ComStringOf Of hoJsonOut "measurements[0].physicalWidth" To sTemp1
    Get ComStringOf Of hoJsonOut "measurements[0].physicalHeight" To sTemp2
    Showln "printed size: " sTemp1 " x " sTemp2 " mm"

    //  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.
    Get ComUpdateBool Of hoJson "render.requireExactPitch" True 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 "requireExactPitch: " sTemp1 " (" sTemp2 ")"
    End

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


End_Procedure