Zig Requires Chilkat v11.7.0+
Zig
Split Data across Several QR Codes (Structured Append)
See more QR Code Examples
Demonstrates structured append, the QR feature for data too large for one symbol of the size wanted. The data is divided over up to 16 symbols, and each carries its position, the total, and a parity byte that ties the set together. With encoding.structuredAppend.enabled, QrCode uses as few symbols as it can. output.pathTemplate writes one file per symbol, with {index} replaced by 1, 2, 3 and so on. The reader must support structured append to put the parts back together. Many phone camera apps do not, and show each part separately.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.
// Some text that is too long for one symbol of the size allowed below.
const sb_text = try chilkat.StringBuilder.init();
defer sb_text.deinit();
var i: i32 = 0;
i = 1;
while (i <= 20) : (i += 1) {
sb_text.append("Line ") catch {};
sb_text.appendInt(i) catch {};
sb_text.appendLine(" of a message that is too long for one small QR code.", true) catch {};
}
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateSb("payload.text", sb_text) catch {};
// Keep each symbol small (version 10 is 57 modules wide) and allow a set of symbols.
json.updateInt("encoding.qr.maxVersion", 10) catch {};
json.updateBool("encoding.structuredAppend.enabled", true) catch {};
// One file for each symbol: qr_part_1.png, qr_part_2.png, ...
json.updateString("output.pathTemplate", "qa_output/qr_part_{index}.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("text length: {d}\n", .{sb_text.getLength()});
const num_symbols = json_out.sizeOfArray("symbols");
std.debug.print("symbols: {d}\n", .{num_symbols});
i = 0;
while (i < num_symbols) : (i += 1) {
json_out.setI(i);
// structuredAppend.index counts from 0.
std.debug.print("{s}: symbol {d} of {d}, version {d}, {d} bytes\n", .{ try json_out.stringOf(alloc, "outputs[i].path"), (json_out.intOf("symbols[i].structuredAppend.index") + 1), json_out.intOf("symbols[i].structuredAppend.total"), json_out.intOf("symbols[i].selectedEncoding.version"), json_out.intOf("symbols[i].payloadBytes") });
}
// Every symbol of a set carries the same parity byte, which is how a reader knows they belong together.
std.debug.print("parity: {d}\n", .{json_out.intOf("symbols[0].structuredAppend.parity")});
// The six symbols:
//
// Sample output:
// text length: 1231
// symbols: 6
// qa_output/qr_part_1.png: symbol 1 of 6, version 10, 206 bytes
// qa_output/qr_part_2.png: symbol 2 of 6, version 10, 205 bytes
// qa_output/qr_part_3.png: symbol 3 of 6, version 10, 205 bytes
// qa_output/qr_part_4.png: symbol 4 of 6, version 10, 205 bytes
// qa_output/qr_part_5.png: symbol 5 of 6, version 10, 205 bytes
// qa_output/qr_part_6.png: symbol 6 of 6, version 10, 205 bytes
// parity: 50
}