Unicode C++ Requires Chilkat v11.7.0+
Unicode C++
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
#include <CkJsonObjectW.h>
#include <CkFormatsW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkJsonObjectW json;
json.UpdateString(L"payload.text",L"https://www.chilkatsoft.com/");
json.UpdateInt(L"render.moduleSize",5);
CkFormatsW fmt;
CkJsonObjectW jsonOut;
success = fmt.QrCode(L"generate",json,jsonOut);
if (success == false) {
wprintf(L"%s\n",fmt.lastErrorText());
return;
}
// A data URI has the form data:<MIME type>;base64,<base64 data>
CkStringBuilderW sbHtml;
sbHtml.Append(L"<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"");
sbHtml.AppendInt(jsonOut.IntOf(L"outputs[0].width"));
sbHtml.Append(L"\" height=\"");
sbHtml.AppendInt(jsonOut.IntOf(L"outputs[0].height"));
sbHtml.Append(L"\" src=\"data:");
sbHtml.Append(jsonOut.stringOf(L"outputs[0].mimeType"));
sbHtml.Append(L";base64,");
sbHtml.Append(jsonOut.stringOf(L"outputs[0].base64"));
sbHtml.Append(L"\"></body></html>");
success = sbHtml.WriteFile(L"qa_output/qr_page.html",L"utf-8",false);
if (success == false) {
wprintf(L"%s\n",sbHtml.lastErrorText());
return;
}
wprintf(L"image: %d x %d pixels\n",jsonOut.IntOf(L"outputs[0].width"),jsonOut.IntOf(L"outputs[0].height"));
wprintf(L"HTML length: %d\n",sbHtml.get_Length());
// The page, as a browser shows it:
//
// Sample output:
// image: 185 x 185 pixels
// HTML length: 1211
}