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

QR Code Colors and Transparent Background

See more QR Code Examples

Demonstrates colored QR codes. render.foreground is the color of the dark modules and render.background the color behind them, each as #RRGGBB. Either can instead be an object with a color and an opacity from 0 to 1, and a background opacity of 0 gives a PNG with a transparent background to place over a page or a design. Readers need contrast: keep the modules clearly darker than whatever ends up behind them.

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 fmt = try chilkat.Formats.init();
    defer fmt.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();

    // Dark blue modules on a pale yellow background.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("payload.text", "https://www.chilkatsoft.com/") catch {};
    json.updateInt("render.moduleSize", 6) catch {};
    json.updateString("render.foreground", "#1B3A6B") catch {};
    json.updateString("render.background", "#FFF8DC") catch {};
    json.updateString("output.path", "qa_output/qr_blue.png") catch {};
    json.updateBool("output.overwrite", true) catch {};
    json.updateString("output.return", "none") catch {};
    fmt.qrCode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("colored: {s}, {d} bytes\n", .{ try json_out.stringOf(alloc, "outputs[0].path"), json_out.intOf("outputs[0].byteCount") });

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

    // image

    // The same modules on a fully transparent background.
    const json2 = try chilkat.JsonObject.init();
    defer json2.deinit();
    json2.updateString("payload.text", "https://www.chilkatsoft.com/") catch {};
    json2.updateInt("render.moduleSize", 6) catch {};
    json2.updateString("render.foreground", "#1B3A6B") catch {};
    json2.updateString("render.background.color", "#FFFFFF") catch {};
    json2.updateNumber("render.background.opacity", "0") catch {};
    json2.updateString("output.path", "qa_output/qr_transparent.png") catch {};
    json2.updateBool("output.overwrite", true) catch {};
    json2.updateString("output.return", "none") catch {};
    fmt.qrCode("generate", json2, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("transparent: {s}, {d} bytes\n", .{ try json_out.stringOf(alloc, "outputs[0].path"), json_out.intOf("outputs[0].byteCount") });

    // qa_output/qr_transparent.png, placed here over a checkerboard and over a colored page
    // to show that whatever is behind the image shows through:

    // image

    // Sample output:

    // colored: qa_output/qr_blue.png, 1345 bytes
    // transparent: qa_output/qr_transparent.png, 1377 bytes
}