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

Measure a QR Code without Rendering It

See more QR Code Examples

Demonstrates the measure operation, which does everything generate does except draw the image. It reports the version chosen and the exact size the image would have, which is useful for laying out a page or a label before any image is made. Unlike generate, a code that does not fit the limits is not a failure for measure: the call succeeds, fits is false, and reasons[] says why.

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

    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,"measure",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "fits: " + Str(CkJsonObject::ckBoolOf(jsonOut,"fits"))
    Debug "QR version: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))
    Debug "image would be: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasWidth")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].canvasHeight")) + " pixels"
    Debug "symbol: " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolWidth")) + " pixels wide at x = " + Str(CkJsonObject::ckIntOf(jsonOut,"measurements[0].symbolX"))

    ;  Would it fit in a space 200 pixels wide?
    CkJsonObject::ckUpdateInt(json,"render.maxWidth",200)
    success = CkFormats::ckQrCode(fmt,"measure",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "within 200 pixels, fits: " + Str(CkJsonObject::ckBoolOf(jsonOut,"fits"))
    If CkJsonObject::ckBoolOf(jsonOut,"fits") = 0
        Debug "reason: " + CkJsonObject::ckStringOf(jsonOut,"reasons[0].code") + " (" + CkJsonObject::ckStringOf(jsonOut,"reasons[0].message") + ")"
    EndIf

    ;  What measure reported, drawn as a diagram.  The measure operation itself makes no image:

    ;  image

    ;  Sample output:

    ;  fits: True
    ;  QR version: 4
    ;  image would be: 328 x 328 pixels
    ;  symbol: 264 pixels wide at x = 32
    ;  within 200 pixels, fits: False
    ;  reason: noFit (exceedsMaxWidth)


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


    ProcedureReturn
EndProcedure