Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

QR Code with a Text Caption

See more QR Code Examples

Demonstrates printing a line of text under a QR code, such as the order number or the address the code holds, so that a person can read it too. render.text turns the caption on and gives its value, which is printable ASCII and is drawn in a built-in font that needs no font files. The caption can go above or below the symbol, be aligned left, center or right, and be scaled.

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/order/12345") catch {};
    json.updateInt("render.moduleSize", 6) catch {};

    // The caption.  It need not be the same as the text in the QR code.
    json.updateBool("render.text.enabled", true) catch {};
    json.updateString("render.text.value", "ORDER 12345") catch {};
    // position is "below" (the default) or "above".  alignment is "center" (the default), "left" or "right".
    json.updateString("render.text.position", "below") catch {};
    // Each dot of the caption's font is drawn this many pixels wide.
    json.updateInt("render.text.scale", 3) catch {};

    json.updateString("output.path", "qa_output/qr_caption.png") catch {};
    json.updateBool("output.overwrite", true) catch {};
    json.updateString("output.return", "none") 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;
    };

    std.debug.print("caption: {s}\n", .{try json_out.stringOf(alloc, "measurements[0].text")});
    std.debug.print("caption scale: {d}\n", .{json_out.intOf("measurements[0].textScale")});
    std.debug.print("height added for the caption: {d} pixels\n", .{json_out.intOf("measurements[0].textBandPx")});
    std.debug.print("image: {d} x {d} pixels\n", .{ json_out.intOf("measurements[0].canvasWidth"), json_out.intOf("measurements[0].canvasHeight") });

    // The image written to qa_output/qr_caption.png:

    // image

    // Sample output:

    // caption: ORDER 12345
    // caption scale: 3
    // height added for the caption: 21 pixels
    // image: 222 x 243 pixels
}