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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("payload.text", "https://www.chilkatsoft.com/") catch {};
    json.updateInt("render.moduleSize", 5) catch {};

    const fmt = try chilkat.Formats.init();
    defer fmt.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    // A data URI has the form  data:<MIME type>;base64,<base64 data>
    const sb_html = try chilkat.StringBuilder.init();
    defer sb_html.deinit();
    sb_html.append("<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"") catch {};
    sb_html.appendInt(json_out.intOf("outputs[0].width")) catch {};
    sb_html.append("\" height=\"") catch {};
    sb_html.appendInt(json_out.intOf("outputs[0].height")) catch {};
    sb_html.append("\" src=\"data:") catch {};
    sb_html.append(try json_out.stringOf(alloc, "outputs[0].mimeType")) catch {};
    sb_html.append(";base64,") catch {};
    sb_html.append(try json_out.stringOf(alloc, "outputs[0].base64")) catch {};
    sb_html.append("\"></body></html>") catch {};

    sb_html.writeFile("qa_output/qr_page.html", "utf-8", false) catch {
        std.debug.print("{s}\n", .{try sb_html.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("image: {d} x {d} pixels\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height") });
    std.debug.print("HTML length: {d}\n", .{sb_html.getLength()});

    // The page, as a browser shows it:

    // image

    // Sample output:

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