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

QR Code with Binary Data

See more QR Code Examples

Demonstrates a QR code that holds arbitrary bytes rather than text. The payload type is binary and the bytes are given as base64 in payload.base64. Any byte value may be used, including zero. Keep in mind that many phone camera apps expect text, so binary payloads are for readers that you control.

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.

    // The bytes to encode.  Here they come from hex, but they could be loaded from a file with LoadFile.
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.appendEncoded("000102030405FF00FE4368696C6B6174", "hex") catch {
        std.debug.print("{s}\n", .{try bd.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("payload.type", "binary") catch {};
    json.updateString("payload.base64", try bd.getEncoded(alloc, "base64")) catch {};
    json.updateString("output.path", "qa_output/qr_binary.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("bytes given: {d}\n", .{bd.getNumBytes()});
    std.debug.print("bytes encoded: {d}\n", .{json_out.intOf("symbols[0].payloadBytes")});
    std.debug.print("mode: {s}\n", .{try json_out.stringOf(alloc, "symbols[0].selectedEncoding.mode")});
    std.debug.print("QR version: {d}\n", .{json_out.intOf("symbols[0].selectedEncoding.version")});

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

    // image

    // Sample output:

    // bytes given: 16
    // bytes encoded: 16
    // mode: byte
    // QR version: 2
}