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

Handle QR Code Errors

See more QR Code Examples

Demonstrates what happens when a request is refused. QrCode returns false and the response holds errors[0] with three members: a code that does not change between versions, the path of the request member at fault (a JSON Pointer such as /encoding/qr/version), and a message. Requests are checked strictly, so a misspelled option is an error rather than being silently ignored. LastErrorText has the same information in more detail.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oFmt
LOCAL oJson
LOCAL oJsonOut

nSuccess := 0

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

oFmt := CreateObject("Chilkat.Formats")
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")

//  1. A misspelled option: "moduleSise" for "moduleSize".
oJson:UpdateString("payload.text", "https://www.chilkatsoft.com/")
oJson:UpdateInt("render.moduleSise", 8)
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? "code: " + oJsonOut:StringOf("errors[0].code")
    ? "path: " + oJsonOut:StringOf("errors[0].path")
    ? "message: " + oJsonOut:StringOf("errors[0].message")
ENDIF

//  2. A value out of range: QR versions are 1 to 40.
oJson:Clear()
oJson:UpdateString("payload.text", "https://www.chilkatsoft.com/")
oJson:UpdateInt("encoding.qr.version", 41)
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? "code: " + oJsonOut:StringOf("errors[0].code")
    ? "path: " + oJsonOut:StringOf("errors[0].path")
    ? "message: " + oJsonOut:StringOf("errors[0].message")
ENDIF

//  3. Too much data: this text does not fit a version 1 symbol.
oJson:Clear()
oJson:UpdateString("payload.text", "https://www.chilkatsoft.com/barcode_overview.asp")
oJson:UpdateInt("encoding.qr.version", 1)
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? "code: " + oJsonOut:StringOf("errors[0].code")
    ? "path: " + oJsonOut:StringOf("errors[0].path")
    ? "message: " + oJsonOut:StringOf("errors[0].message")
ENDIF

//  A program can act on the code.
IF (oJsonOut:StringOf("errors[0].code") == "capacityExceeded")
    ? "The text is too long for the symbol size allowed."
ENDIF

//  Sample output:

//  code: unknownOption
//  path: /render/moduleSise
//  message: unknown option
//  code: outOfRange
//  path: /encoding/qr/version
//  message: the value is out of range
//  code: capacityExceeded
//  path: /payload
//  message: the payload does not fit the requested QR version at the requested error-correction level
//  The text is too long for the symbol size allowed.

oFmt:destroy()
oJson:destroy()
oJsonOut:destroy()