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

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

Chilkat Unicode C Downloads

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

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

    success = FALSE;

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

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"payload.text",L"https://www.chilkatsoft.com/");
    CkJsonObjectW_UpdateInt(json,L"render.moduleSize",6);

    fmt = CkFormatsW_Create();
    jsonOut = CkJsonObjectW_Create();
    success = CkFormatsW_QrCode(fmt,L"generate",json,jsonOut);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFormatsW_lastErrorText(fmt));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        return;
    }

    wprintf(L"format: %s\n",CkJsonObjectW_stringOf(jsonOut,L"outputs[0].format"));
    wprintf(L"MIME type: %s\n",CkJsonObjectW_stringOf(jsonOut,L"outputs[0].mimeType"));
    wprintf(L"image: %d x %d pixels\n",CkJsonObjectW_IntOf(jsonOut,L"outputs[0].width"),CkJsonObjectW_IntOf(jsonOut,L"outputs[0].height"));

    //  outputs[0].base64 is the PNG file, base64 encoded.
    bdPng = CkBinDataW_Create();
    success = CkBinDataW_AppendEncoded(bdPng,CkJsonObjectW_stringOf(jsonOut,L"outputs[0].base64"),L"base64");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bdPng));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        CkBinDataW_Dispose(bdPng);
        return;
    }

    wprintf(L"PNG bytes: %d\n",CkBinDataW_getNumBytes(bdPng));

    //  The BinData holds the image.  Here it is simply saved.
    success = CkBinDataW_WriteFile(bdPng,L"qa_output/qr_in_memory.png");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bdPng));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        CkBinDataW_Dispose(bdPng);
        return;
    }

    //  The PNG held in bdPng:

    //  image

    //  Sample output:

    //  format: png
    //  MIME type: image/png
    //  image: 222 x 222 pixels
    //  PNG bytes: 877


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

    }