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

Embed a QR Code in HTML as a Data URI

See more QR Code Examples

Demonstrates putting a QR code directly into an HTML page or HTML email with no separate image file. The base64 PNG returned by QrCode becomes the src of an img tag as a data URI, which is the MIME type from outputs[0].mimeType followed by the base64 from outputs[0].base64.

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

    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",5);

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

    //  A data URI has the form  data:<MIME type>;base64,<base64 data>
    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHtml,L"<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"");
    CkStringBuilderW_AppendInt(sbHtml,CkJsonObjectW_IntOf(jsonOut,L"outputs[0].width"));
    CkStringBuilderW_Append(sbHtml,L"\" height=\"");
    CkStringBuilderW_AppendInt(sbHtml,CkJsonObjectW_IntOf(jsonOut,L"outputs[0].height"));
    CkStringBuilderW_Append(sbHtml,L"\" src=\"data:");
    CkStringBuilderW_Append(sbHtml,CkJsonObjectW_stringOf(jsonOut,L"outputs[0].mimeType"));
    CkStringBuilderW_Append(sbHtml,L";base64,");
    CkStringBuilderW_Append(sbHtml,CkJsonObjectW_stringOf(jsonOut,L"outputs[0].base64"));
    CkStringBuilderW_Append(sbHtml,L"\"></body></html>");

    success = CkStringBuilderW_WriteFile(sbHtml,L"qa_output/qr_page.html",L"utf-8",FALSE);
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbHtml));
        CkJsonObjectW_Dispose(json);
        CkFormatsW_Dispose(fmt);
        CkJsonObjectW_Dispose(jsonOut);
        CkStringBuilderW_Dispose(sbHtml);
        return;
    }

    wprintf(L"image: %d x %d pixels\n",CkJsonObjectW_IntOf(jsonOut,L"outputs[0].width"),CkJsonObjectW_IntOf(jsonOut,L"outputs[0].height"));
    wprintf(L"HTML length: %d\n",CkStringBuilderW_getLength(sbHtml));

    //  The page, as a browser shows it:

    //  image

    //  Sample output:

    //  image: 185 x 185 pixels
    //  HTML length: 1211


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

    }