Rust Requires Chilkat v11.7.0+
Rust
Embed a QR Code in HTML as a Data URI
See more QR Code Examples
Demonstrates putting a QR code directly into an HTML page or HTML email with no separate image file. The base64 PNG returned by QrCode becomes the src of an img tag as a data URI, which is the MIME type from outputs[0].mimeType followed by the base64 from outputs[0].base64.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", 5);
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;
}
// A data URI has the form data:<MIME type>;base64,<base64 data>
let sb_html = chilkat::StringBuilder::new();
let _ = sb_html.append("<html><body><p>Scan to visit chilkatsoft.com</p><img alt=\"QR code\" width=\"");
let _ = sb_html.append_int(json_out.int_of("outputs[0].width"));
let _ = sb_html.append("\" height=\"");
let _ = sb_html.append_int(json_out.int_of("outputs[0].height"));
let _ = sb_html.append("\" src=\"data:");
let _ = sb_html.append(&json_out.string_of("outputs[0].mimeType").unwrap_or_default());
let _ = sb_html.append(";base64,");
let _ = sb_html.append(&json_out.string_of("outputs[0].base64").unwrap_or_default());
let _ = sb_html.append("\"></body></html>");
if sb_html.write_file("qa_output/qr_page.html", "utf-8", false).is_err() {
println!("{}", sb_html.last_error_text());
return;
}
println!("image: {} x {} pixels", json_out.int_of("outputs[0].width"), json_out.int_of("outputs[0].height"));
println!("HTML length: {}", sb_html.length());
// The page, as a browser shows it:
//
// Sample output:
// image: 185 x 185 pixels
// HTML length: 1211