Zig Requires Chilkat v11.7.0+
Zig
QR Code Error Correction Levels
See more QR Code Examples
Demonstrates the four QR error-correction levels. L, M, Q and H allow a reader to recover the data when about 7%, 15%, 25% and 30% of the symbol is damaged or covered. More error correction leaves less room for data, so the same text needs a larger symbol at a higher level. The default is M. The example also shows boostErrorCorrection, which raises the level for free when the chosen version has room to spare.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 fmt = try chilkat.Formats.init();
defer fmt.deinit();
const json = try chilkat.JsonObject.init();
defer json.deinit();
const json_out = try chilkat.JsonObject.init();
defer json_out.deinit();
json.updateString("payload.text", "https://www.chilkatsoft.com/barcode_overview.asp") catch {};
// Encode the same text at each level.
const levels = try chilkat.JsonArray.init();
defer levels.deinit();
levels.load("[\"L\",\"M\",\"Q\",\"H\"]") catch {};
var i: i32 = 0;
i = 0;
while (i <= 3) : (i += 1) {
json.updateString("encoding.qr.errorCorrection", try levels.stringAt(alloc, i)) catch {};
fmt.qrCode("generate", json, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("level {s}: version {d}, {d} modules wide\n", .{ try levels.stringAt(alloc, i), json_out.intOf("symbols[0].selectedEncoding.version"), json_out.intOf("symbols[0].dimensions.modulesWide") });
}
// The four symbols. They hold the same text, and the higher the level, the larger the symbol:
//
// A symbol often has room left over. boostErrorCorrection uses it: the level is raised
// as far as the same version allows, so the symbol is no larger but is more robust.
const json2 = try chilkat.JsonObject.init();
defer json2.deinit();
json2.updateString("payload.text", "https://chilkatsoft.com") catch {};
json2.updateString("encoding.qr.errorCorrection", "L") catch {};
fmt.qrCode("generate", json2, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("level L requested: version {d}, level {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "symbols[0].selectedEncoding.errorCorrection") });
json2.updateBool("encoding.qr.boostErrorCorrection", true) catch {};
fmt.qrCode("generate", json2, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("level L with boost: version {d}, level {s}\n", .{ json_out.intOf("symbols[0].selectedEncoding.version"), try json_out.stringOf(alloc, "symbols[0].selectedEncoding.errorCorrection") });
// Level L, and level M reached by the boost. The two symbols are the same size:
//
// Sample output:
// level L: version 3, 29 modules wide
// level M: version 4, 33 modules wide
// level Q: version 5, 37 modules wide
// level H: version 6, 41 modules wide
// level L requested: version 2, level L
// level L with boost: version 2, level M
}