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

Generate a QR Code with the Barcode Method

See more QR Code Examples

Demonstrates the Barcode method, which is the general form of QrCode: the request names the symbology, and everything else is the same. QrCode is Barcode with the symbology already set to qr, and identical requests to the two methods give identical results. A program that will produce more than one kind of barcode can use Barcode for all of 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 json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("symbology", "qr") catch {};
    json.updateString("payload.text", "https://www.chilkatsoft.com/") catch {};
    json.updateString("output.path", "qa_output/qr_from_barcode.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.barcode("generate", json, json_out) catch {
        std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("symbology: {s}\n", .{try json_out.stringOf(alloc, "symbols[0].symbology")});
    std.debug.print("QR version: {d}\n", .{json_out.intOf("symbols[0].selectedEncoding.version")});
    std.debug.print("image: {d} x {d} pixels\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height") });

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

    // image

    // Barcode requires the symbology.  QrCode does not.
    const json2 = try chilkat.JsonObject.init();
    defer json2.deinit();
    json2.updateString("payload.text", "https://www.chilkatsoft.com/") catch {};
    fmt.barcode("generate", json2, json_out) catch {
        std.debug.print("without a symbology: {s} at {s}\n", .{ try json_out.stringOf(alloc, "errors[0].code"), try json_out.stringOf(alloc, "errors[0].path") });
    };

    // Sample output:

    // symbology: qr
    // QR version: 3
    // image: 148 x 148 pixels
    // without a symbology: missingOption at /symbology
}