Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Get the Modules of a QR Code (Bit Matrix)

See more QR Code Examples

Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.

Chilkat Zig Downloads

Zig
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", "CHILKAT") catch {};
    json.updateBool("output.includeModuleData", true) 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 width = json_out.intOf("symbols[0].moduleData.width");
    const height = json_out.intOf("symbols[0].moduleData.height");
    const row_bytes = json_out.intOf("symbols[0].moduleData.rowBytes");
    std.debug.print("kind: {s}\n", .{try json_out.stringOf(alloc, "symbols[0].moduleData.kind")});
    std.debug.print("modules: {d} x {d}, {d} bytes per row\n", .{ width, height, row_bytes });

    // The matrix is height rows of rowBytes bytes.
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.appendEncoded(try json_out.stringOf(alloc, "symbols[0].moduleData.bytesBase64"), "base64") catch {
        std.debug.print("{s}\n", .{try bd.getLastErrorText(alloc)});
        return;
    };

    const sb_row = try chilkat.StringBuilder.init();
    defer sb_row.deinit();
    var row: i32 = 0;
    var col: i32 = 0;
    var b: i32 = 0;
    var mask: i32 = 128;
    row = 0;
    while (row < height) : (row += 1) {
        sb_row.clear();
        col = 0;
        while (col < width) : (col += 1) {
            // The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
            // The leftmost module of each byte is its most significant bit.
            if ((@rem(col, 8)) == 0) {
                mask = 128;
            }

            b = bd.getByte((row * row_bytes) + (@divTrunc(col, 8)));
            if ((@rem((@divTrunc(b, mask)), 2)) == 1) {
                sb_row.append("##") catch {};
            } else {
                sb_row.append("  ") catch {};
            }

            mask = @divTrunc(mask, 2);
        }

        std.debug.print("{s}\n", .{try sb_row.getAsString(alloc)});
    }

    // The same symbol as the PNG that the call also returned:

    // image

    // Sample output:

    // kind: bitMatrix
    // modules: 21 x 21, 3 bytes per row
    // ##############              ##############
    // ##          ##      ####    ##          ##
    // ##  ######  ##  ##  ####    ##  ######  ##
    // ##  ######  ##  ##          ##  ######  ##
    // ##  ######  ##  ##  ##  ##  ##  ######  ##
    // ##          ##  ####  ##    ##          ##
    // ##############  ##  ##  ##  ##############
    //                 ####
    // ##  ##########      ####    ##########
    //     ####          ##  ########    ######
    // ####  ########    ####  ##  ####      ####
    //       ##  ##      ############    ####  ##
    //   ######  ####  ######  ##    ##  ##
    //                 ##  ##  ##    ##  ##    ##
    // ##############        ##  ##    ##      ##
    // ##          ##  ##  ##        ######  ####
    // ##  ######  ##  ####  ##  ##    ##
    // ##  ######  ##  ##  ############    ##
    // ##  ######  ##  ##      ##  ####  ##
    // ##          ##    ##  ########          ##
    // ##############  ##      ##      ######
}