27 releases (14 breaking)

0.14.1 Jul 5, 2024
0.14.0 Mar 14, 2024
0.13.0 Dec 11, 2023
0.12.0 Feb 23, 2020
0.0.2 Nov 28, 2014

#3 in Images

Download history 50720/week @ 2024-07-18 52615/week @ 2024-07-25 40326/week @ 2024-08-01 43500/week @ 2024-08-08 38190/week @ 2024-08-15 40179/week @ 2024-08-22 39015/week @ 2024-08-29 44225/week @ 2024-09-05 40176/week @ 2024-09-12 44024/week @ 2024-09-19 43330/week @ 2024-09-26 48152/week @ 2024-10-03 42015/week @ 2024-10-10 50726/week @ 2024-10-17 50765/week @ 2024-10-24 49711/week @ 2024-10-31

203,438 downloads per month
Used in 163 crates (115 directly)

MIT/Apache

215KB
4K SLoC

qrcode-rust

Build status crates.io MIT OR Apache 2.0

QR code and Micro QR code encoder in Rust. Documentation.

Cargo.toml

[dependencies]
qrcode = "0.14.1"

The default settings will depend on the image crate. If you don't need image generation capability, disable the default-features:

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

Example

Image generation

use qrcode::QrCode;
use image::Luma;

fn main() {
    // Encode some data into bits.
    let code = QrCode::new(b"01234567").unwrap();

    // Render the bits into an image.
    let image = code.render::<Luma<u8>>().build();

    // Save the image.
    image.save("/tmp/qrcode.png").unwrap();
}

Generates this image:

Output

String generation

use qrcode::QrCode;

fn main() {
    let code = QrCode::new(b"Hello").unwrap();
    let string = code.render::<char>()
        .quiet_zone(false)
        .module_dimensions(2, 1)
        .build();
    println!("{}", string);
}

Generates this output:

##############    ########  ##############
##          ##          ##  ##          ##
##  ######  ##  ##  ##  ##  ##  ######  ##
##  ######  ##  ##  ##      ##  ######  ##
##  ######  ##  ####    ##  ##  ######  ##
##          ##  ####  ##    ##          ##
##############  ##  ##  ##  ##############
                ##  ##
##  ##########    ##  ##    ##########
      ##        ##    ########    ####  ##
    ##########    ####  ##  ####  ######
    ##    ##  ####  ##########    ####
  ######    ##########  ##    ##        ##
                ##      ##    ##  ##
##############    ##  ##  ##    ##  ####
##          ##  ##  ##        ##########
##  ######  ##  ##    ##  ##    ##    ##
##  ######  ##  ####  ##########  ##
##  ######  ##  ####    ##  ####    ##
##          ##    ##  ########  ######
##############  ####    ##      ##    ##

SVG generation

use qrcode::{QrCode, Version, EcLevel};
use qrcode::render::svg;

fn main() {
    let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
    let image = code.render()
        .min_dimensions(200, 200)
        .dark_color(svg::Color("#800000"))
        .light_color(svg::Color("#ffff80"))
        .build();
    println!("{}", image);
}

Generates this SVG:

Output

Unicode string generation

use qrcode::QrCode;
use qrcode::render::unicode;

fn main() {
    let code = QrCode::new("mow mow").unwrap();
    let image = code.render::<unicode::Dense1x2>()
        .dark_color(unicode::Dense1x2::Light)
        .light_color(unicode::Dense1x2::Dark)
        .build();
    println!("{}", image);
}

Generates this output:

█████████████████████████████
█████████████████████████████
████ ▄▄▄▄▄ █ ▀▀▀▄█ ▄▄▄▄▄ ████
████ █   █ █▀ ▀ ▀█ █   █ ████
████ █▄▄▄█ ██▄  ▀█ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ ▀▄▀ █▄▄▄▄▄▄▄████
████▄▀ ▄▀ ▄ █▄█  ▀ ▀█ █▄ ████
████▄██▄▄▀▄▄▀█▄ ██▀▀█▀▄▄▄████
█████▄▄▄█▄▄█  ▀▀▄█▀▀▀▄█▄▄████
████ ▄▄▄▄▄ █   ▄▄██▄ ▄ ▀▀████
████ █   █ █▀▄▄▀▄▄ ▄▄▄▄ ▄████
████ █▄▄▄█ █▄  █▄▀▄▀██▄█▀████
████▄▄▄▄▄▄▄█▄████▄█▄██▄██████
█████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

PIC generation

use qrcode::render::pic;
use qrcode::QrCode;

fn main() {
    let code = QrCode::new(b"01234567").unwrap();
    let image = code
        .render::<pic::Color>()
        .min_dimensions(1, 1)
        .build();
    println!("{}", image);
}

Generates PIC output that renders as follows:

maxpswid=29;maxpsht=29;movewid=0;moveht=1;boxwid=1;boxht=1
define p { box wid $3 ht $4 fill 1 with .nw at $1,-$2 }
box wid maxpswid ht maxpsht with .nw at 0,0
p(4,4,1,1)
p(5,4,1,1)
p(6,4,1,1)
p(7,4,1,1)
p(8,4,1,1)
p(9,4,1,1)

See test_annex_i_micro_qr_as_pic.pic for a full example.

Dependencies

~1.5MB
~28K SLoC