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

QR Code Quiet Zone, Padding and Rotation

See more QR Code Examples

Demonstrates the space around a QR code and its rotation. The quiet zone is the blank margin a reader needs around the symbol. It is measured in modules, the QR standard requires 4, and QrCode will make it larger but not smaller. Padding is extra space outside the quiet zone, measured in the render units (pixels by default). Rotation turns the whole image by 90, 180 or 270 degrees. A QR code reads the same at any rotation.

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/")
    CkJsonObject::ckUpdateInt(json,"render.moduleSize",6)

    ;  A quiet zone of 6 modules on every side instead of the standard 4.
    CkJsonObject::ckUpdateInt(json,"render.quietZone.left",6)
    CkJsonObject::ckUpdateInt(json,"render.quietZone.top",6)
    CkJsonObject::ckUpdateInt(json,"render.quietZone.right",6)
    CkJsonObject::ckUpdateInt(json,"render.quietZone.bottom",6)

    ;  10 more pixels of background around that.
    CkJsonObject::ckUpdateInt(json,"render.padding",10)

    ;  Turn the image a quarter turn clockwise.
    CkJsonObject::ckUpdateInt(json,"render.rotation",90)

    CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_margins.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: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].pitch")) + " pixels"
    Debug "symbol: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolWidth")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolHeight")) + " pixels at (" + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolX")) + "," + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolY")) + ")"
    Debug "quiet zone: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].quietZonePx.left")) + " pixels"
    Debug "padding: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].paddingPx")) + " pixels"
    Debug "rotation: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].rotation"))
    Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasWidth")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasHeight")) + " pixels"

    ;  The image, enlarged, with the quiet zone and the padding tinted here to show where they are.
    ;  In the image itself both are plain white.  A QR code has no finder pattern (the large squares)
    ;  in its bottom right corner.  After the quarter turn that corner is at the bottom left:

    ;  image

    ;  Sample output:

    ;  module: 6 pixels
    ;  symbol: 174 x 174 pixels at (46,46)
    ;  quiet zone: 36 pixels
    ;  padding: 10 pixels
    ;  rotation: 90
    ;  image: 266 x 266 pixels


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


    ProcedureReturn
EndProcedure