Zig Requires Chilkat v11.7.0+
Zig
QR Code with Non-English Text (UTF-8 and ECI)
See more QR Code Examples
Demonstrates text outside ASCII. QrCode always stores text as UTF-8 bytes, which is what phones expect. A QR code can also say so explicitly with an ECI header: setting encoding.eci to 26 declares the bytes to be UTF-8, for readers that would otherwise assume another character set. The header costs a little space, and a few older readers do not understand it, so it is off by default.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.
// The text is "Gruesse aus Muenchen" written with u-umlaut and sharp s. It is given here with
// JSON escapes only so that this source is plain ASCII. Decode turns the escapes into characters.
const sb_text = try chilkat.StringBuilder.init();
defer sb_text.deinit();
sb_text.append("Gr\u{00fc}\u{00df}e aus M\u{00fc}nchen") catch {};
sb_text.decode("json", "utf-8") catch {};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateSb("payload.text", sb_text) catch {};
json.updateString("output.path", "qa_output/qr_utf8.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;
};
// 17 characters, three of which take two bytes in UTF-8.
std.debug.print("characters: {d}\n", .{sb_text.getLength()});
std.debug.print("bytes encoded: {d}\n", .{json_out.intOf("symbols[0].payloadBytes")});
std.debug.print("ECI header: {}\n", .{json_out.hasMember("symbols[0].selectedEncoding.eci")});
// Without an ECI header (qa_output/qr_utf8.png):
//
// The same text with an ECI header declaring UTF-8.
json.updateInt("encoding.eci", 26) catch {};
json.updateString("output.path", "qa_output/qr_utf8_eci.png") catch {};
fmt.qrCode("generate", json, json_out) catch {
std.debug.print("{s}\n", .{try fmt.getLastErrorText(alloc)});
return;
};
std.debug.print("with encoding.eci = 26, ECI header: {}, value {d}\n", .{ json_out.hasMember("symbols[0].selectedEncoding.eci"), json_out.intOf("symbols[0].selectedEncoding.eci") });
// With the ECI header (qa_output/qr_utf8_eci.png). Both scan as the same text:
//
// Sample output:
// characters: 17
// bytes encoded: 20
// ECI header: False
// with encoding.eci = 26, ECI header: True, value 26
}