Zig Requires Chilkat v11.7.0+
Zig
Measure a QR Code without Rendering It
See more QR Code Examples
Demonstrates the measure operation, which does everything generate does except draw the image. It reports the version chosen and the exact size the image would have, which is useful for laying out a page or a label before any image is made. Unlike generate, a code that does not fit the limits is not a failure for measure: the call succeeds, fits is false, and reasons[] says why.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/barcode_overview.asp") catch {};
json.updateInt("render.moduleSize", 8) catch {};
const fmt = try chilkat.Formats.init();
defer fmt.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
fmt.qrCode("measure", json, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("fits: {}\n", .{json_out.boolOf("fits")});
std.debug.print("QR version: {d}\n", .{json_out.intOf("symbols[0].selectedEncoding.version")});
std.debug.print("image would be: {d} x {d} pixels\n", .{ json_out.intOf("measurements[0].canvasWidth"), json_out.intOf("measurements[0].canvasHeight") });
std.debug.print("symbol: {d} pixels wide at x = {d}\n", .{ json_out.intOf("measurements[0].symbolWidth"), json_out.intOf("measurements[0].symbolX") });
// Would it fit in a space 200 pixels wide?
json.updateInt("render.maxWidth", 200) catch {};
fmt.qrCode("measure", json, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("within 200 pixels, fits: {}\n", .{json_out.boolOf("fits")});
if (!json_out.boolOf("fits")) {
std.debug.print("reason: {s} ({s})\n", .{ try json_out.stringOf(alloc, "reasons[0].code"), try json_out.stringOf(alloc, "reasons[0].message") });
}
// What measure reported, drawn as a diagram. The measure operation itself makes no image:
//
// Sample output:
// fits: True
// QR version: 4
// image would be: 328 x 328 pixels
// symbol: 264 pixels wide at x = 32
// within 200 pixels, fits: False
// reason: noFit (exceedsMaxWidth)
}