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

Generate a QR Code PNG File

See more QR Code Examples

Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.

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 {};

    // Each module (one of the small squares) is drawn 8 pixels wide.  The default is 4.
    json.updateInt("render.moduleSize", 8) catch {};

    // Write the image to a file.  An existing file is replaced only when output.overwrite is true.
    // Otherwise the call fails and the existing file is left as it was.
    json.updateString("output.path", "qa_output/qr_chilkat.png") catch {};
    json.updateBool("output.overwrite", true) catch {};

    // The image is also returned in the response as base64 unless output.return is "none".
    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("file: {s}\n", .{try json_out.stringOf(alloc, "outputs[0].path")});
    std.debug.print("image: {d} x {d} pixels, {d} bytes\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height"), json_out.intOf("outputs[0].byteCount") });
    std.debug.print("QR version: {d}\n", .{json_out.intOf("symbols[0].selectedEncoding.version")});
    std.debug.print("error correction: {s}\n", .{try json_out.stringOf(alloc, "symbols[0].selectedEncoding.errorCorrection")});
    std.debug.print("modules: {d} x {d}\n", .{ json_out.intOf("symbols[0].dimensions.modulesWide"), json_out.intOf("symbols[0].dimensions.modulesHigh") });

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

    // image

    // Sample output:

    // file: qa_output/qr_chilkat.png
    // image: 296 x 296 pixels, 1251 bytes
    // QR version: 3
    // error correction: M
    // modules: 29 x 29
}