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

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oFmt
LOCAL oJsonOut
LOCAL oSbHtml

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("payload.text", "https://www.chilkatsoft.com/")
oJson:UpdateInt("render.moduleSize", 5)

oFmt := CreateObject("Chilkat.Formats")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oFmt:QrCode("generate", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oFmt:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  A data URI has the form  data:<MIME type>;base64,<base64 data>
oSbHtml := CreateObject("Chilkat.StringBuilder")
oSbHtml:Append('<html><body><p>Scan to visit chilkatsoft.com</p><img alt="QR code" width="')
oSbHtml:AppendInt(oJsonOut:IntOf("outputs[0].width"))
oSbHtml:Append('" height="')
oSbHtml:AppendInt(oJsonOut:IntOf("outputs[0].height"))
oSbHtml:Append('" src="data:')
oSbHtml:Append(oJsonOut:StringOf("outputs[0].mimeType"))
oSbHtml:Append(";base64,")
oSbHtml:Append(oJsonOut:StringOf("outputs[0].base64"))
oSbHtml:Append('"></body></html>')

nSuccess := oSbHtml:WriteFile("qa_output/qr_page.html", "utf-8", 0)
IF (nSuccess == 0)
    ? oSbHtml:LastErrorText
    oJson:destroy()
    oFmt:destroy()
    oJsonOut:destroy()
    oSbHtml:destroy()
    RETURN
ENDIF

? "image: " + Str(oJsonOut:IntOf("outputs[0].width")) + " x " + Str(oJsonOut:IntOf("outputs[0].height")) + " pixels"
? "HTML length: " + Str(oSbHtml:Length)

//  The page, as a browser shows it:

//  image

//  Sample output:

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

oJson:destroy()
oFmt:destroy()
oJsonOut:destroy()
oSbHtml:destroy()