| 1 | qrcode-rust |
| 2 | =========== |
| 3 | |
| 4 | [](https://github.com/kennytm/qrcode-rust/actions?query=workflow%3ARust) |
| 5 | [](https://crates.io/crates/qrcode) |
| 6 | [](./LICENSE-APACHE.txt) |
| 7 | |
| 8 | QR code and Micro QR code encoder in Rust. [Documentation](https://docs.rs/qrcode). |
| 9 | |
| 10 | Cargo.toml |
| 11 | ---------- |
| 12 | |
| 13 | ```toml |
| 14 | [dependencies] |
| 15 | qrcode = "0.14.1" |
| 16 | ``` |
| 17 | |
| 18 | The default settings will depend on the `image` crate. If you don't need image generation capability, disable the `default-features`: |
| 19 | |
| 20 | ```toml |
| 21 | [dependencies] |
| 22 | qrcode = { version = "0.14.1", default-features = false } |
| 23 | ``` |
| 24 | |
| 25 | Example |
| 26 | ------- |
| 27 | |
| 28 | ## Image generation |
| 29 | |
| 30 | ```rust |
| 31 | use qrcode::QrCode; |
| 32 | use image::Luma; |
| 33 | |
| 34 | fn main() { |
| 35 | // Encode some data into bits. |
| 36 | let code = QrCode::new(b"01234567").unwrap(); |
| 37 | |
| 38 | // Render the bits into an image. |
| 39 | let image = code.render::<Luma<u8>>().build(); |
| 40 | |
| 41 | // Save the image. |
| 42 | image.save("/tmp/qrcode.png").unwrap(); |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | Generates this image: |
| 47 | |
| 48 |  |
| 49 | |
| 50 | ## String generation |
| 51 | |
| 52 | ```rust |
| 53 | use qrcode::QrCode; |
| 54 | |
| 55 | fn main() { |
| 56 | let code = QrCode::new(b"Hello").unwrap(); |
| 57 | let string = code.render::<char>() |
| 58 | .quiet_zone(false) |
| 59 | .module_dimensions(2, 1) |
| 60 | .build(); |
| 61 | println!("{}", string); |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | Generates this output: |
| 66 | |
| 67 | ```none |
| 68 | ############## ######## ############## |
| 69 | ## ## ## ## ## |
| 70 | ## ###### ## ## ## ## ## ###### ## |
| 71 | ## ###### ## ## ## ## ###### ## |
| 72 | ## ###### ## #### ## ## ###### ## |
| 73 | ## ## #### ## ## ## |
| 74 | ############## ## ## ## ############## |
| 75 | ## ## |
| 76 | ## ########## ## ## ########## |
| 77 | ## ## ######## #### ## |
| 78 | ########## #### ## #### ###### |
| 79 | ## ## #### ########## #### |
| 80 | ###### ########## ## ## ## |
| 81 | ## ## ## ## |
| 82 | ############## ## ## ## ## #### |
| 83 | ## ## ## ## ########## |
| 84 | ## ###### ## ## ## ## ## ## |
| 85 | ## ###### ## #### ########## ## |
| 86 | ## ###### ## #### ## #### ## |
| 87 | ## ## ## ######## ###### |
| 88 | ############## #### ## ## ## |
| 89 | ``` |
| 90 | |
| 91 | ## SVG generation |
| 92 | |
| 93 | ```rust |
| 94 | use qrcode::{QrCode, Version, EcLevel}; |
| 95 | use qrcode::render::svg; |
| 96 | |
| 97 | fn main() { |
| 98 | let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap(); |
| 99 | let image = code.render() |
| 100 | .min_dimensions(200, 200) |
| 101 | .dark_color(svg::Color("#800000")) |
| 102 | .light_color(svg::Color("#ffff80")) |
| 103 | .build(); |
| 104 | println!("{}", image); |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | Generates this SVG: |
| 109 | |
| 110 | [](src/test_annex_i_micro_qr_as_svg.svg) |
| 111 | |
| 112 | ## Unicode string generation |
| 113 | |
| 114 | ```rust |
| 115 | use qrcode::QrCode; |
| 116 | use qrcode::render::unicode; |
| 117 | |
| 118 | fn main() { |
| 119 | let code = QrCode::new("mow mow").unwrap(); |
| 120 | let image = code.render::<unicode::Dense1x2>() |
| 121 | .dark_color(unicode::Dense1x2::Light) |
| 122 | .light_color(unicode::Dense1x2::Dark) |
| 123 | .build(); |
| 124 | println!("{}", image); |
| 125 | } |
| 126 | ``` |
| 127 | |
| 128 | Generates this output: |
| 129 | |
| 130 | ```text |
| 131 | █████████████████████████████ |
| 132 | █████████████████████████████ |
| 133 | ████ ▄▄▄▄▄ █ ▀▀▀▄█ ▄▄▄▄▄ ████ |
| 134 | ████ █ █ █▀ ▀ ▀█ █ █ ████ |
| 135 | ████ █▄▄▄█ ██▄ ▀█ █▄▄▄█ ████ |
| 136 | ████▄▄▄▄▄▄▄█ ▀▄▀ █▄▄▄▄▄▄▄████ |
| 137 | ████▄▀ ▄▀ ▄ █▄█ ▀ ▀█ █▄ ████ |
| 138 | ████▄██▄▄▀▄▄▀█▄ ██▀▀█▀▄▄▄████ |
| 139 | █████▄▄▄█▄▄█ ▀▀▄█▀▀▀▄█▄▄████ |
| 140 | ████ ▄▄▄▄▄ █ ▄▄██▄ ▄ ▀▀████ |
| 141 | ████ █ █ █▀▄▄▀▄▄ ▄▄▄▄ ▄████ |
| 142 | ████ █▄▄▄█ █▄ █▄▀▄▀██▄█▀████ |
| 143 | ████▄▄▄▄▄▄▄█▄████▄█▄██▄██████ |
| 144 | █████████████████████████████ |
| 145 | ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ |
| 146 | ``` |
| 147 | |
| 148 | ## PIC generation |
| 149 | |
| 150 | ```rust |
| 151 | use qrcode::render::pic; |
| 152 | use qrcode::QrCode; |
| 153 | |
| 154 | fn main() { |
| 155 | let code = QrCode::new(b"01234567").unwrap(); |
| 156 | let image = code |
| 157 | .render::<pic::Color>() |
| 158 | .min_dimensions(1, 1) |
| 159 | .build(); |
| 160 | println!("{}", image); |
| 161 | } |
| 162 | ``` |
| 163 | |
| 164 | Generates [PIC](https://en.wikipedia.org/wiki/PIC_(markup_language)) |
| 165 | output that renders as follows: |
| 166 | |
| 167 | ```pic |
| 168 | maxpswid=29;maxpsht=29;movewid=0;moveht=1;boxwid=1;boxht=1 |
| 169 | define p { box wid $3 ht $4 fill 1 with .nw at $1,-$2 } |
| 170 | box wid maxpswid ht maxpsht with .nw at 0,0 |
| 171 | p(4,4,1,1) |
| 172 | p(5,4,1,1) |
| 173 | p(6,4,1,1) |
| 174 | p(7,4,1,1) |
| 175 | p(8,4,1,1) |
| 176 | p(9,4,1,1) |
| 177 | … |
| 178 | ``` |
| 179 | See [`test_annex_i_micro_qr_as_pic.pic`](src/test_annex_i_micro_qr_as_pic.pic) for a full example. |