Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
json: HCkJsonObject;
fmt: HCkFormats;
jsonOut: HCkJsonObject;
sbHtml: HCkStringBuilder;

begin
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) then
  begin
    Memo1.Lines.Add(CkFormats__lastErrorText(fmt));
    Exit;
  end;

//  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) then
  begin
    Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbHtml));
    Exit;
  end;

Memo1.Lines.Add('image: ' + IntToStr(CkJsonObject_IntOf(jsonOut,'outputs[0].width')) + ' x ' + IntToStr(CkJsonObject_IntOf(jsonOut,'outputs[0].height')) + ' pixels');
Memo1.Lines.Add('HTML length: ' + IntToStr(CkStringBuilder_getLength(sbHtml)));

//  The page, as a browser shows it:

//  image

//  Sample output:

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

CkJsonObject_Dispose(json);
CkFormats_Dispose(fmt);
CkJsonObject_Dispose(jsonOut);
CkStringBuilder_Dispose(sbHtml);