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

QR Code Colors and Transparent Background

See more QR Code Examples

Demonstrates colored QR codes. render.foreground is the color of the dark modules and render.background the color behind them, each as #RRGGBB. Either can instead be an object with a color and an opacity from 0 to 1, and a background opacity of 0 gives a PNG with a transparent background to place over a page or a design. Readers need contrast: keep the modules clearly darker than whatever ends up behind them.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let fmt = chilkat::Formats::new();
let json_out = chilkat::JsonObject::new();

// Dark blue modules on a pale yellow background.
let json = chilkat::JsonObject::new();
let _ = json.update_string("payload.text", "https://www.chilkatsoft.com/");
let _ = json.update_int("render.moduleSize", 6);
let _ = json.update_string("render.foreground", "#1B3A6B");
let _ = json.update_string("render.background", "#FFF8DC");
let _ = json.update_string("output.path", "qa_output/qr_blue.png");
let _ = json.update_bool("output.overwrite", true);
let _ = json.update_string("output.return", "none");
if fmt.qr_code("generate", &json, &json_out).is_err() {
    println!("{}", fmt.last_error_text());
    return;
}

println!("colored: {}, {} bytes", json_out.string_of("outputs[0].path").unwrap_or_default(), json_out.int_of("outputs[0].byteCount"));

// The image written to qa_output/qr_blue.png:

// image

// The same modules on a fully transparent background.
let json2 = chilkat::JsonObject::new();
let _ = json2.update_string("payload.text", "https://www.chilkatsoft.com/");
let _ = json2.update_int("render.moduleSize", 6);
let _ = json2.update_string("render.foreground", "#1B3A6B");
let _ = json2.update_string("render.background.color", "#FFFFFF");
let _ = json2.update_number("render.background.opacity", "0");
let _ = json2.update_string("output.path", "qa_output/qr_transparent.png");
let _ = json2.update_bool("output.overwrite", true);
let _ = json2.update_string("output.return", "none");
if fmt.qr_code("generate", &json2, &json_out).is_err() {
    println!("{}", fmt.last_error_text());
    return;
}

println!("transparent: {}, {} bytes", json_out.string_of("outputs[0].path").unwrap_or_default(), json_out.int_of("outputs[0].byteCount"));

// qa_output/qr_transparent.png, placed here over a checkerboard and over a colored page
// to show that whatever is behind the image shows through:

// image

// Sample output:

// colored: qa_output/qr_blue.png, 1345 bytes
// transparent: qa_output/qr_transparent.png, 1377 bytes