Zig Requires Chilkat v11.7.0+
Zig
Generate a QR Code as SVG
See more QR Code Examples
Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.Chilkat Zig Downloads
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.updateString("output.format", "svg") catch {};
// An SVG is text, so it can be returned as text rather than as base64.
json.updateString("output.return", "text") 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;
};
const sb_svg = try chilkat.StringBuilder.init();
defer sb_svg.deinit();
json_out.stringOfSb("outputs[0].text", sb_svg) catch {};
std.debug.print("MIME type: {s}\n", .{try json_out.stringOf(alloc, "outputs[0].mimeType")});
std.debug.print("size: {d} x {d} {s}\n", .{ json_out.intOf("outputs[0].width"), json_out.intOf("outputs[0].height"), try json_out.stringOf(alloc, "outputs[0].units") });
std.debug.print("SVG length: {d} characters\n", .{sb_svg.getLength()});
// The SVG is a complete document that can be saved, or placed inline in an HTML page.
sb_svg.writeFile("qa_output/qr_chilkat.svg", "utf-8", false) catch {
std.debug.print("{s}\n", .{try sb_svg.getLastErrorText(alloc)});
return;
};
// The SVG:
//
// Sample output:
// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters
}