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

Unicode C
#include <C_CkFormatsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFormatsW fmt;
    HCkJsonObjectW json;
    HCkJsonObjectW jsonOut;

    success = FALSE;

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

    fmt = CkFormatsW_Create();
    json = CkJsonObjectW_Create();
    jsonOut = CkJsonObjectW_Create();

    //  1. A misspelled option: "moduleSise" for "moduleSize".
    CkJsonObjectW_UpdateString(json,L"payload.text",L"https://www.chilkatsoft.com/");
    CkJsonObjectW_UpdateInt(json,L"render.moduleSise",8);
    success = CkFormatsW_QrCode(fmt,L"generate",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"code: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].code"));
        wprintf(L"path: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].path"));
        wprintf(L"message: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].message"));
    }

    //  2. A value out of range: QR versions are 1 to 40.
    CkJsonObjectW_Clear(json);
    CkJsonObjectW_UpdateString(json,L"payload.text",L"https://www.chilkatsoft.com/");
    CkJsonObjectW_UpdateInt(json,L"encoding.qr.version",41);
    success = CkFormatsW_QrCode(fmt,L"generate",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"code: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].code"));
        wprintf(L"path: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].path"));
        wprintf(L"message: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].message"));
    }

    //  3. Too much data: this text does not fit a version 1 symbol.
    CkJsonObjectW_Clear(json);
    CkJsonObjectW_UpdateString(json,L"payload.text",L"https://www.chilkatsoft.com/barcode_overview.asp");
    CkJsonObjectW_UpdateInt(json,L"encoding.qr.version",1);
    success = CkFormatsW_QrCode(fmt,L"generate",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"code: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].code"));
        wprintf(L"path: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].path"));
        wprintf(L"message: %s\n",CkJsonObjectW_stringOf(jsonOut,L"errors[0].message"));
    }

    //  A program can act on the code.
    if (CkJsonObjectW_stringOf(jsonOut,L"errors[0].code") == L"capacityExceeded") {
        wprintf(L"The text is too long for the symbol size allowed.\n");
    }

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


    CkFormatsW_Dispose(fmt);
    CkJsonObjectW_Dispose(json);
    CkJsonObjectW_Dispose(jsonOut);

    }