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

QR Code Error Correction Levels

See more QR Code Examples

Demonstrates the four QR error-correction levels. L, M, Q and H allow a reader to recover the data when about 7%, 15%, 25% and 30% of the symbol is damaged or covered. More error correction leaves less room for data, so the same text needs a larger symbol at a higher level. The default is M. The example also shows boostErrorCorrection, which raises the level for free when the chosen version has room to spare.

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 = chilkat::JsonObject::new();
let json_out = chilkat::JsonObject::new();
let _ = json.update_string("payload.text", "https://www.chilkatsoft.com/barcode_overview.asp");

// Encode the same text at each level.
let levels = chilkat::JsonArray::new();
let _ = levels.load("[\"L\",\"M\",\"Q\",\"H\"]");
for i in 0..=3 {
    let _ = json.update_string("encoding.qr.errorCorrection", &levels.string_at(i).unwrap_or_default());
    if fmt.qr_code("generate", &json, &json_out).is_err() {
        println!("{}", fmt.last_error_text());
        return;
    }

    println!("level {}: version {}, {} modules wide", levels.string_at(i).unwrap_or_default(), json_out.int_of("symbols[0].selectedEncoding.version"), json_out.int_of("symbols[0].dimensions.modulesWide"));
}

// The four symbols.  They hold the same text, and the higher the level, the larger the symbol:

// image

// A symbol often has room left over.  boostErrorCorrection uses it: the level is raised
// as far as the same version allows, so the symbol is no larger but is more robust.
let json2 = chilkat::JsonObject::new();
let _ = json2.update_string("payload.text", "https://chilkatsoft.com");
let _ = json2.update_string("encoding.qr.errorCorrection", "L");
if fmt.qr_code("generate", &json2, &json_out).is_err() {
    println!("{}", fmt.last_error_text());
    return;
}

println!("level L requested: version {}, level {}", json_out.int_of("symbols[0].selectedEncoding.version"), json_out.string_of("symbols[0].selectedEncoding.errorCorrection").unwrap_or_default());

let _ = json2.update_bool("encoding.qr.boostErrorCorrection", true);
if fmt.qr_code("generate", &json2, &json_out).is_err() {
    println!("{}", fmt.last_error_text());
    return;
}

println!("level L with boost: version {}, level {}", json_out.int_of("symbols[0].selectedEncoding.version"), json_out.string_of("symbols[0].selectedEncoding.errorCorrection").unwrap_or_default());

// Level L, and level M reached by the boost.  The two symbols are the same size:

// image

// Sample output:

// level L: version 3, 29 modules wide
// level M: version 4, 33 modules wide
// level Q: version 5, 37 modules wide
// level H: version 6, 41 modules wide
// level L requested: version 2, level L
// level L with boost: version 2, level M