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

Lua
local chilkat = require("chilkat")

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

local json = chilkat.JsonObject.new()
json:update_string("payload.text", "https://www.chilkatsoft.com/")
json:update_int("render.moduleSize", 5)

local fmt = chilkat.Formats.new()
local jsonOut = chilkat.JsonObject.new()
local success = fmt:qr_code("generate", json, jsonOut)
if not success then
    print(fmt.last_error_text)
    return
end

-- A data URI has the form  data:<MIME type>;base64,<base64 data>
local sbHtml = chilkat.StringBuilder.new()
sbHtml:append("<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"")
sbHtml:append_int(jsonOut:int_of("outputs[0].width"))
sbHtml:append("\" height=\"")
sbHtml:append_int(jsonOut:int_of("outputs[0].height"))
sbHtml:append("\" src=\"data:")
sbHtml:append(jsonOut:string_of("outputs[0].mimeType"))
sbHtml:append(";base64,")
sbHtml:append(jsonOut:string_of("outputs[0].base64"))
sbHtml:append("\"></body></html>")

success = sbHtml:write_file("qa_output/qr_page.html", "utf-8", false)
if not success then
    print(sbHtml.last_error_text)
    return
end

print("image: " .. jsonOut:int_of("outputs[0].width") .. " x " .. jsonOut:int_of("outputs[0].height") .. " pixels")
print("HTML length: " .. sbHtml.length)

-- The page, as a browser shows it:

-- image

-- Sample output:

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