Rust Requires Chilkat v11.7.0+
Rust
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 Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let json = chilkat::JsonObject::new();
let _ = json.update_string("payload.text", "CHILKAT");
let _ = json.update_bool("output.includeModuleData", true);
let fmt = chilkat::Formats::new();
let json_out = chilkat::JsonObject::new();
if fmt.qr_code("generate", &json, &json_out).is_err() {
println!("{}", fmt.last_error_text());
return;
}
let width = json_out.int_of("symbols[0].moduleData.width");
let height = json_out.int_of("symbols[0].moduleData.height");
let row_bytes = json_out.int_of("symbols[0].moduleData.rowBytes");
println!("kind: {}", json_out.string_of("symbols[0].moduleData.kind").unwrap_or_default());
println!("modules: {} x {}, {} bytes per row", width, height, row_bytes);
// The matrix is height rows of rowBytes bytes.
let bd = chilkat::BinData::new();
if bd.append_encoded(&json_out.string_of("symbols[0].moduleData.bytesBase64").unwrap_or_default(), "base64").is_err() {
println!("{}", bd.last_error_text());
return;
}
let sb_row = chilkat::StringBuilder::new();
let mut b: i32 = 0;
let mut mask = 128;
for row in 0..height {
sb_row.clear();
for col in 0..width {
// 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 (col % 8) == 0 {
mask = 128;
}
b = bd.get_byte((row * row_bytes) + (col / 8));
if ((b / mask) % 2) == 1 {
let _ = sb_row.append("##");
} else {
let _ = sb_row.append(" ");
}
mask = mask / 2;
}
println!("{}", sb_row.get_as_string().unwrap_or_default());
}
// The same symbol as the PNG that the call also returned:
//
// Sample output:
// kind: bitMatrix
// modules: 21 x 21, 3 bytes per row
// ############## ##############
// ## ## #### ## ##
// ## ###### ## ## #### ## ###### ##
// ## ###### ## ## ## ###### ##
// ## ###### ## ## ## ## ## ###### ##
// ## ## #### ## ## ##
// ############## ## ## ## ##############
// ####
// ## ########## #### ##########
// #### ## ######## ######
// #### ######## #### ## #### ####
// ## ## ############ #### ##
// ###### #### ###### ## ## ##
// ## ## ## ## ## ##
// ############## ## ## ## ##
// ## ## ## ## ###### ####
// ## ###### ## #### ## ## ##
// ## ###### ## ## ############ ##
// ## ###### ## ## ## #### ##
// ## ## ## ######## ##
// ############## ## ## ######