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

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

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", 6) 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("format: {s}\n", .{try json_out.stringOf(alloc, "outputs[0].format")});
    std.debug.print("MIME type: {s}\n", .{try json_out.stringOf(alloc, "outputs[0].mimeType")});
    std.debug.print("image: {d} x {d} pixels\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height") });

    // outputs[0].base64 is the PNG file, base64 encoded.
    const bd_png = try chilkat.BinData.init();
    defer bd_png.deinit();
    bd_png.appendEncoded(try json_out.stringOf(alloc, "outputs[0].base64"), "base64") catch {
        std.debug.print("{s}\n", .{try bd_png.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("PNG bytes: {d}\n", .{bd_png.getNumBytes()});

    // The BinData holds the image.  Here it is simply saved.
    bd_png.writeFile("qa_output/qr_in_memory.png") catch {
        std.debug.print("{s}\n", .{try bd_png.getLastErrorText(alloc)});
        return;
    };

    // The PNG held in bdPng:

    // image

    // Sample output:

    // format: png
    // MIME type: image/png
    // image: 222 x 222 pixels
    // PNG bytes: 877
}