Media

QR codes

QR codes as SVG for links, tickets, Wi-Fi and two-factor set-up, in any colours, or drawn as text.

Version 1.0.0 · 4 functions · 1 crate

#qr#svg#images#2fa#tickets

Functions

What use qr.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use qr_svg, qr_svg_colors from qr (Quillo 0.6).

qr_svg(text: str, size: int) → stror an error

An SVG image of text as a QR code, at least size pixels wide, dark on light; put it in a page with raw(...) or serve it as image/svg+xml.

qr_svg_colors(text: str, size: int, dark: str, light: str) → stror an error

The same, in the colours given (CSS colours), with error correction "M"; use a dark colour on a light one for scanners.

qr_text(text: str) → stror an error

The QR code drawn in text, for a terminal or a log: quillo run printing a link to scan.

wifi_qr_text(ssid: str, password: str) → str

The text a phone's camera joins a Wi-Fi network from (WPA security), for a QR code by the door.

Get it

$ quillo install qr               # qr.qwer into this folder$ quillo install qr --example     # and its example, qr.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/qr.qwer$ curl -O https://quillolang.org/plugins/files/qr.qwe

Then, in your program, all of it or only what you call:

main.qwe
use qr.qweruse qr_svg, qr_svg_colors from qr      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
qrcode = { version = "0.14", default-features = false, features = ["svg"] }

Example

qr.qwe
// A QR code for a link, served as an image and shown in a page.////   quillo run qr.qwe      then open http://localhost:8080/use "qr.qwer"route GET /qr.svg ? { text: str = "https://quillolang.org" } =    respond(200, qr_svg(text, 256)?, { "content-type": "image/svg+xml", "cache-control": "max-age=86400" })route GET / = <html><body>    <h1>Scan to open the docs</h1>    <div style="width: 200px">{raw(qr_svg_colors("https://quillolang.org/docs/", 200, "#1b1630", "#f7f3e8")?)}</div>    <img src="/qr.svg?text=WIFI%3AT%3AWPA%3BS%3ACafe%3BP%3Asecret%3B%3B" width="160" alt="Join the café's Wi-Fi"/></body></html>
$ quillo run qr.qwe$ curl 'localhost:8080/qr.svg?text=hello'$ curl 'localhost:8080/'

Source

qr.qwer
//! QR codes as SVG: links, tickets, payments, Wi-Fi and two-factor set-up, sharp at any size.//! version: 1.0.0//!//! [dependencies]//! qrcode = { version = "0.14", default-features = false, features = ["svg"] }use qrcode::render::svg;use qrcode::{EcLevel, QrCode};fn code(text: &str, level: &str) -> Result<QrCode, String> {    let ec = match level { "L" | "l" => EcLevel::L, "Q" | "q" => EcLevel::Q, "H" | "h" => EcLevel::H, _ => EcLevel::M };    QrCode::with_error_correction_level(text.as_bytes(), ec).map_err(|e| e.to_string())}/// An SVG image of `text` as a QR code, at least `size` pixels wide, dark on light; put it in a page with `raw(...)` or serve it as image/svg+xml.pub fn qr_svg(text: &str, size: i64) -> Result<String, String> {    qr_svg_colors(text, size, "#000000", "#ffffff")}/// The same, in the colours given (CSS colours), with error correction "M"; use a dark colour on a light one for scanners.pub fn qr_svg_colors(text: &str, size: i64, dark: &str, light: &str) -> Result<String, String> {    let s = size.clamp(32, 4096) as u32;    Ok(code(text, "M")?.render::<svg::Color>().min_dimensions(s, s).dark_color(svg::Color(dark)).light_color(svg::Color(light)).build())}/// The QR code drawn in text, for a terminal or a log: `quillo run` printing a link to scan.pub fn qr_text(text: &str) -> Result<String, String> {    Ok(code(text, "L")?.render::<char>().quiet_zone(true).module_dimensions(2, 1).dark_color('█').light_color(' ').build())}/// The text a phone's camera joins a Wi-Fi network from (`WPA` security), for a QR code by the door.pub fn wifi_qr_text(ssid: &str, password: &str) -> String {    let esc = |s: &str| s.replace('\\', "\\\\").replace(';', "\\;").replace(',', "\\,").replace(':', "\\:").replace('"', "\\\"");    format!("WIFI:T:WPA;S:{};P:{};;", esc(ssid), esc(password))}
Thread
107Useful? Like it, or add to its thread below.

Thread 0 posts

On QR codes: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.