Rust Requires Chilkat v11.7.0+
Rust
Generate a QR Code PNG in Memory
See more QR Code Examples
Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.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", "https://www.chilkatsoft.com/");
let _ = json.update_int("render.moduleSize", 6);
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;
}
println!("format: {}", json_out.string_of("outputs[0].format").unwrap_or_default());
println!("MIME type: {}", json_out.string_of("outputs[0].mimeType").unwrap_or_default());
println!("image: {} x {} pixels", json_out.int_of("outputs[0].width"), json_out.int_of("outputs[0].height"));
// outputs[0].base64 is the PNG file, base64 encoded.
let bd_png = chilkat::BinData::new();
if bd_png.append_encoded(&json_out.string_of("outputs[0].base64").unwrap_or_default(), "base64").is_err() {
println!("{}", bd_png.last_error_text());
return;
}
println!("PNG bytes: {}", bd_png.num_bytes());
// The BinData holds the image. Here it is simply saved.
if bd_png.write_file("qa_output/qr_in_memory.png").is_err() {
println!("{}", bd_png.last_error_text());
return;
}
// The PNG held in bdPng:
//
// Sample output:
// format: png
// MIME type: image/png
// image: 222 x 222 pixels
// PNG bytes: 877