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

Generate a QR Code as SVG

See more QR Code Examples

Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.

Chilkat Unicode C Downloads

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

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

    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_UpdateString(json,L"output.format",L"svg");

    //  An SVG is text, so it can be returned as text rather than as base64.
    CkJsonObjectW_UpdateString(json,L"output.return",L"text");

    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;
    }

    sbSvg = CkStringBuilderW_Create();
    CkJsonObjectW_StringOfSb(jsonOut,L"outputs[0].text",sbSvg);

    wprintf(L"MIME type: %s\n",CkJsonObjectW_stringOf(jsonOut,L"outputs[0].mimeType"));
    wprintf(L"size: %d x %d %s\n",CkJsonObjectW_IntOf(jsonOut,L"outputs[0].width"),CkJsonObjectW_IntOf(jsonOut,L"outputs[0].height"),CkJsonObjectW_stringOf(jsonOut,L"outputs[0].units"));
    wprintf(L"SVG length: %d characters\n",CkStringBuilderW_getLength(sbSvg));

    //  The SVG is a complete document that can be saved, or placed inline in an HTML page.
    success = CkStringBuilderW_WriteFile(sbSvg,L"qa_output/qr_chilkat.svg",L"utf-8",FALSE);
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbSvg));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        CkStringBuilderW_Dispose(sbSvg);
        return;
    }

    //  The SVG:

    //  image

    //  Sample output:

    //  MIME type: image/svg+xml
    //  size: 148 x 148 px
    //  SVG length: 12500 characters


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

    }