C Requires Chilkat v11.7.0+
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 C Downloads
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject json;
HCkFormats fmt;
HCkJsonObject jsonOut;
HCkStringBuilder sbHtml;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"payload.text","https://www.chilkatsoft.com/");
CkJsonObject_UpdateInt(json,"render.moduleSize",5);
fmt = CkFormats_Create();
jsonOut = CkJsonObject_Create();
success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkFormats_lastErrorText(fmt));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
return;
}
// A data URI has the form data:<MIME type>;base64,<base64 data>
sbHtml = CkStringBuilder_Create();
CkStringBuilder_Append(sbHtml,"<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"");
CkStringBuilder_AppendInt(sbHtml,CkJsonObject_IntOf(jsonOut,"outputs[0].width"));
CkStringBuilder_Append(sbHtml,"\" height=\"");
CkStringBuilder_AppendInt(sbHtml,CkJsonObject_IntOf(jsonOut,"outputs[0].height"));
CkStringBuilder_Append(sbHtml,"\" src=\"data:");
CkStringBuilder_Append(sbHtml,CkJsonObject_stringOf(jsonOut,"outputs[0].mimeType"));
CkStringBuilder_Append(sbHtml,";base64,");
CkStringBuilder_Append(sbHtml,CkJsonObject_stringOf(jsonOut,"outputs[0].base64"));
CkStringBuilder_Append(sbHtml,"\"></body></html>");
success = CkStringBuilder_WriteFile(sbHtml,"qa_output/qr_page.html","utf-8",FALSE);
if (success == FALSE) {
printf("%s\n",CkStringBuilder_lastErrorText(sbHtml));
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkStringBuilder_Dispose(sbHtml);
return;
}
printf("image: %d x %d pixels\n",CkJsonObject_IntOf(jsonOut,"outputs[0].width"),CkJsonObject_IntOf(jsonOut,"outputs[0].height"));
printf("HTML length: %d\n",CkStringBuilder_getLength(sbHtml));
// The page, as a browser shows it:
//
// Sample output:
// image: 185 x 185 pixels
// HTML length: 1211
CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkStringBuilder_Dispose(sbHtml);
}