| 1 | //! QRCode encoder |
| 2 | //! |
| 3 | //! This crate provides a QR code and Micro QR code encoder for binary data. |
| 4 | //! |
| 5 | #![cfg_attr(feature = "image", doc = "```rust")] |
| 6 | #![cfg_attr(not(feature = "image"), doc = "```ignore")] |
| 7 | //! use qrcode::QrCode; |
| 8 | //! use image::Luma; |
| 9 | //! |
| 10 | //! // Encode some data into bits. |
| 11 | //! let code = QrCode::new(b"01234567").unwrap(); |
| 12 | //! |
| 13 | //! // Render the bits into an image. |
| 14 | //! let image = code.render::<Luma<u8>>().build(); |
| 15 | //! |
| 16 | //! // Save the image. |
| 17 | //! # if cfg!(unix) { |
| 18 | //! image.save("/tmp/qrcode.png").unwrap(); |
| 19 | //! # } |
| 20 | //! |
| 21 | //! // You can also render it into a string. |
| 22 | //! let string = code.render() |
| 23 | //! .light_color(' ') |
| 24 | //! .dark_color('#') |
| 25 | //! .build(); |
| 26 | //! println!("{}", string); |
| 27 | //! ``` |
| 28 | |
| 29 | #![cfg_attr(feature = "bench", feature(test))] // Unstable libraries |
| 30 | #![warn(clippy::pedantic)] |
| 31 | #![allow( |
| 32 | clippy::must_use_candidate, // This is just annoying. |
| 33 | )] |
| 34 | #![cfg_attr(feature = "bench", doc = include_str!("../README.md"))] |
| 35 | // ^ make sure we can test our README.md. |
| 36 | |
| 37 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#; |
| 38 | |
| 39 | pub mod bitsrust-analyzer cargo qrcode 0.14.1 bits/; |
| 40 | pub mod canvasrust-analyzer cargo qrcode 0.14.1 canvas/; |
| 41 | mod castrust-analyzer cargo qrcode 0.14.1 cast/; |
| 42 | pub mod ecrust-analyzer cargo qrcode 0.14.1 ec/; |
| 43 | pub mod optimizerust-analyzer cargo qrcode 0.14.1 optimize/; |
| 44 | pub mod renderrust-analyzer cargo qrcode 0.14.1 render/; |
| 45 | pub mod typesrust-analyzer cargo qrcode 0.14.1 types/; |
| 46 | |
| 47 | pub use craterust-analyzer cargo qrcode 0.14.1 crate/::typesrust-analyzer cargo qrcode 0.14.1 types/::{Colorrust-analyzer cargo qrcode 0.14.1 types/Color#, EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, QrResultrust-analyzer cargo qrcode 0.14.1 types/QrResult#, Versionrust-analyzer cargo qrcode 0.14.1 types/Version#}; |
| 48 | |
| 49 | use craterust-analyzer cargo qrcode 0.14.1 crate/::castrust-analyzer cargo qrcode 0.14.1 cast/::Asrust-analyzer cargo qrcode 0.14.1 cast/As#; |
| 50 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::{Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel#, Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#}; |
| 51 | |
| 52 | /// The encoded QR code symbol. |
| 53 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 54 | pub struct QrCoderust-analyzer cargo qrcode 0.14.1 QrCode# { |
| 55 | contentrust-analyzer cargo qrcode 0.14.1 QrCode#content.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Colorrust-analyzer cargo qrcode 0.14.1 types/Color#>, |
| 56 | versionrust-analyzer cargo qrcode 0.14.1 QrCode#version.: Versionrust-analyzer cargo qrcode 0.14.1 types/Version#, |
| 57 | ec_levelrust-analyzer cargo qrcode 0.14.1 QrCode#ec_level.: EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, |
| 58 | widthrust-analyzer cargo qrcode 0.14.1 QrCode#width.: usize, |
| 59 | } |
| 60 | |
| 61 | impl QrCoderust-analyzer cargo qrcode 0.14.1 QrCode# { |
| 62 | /// Constructs a new QR code which automatically encodes the given data. |
| 63 | /// |
| 64 | /// This method uses the "medium" error correction level and automatically |
| 65 | /// chooses the smallest QR code. |
| 66 | /// |
| 67 | /// use qrcode::QrCode; |
| 68 | /// |
| 69 | /// let code = QrCode::new(b"Some data").unwrap(); |
| 70 | /// |
| 71 | /// # Errors |
| 72 | /// |
| 73 | /// Returns error if the QR code cannot be constructed, e.g. when the data |
| 74 | /// is too long. |
| 75 | pub fn newrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]new().<D: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(data: D) -> QrResultrust-analyzer cargo qrcode 0.14.1 types/QrResult#<Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]> { |
| 76 | Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]::with_error_correction_levelrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_error_correction_level().(data, EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Mrust-analyzer cargo qrcode 0.14.1 types/EcLevel#M#) |
| 77 | } |
| 78 | |
| 79 | /// Constructs a new QR code which automatically encodes the given data at a |
| 80 | /// specific error correction level. |
| 81 | /// |
| 82 | /// This method automatically chooses the smallest QR code. |
| 83 | /// |
| 84 | /// use qrcode::{QrCode, EcLevel}; |
| 85 | /// |
| 86 | /// let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap(); |
| 87 | /// |
| 88 | /// # Errors |
| 89 | /// |
| 90 | /// Returns error if the QR code cannot be constructed, e.g. when the data |
| 91 | /// is too long. |
| 92 | pub fn with_error_correction_levelrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_error_correction_level().<D: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(data: D, ec_level: EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#) -> QrResultrust-analyzer cargo qrcode 0.14.1 types/QrResult#<Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]> { |
| 93 | let bits = bitsrust-analyzer cargo qrcode 0.14.1 bits/::encode_autorust-analyzer cargo qrcode 0.14.1 bits/encode_auto().(data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), ec_level)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 94 | Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]::with_bitsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_bits().(bits, ec_level) |
| 95 | } |
| 96 | |
| 97 | /// Constructs a new QR code for the given version and error correction |
| 98 | /// level. |
| 99 | /// |
| 100 | /// use qrcode::{QrCode, Version, EcLevel}; |
| 101 | /// |
| 102 | /// let code = QrCode::with_version(b"Some data", Version::Normal(5), EcLevel::M).unwrap(); |
| 103 | /// |
| 104 | /// This method can also be used to generate Micro QR code. |
| 105 | /// |
| 106 | /// use qrcode::{QrCode, Version, EcLevel}; |
| 107 | /// |
| 108 | /// let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap(); |
| 109 | /// |
| 110 | /// # Errors |
| 111 | /// |
| 112 | /// Returns error if the QR code cannot be constructed, e.g. when the data |
| 113 | /// is too long, or when the version and error correction level are |
| 114 | /// incompatible. |
| 115 | pub fn with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().<D: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(data: D, version: Versionrust-analyzer cargo qrcode 0.14.1 types/Version#, ec_level: EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#) -> QrResultrust-analyzer cargo qrcode 0.14.1 types/QrResult#<Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]> { |
| 116 | let mut bits = bitsrust-analyzer cargo qrcode 0.14.1 bits/::Bitsrust-analyzer cargo qrcode 0.14.1 bits/Bits#::newrust-analyzer cargo qrcode 0.14.1 bits/impl#[Bits]new().(version); |
| 117 | bits.push_optimal_datarust-analyzer cargo qrcode 0.14.1 bits/impl#[Bits]push_optimal_data().(data.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 118 | bits.push_terminatorrust-analyzer cargo qrcode 0.14.1 bits/impl#[Bits]push_terminator().(ec_level)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 119 | Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]::with_bitsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_bits().(bits, ec_level) |
| 120 | } |
| 121 | |
| 122 | /// Constructs a new QR code with encoded bits. |
| 123 | /// |
| 124 | /// Use this method only if there are very special need to manipulate the |
| 125 | /// raw bits before encoding. Some examples are: |
| 126 | /// |
| 127 | /// * Encode data using specific character set with ECI |
| 128 | /// * Use the FNC1 modes |
| 129 | /// * Avoid the optimal segmentation algorithm |
| 130 | /// |
| 131 | /// See the `Bits` structure for detail. |
| 132 | /// |
| 133 | /// #![allow(unused_must_use)] |
| 134 | /// |
| 135 | /// use qrcode::{QrCode, Version, EcLevel}; |
| 136 | /// use qrcode::bits::Bits; |
| 137 | /// |
| 138 | /// let mut bits = Bits::new(Version::Normal(1)); |
| 139 | /// bits.push_eci_designator(9); |
| 140 | /// bits.push_byte_data(b"\xca\xfe\xe4\xe9\xea\xe1\xf2 QR"); |
| 141 | /// bits.push_terminator(EcLevel::L); |
| 142 | /// let qrcode = QrCode::with_bits(bits, EcLevel::L); |
| 143 | /// |
| 144 | /// # Errors |
| 145 | /// |
| 146 | /// Returns error if the QR code cannot be constructed, e.g. when the bits |
| 147 | /// are too long, or when the version and error correction level are |
| 148 | /// incompatible. |
| 149 | pub fn with_bitsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_bits().(bits: bitsrust-analyzer cargo qrcode 0.14.1 bits/::Bitsrust-analyzer cargo qrcode 0.14.1 bits/Bits#, ec_level: EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#) -> QrResultrust-analyzer cargo qrcode 0.14.1 types/QrResult#<Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]> { |
| 150 | let version = bits.versionrust-analyzer cargo qrcode 0.14.1 bits/impl#[Bits]version().(); |
| 151 | let data = bits.into_bytesrust-analyzer cargo qrcode 0.14.1 bits/impl#[Bits]into_bytes().(); |
| 152 | let (encoded_data, ec_data) = ecrust-analyzer cargo qrcode 0.14.1 ec/::construct_codewordsrust-analyzer cargo qrcode 0.14.1 ec/construct_codewords().(&data, version, ec_level)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 153 | let mut canvas = canvasrust-analyzer cargo qrcode 0.14.1 canvas/::Canvasrust-analyzer cargo qrcode 0.14.1 canvas/Canvas#::newrust-analyzer cargo qrcode 0.14.1 canvas/impl#[Canvas]new().(version, ec_level); |
| 154 | canvas.draw_all_functional_patternsrust-analyzer cargo qrcode 0.14.1 canvas/impl#[Canvas]draw_all_functional_patterns().(); |
| 155 | canvas.draw_datarust-analyzer cargo qrcode 0.14.1 canvas/impl#[Canvas]draw_data().(&encoded_data, &ec_data); |
| 156 | let canvas = canvas.apply_best_maskrust-analyzer cargo qrcode 0.14.1 canvas/impl#[Canvas]apply_best_mask().(); |
| 157 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo qrcode 0.14.1 impl#[QrCode] { contentrust-analyzer cargo qrcode 0.14.1 QrCode#content.: canvas.into_colorsrust-analyzer cargo qrcode 0.14.1 canvas/impl#[Canvas]into_colors().(), versionrust-analyzer cargo qrcode 0.14.1 QrCode#version., ec_levelrust-analyzer cargo qrcode 0.14.1 QrCode#ec_level., widthrust-analyzer cargo qrcode 0.14.1 QrCode#width.: version.widthrust-analyzer cargo qrcode 0.14.1 types/impl#[Version]width().().as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[i16][As]as_usize().() }) |
| 158 | } |
| 159 | |
| 160 | /// Gets the version of this QR code. |
| 161 | pub fn versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]version().(&self) -> Versionrust-analyzer cargo qrcode 0.14.1 types/Version# { |
| 162 | self.versionrust-analyzer cargo qrcode 0.14.1 QrCode#version. |
| 163 | } |
| 164 | |
| 165 | /// Gets the error correction level of this QR code. |
| 166 | pub fn error_correction_levelrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]error_correction_level().(&self) -> EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel# { |
| 167 | self.ec_levelrust-analyzer cargo qrcode 0.14.1 QrCode#ec_level. |
| 168 | } |
| 169 | |
| 170 | /// Gets the number of modules per side, i.e. the width of this QR code. |
| 171 | /// |
| 172 | /// The width here does not contain the quiet zone paddings. |
| 173 | pub fn widthrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]width().(&self) -> usize { |
| 174 | self.widthrust-analyzer cargo qrcode 0.14.1 QrCode#width. |
| 175 | } |
| 176 | |
| 177 | /// Gets the maximum number of allowed erratic modules can be introduced |
| 178 | /// before the data becomes corrupted. Note that errors should not be |
| 179 | /// introduced to functional modules. |
| 180 | #[allow(clippy::missing_panics_doc)] // the version and ec_level should have been checked when calling `.with_version()`. |
| 181 | pub fn max_allowed_errorsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]max_allowed_errors().(&self) -> usize { |
| 182 | ecrust-analyzer cargo qrcode 0.14.1 ec/::max_allowed_errorsrust-analyzer cargo qrcode 0.14.1 ec/max_allowed_errors().(self.versionrust-analyzer cargo qrcode 0.14.1 QrCode#version., self.ec_levelrust-analyzer cargo qrcode 0.14.1 QrCode#ec_level.).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("invalid version or ec_level") |
| 183 | } |
| 184 | |
| 185 | /// Checks whether a module at coordinate (x, y) is a functional module or |
| 186 | /// not. |
| 187 | /// |
| 188 | /// # Panics |
| 189 | /// |
| 190 | /// Panics if `x` or `y` is beyond the size of the QR code. |
| 191 | pub fn is_functionalrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]is_functional().(&self, x: usize, y: usize) -> bool { |
| 192 | let x = x.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("coordinate is too large for QR code"); |
| 193 | let y = y.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("coordinate is too large for QR code"); |
| 194 | canvasrust-analyzer cargo qrcode 0.14.1 canvas/::is_functionalrust-analyzer cargo qrcode 0.14.1 canvas/is_functional().(self.versionrust-analyzer cargo qrcode 0.14.1 QrCode#version., self.versionrust-analyzer cargo qrcode 0.14.1 QrCode#version..widthrust-analyzer cargo qrcode 0.14.1 types/impl#[Version]width().(), x, y) |
| 195 | } |
| 196 | |
| 197 | /// Converts the QR code into a human-readable string. This is mainly for |
| 198 | /// debugging only. |
| 199 | pub fn to_debug_strrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]to_debug_str().(&self, on_char: char, off_char: char) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 200 | self.renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().().quiet_zonerust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]quiet_zone().(false).dark_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]dark_color().(on_char).light_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]light_color().(off_char).buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().() |
| 201 | } |
| 202 | |
| 203 | /// Converts the QR code to a vector of booleans. Each entry represents the |
| 204 | /// color of the module, with "true" means dark and "false" means light. |
| 205 | #[deprecated(since = "0.4.0", note = "use `to_colors()` instead")] |
| 206 | pub fn to_vecrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]to_vec().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<bool> { |
| 207 | self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 208 | } |
| 209 | |
| 210 | /// Converts the QR code to a vector of booleans. Each entry represents the |
| 211 | /// color of the module, with "true" means dark and "false" means light. |
| 212 | #[deprecated(since = "0.4.0", note = "use `into_colors()` instead")] |
| 213 | pub fn into_vecrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]into_vec().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<bool> { |
| 214 | self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|c| c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 215 | } |
| 216 | |
| 217 | /// Converts the QR code to a vector of colors. |
| 218 | pub fn to_colorsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]to_colors().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Colorrust-analyzer cargo qrcode 0.14.1 types/Color#> { |
| 219 | self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() |
| 220 | } |
| 221 | |
| 222 | /// Converts the QR code to a vector of colors. |
| 223 | pub fn into_colorsrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]into_colors().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Colorrust-analyzer cargo qrcode 0.14.1 types/Color#> { |
| 224 | self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content. |
| 225 | } |
| 226 | |
| 227 | /// Renders the QR code into an image. The result is an image builder, which |
| 228 | /// you may do some additional configuration before copying it into a |
| 229 | /// concrete image. |
| 230 | /// |
| 231 | /// # Examples |
| 232 | /// |
| 233 | #[cfg_attr(feature = "image", doc = " ```rust")] |
| 234 | #[cfg_attr(not(feature = "image"), doc = " ```ignore")] |
| 235 | /// # use qrcode::QrCode; |
| 236 | /// # use image::Rgb; |
| 237 | /// |
| 238 | /// let image = QrCode::new(b"hello").unwrap() |
| 239 | /// .render() |
| 240 | /// .dark_color(Rgb([0, 0, 128])) |
| 241 | /// .light_color(Rgb([224, 224, 224])) // adjust colors |
| 242 | /// .quiet_zone(false) // disable quiet zone (white border) |
| 243 | /// .min_dimensions(300, 300) // sets minimum image size |
| 244 | /// .build(); |
| 245 | /// ``` |
| 246 | /// |
| 247 | /// Note: the `image` crate itself also provides method to rotate the image, |
| 248 | /// or overlay a logo on top of the QR code. |
| 249 | pub fn renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().<P: Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel#>(&self) -> Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#<P> { |
| 250 | let quiet_zone = if self.versionrust-analyzer cargo qrcode 0.14.1 QrCode#version..is_microrust-analyzer cargo qrcode 0.14.1 types/impl#[Version]is_micro().() { 2 } else { 4 }; |
| 251 | Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(&self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content., self.widthrust-analyzer cargo qrcode 0.14.1 QrCode#width., quiet_zone) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | impl Indexrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#<(usize, usize)> for QrCoderust-analyzer cargo qrcode 0.14.1 QrCode# { |
| 256 | type Outputrust-analyzer cargo qrcode 0.14.1 impl#[QrCode][`Index<(usize, usize)>`]Output# = Colorrust-analyzer cargo qrcode 0.14.1 types/Color#; |
| 257 | |
| 258 | fn indexrust-analyzer cargo qrcode 0.14.1 impl#[QrCode][`Index<(usize, usize)>`]index().(&self, (x, y): (usize, usize)) -> &Colorrust-analyzer cargo qrcode 0.14.1 types/Color# { |
| 259 | let index = y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.widthrust-analyzer cargo qrcode 0.14.1 QrCode#width. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().x; |
| 260 | &self.contentrust-analyzer cargo qrcode 0.14.1 QrCode#content.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #[cfg(test)] |
| 265 | mod testsrust-analyzer cargo qrcode 0.14.1 tests/ { |
| 266 | use craterust-analyzer cargo qrcode 0.14.1 crate/::{EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#, Versionrust-analyzer cargo qrcode 0.14.1 types/Version#}; |
| 267 | |
| 268 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 269 | fn test_annex_i_qrrust-analyzer cargo qrcode 0.14.1 tests/test_annex_i_qr().() { |
| 270 | // This uses the ISO Annex I as test vector. |
| 271 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().(b"01234567", Versionrust-analyzer cargo qrcode 0.14.1 types/Version#::Normalrust-analyzer cargo qrcode 0.14.1 types/Version#Normal#(1), EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Mrust-analyzer cargo qrcode 0.14.1 types/EcLevel#M#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 272 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 273 | &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().code.to_debug_strrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]to_debug_str().('#', '.'), |
| 274 | "\ |
| 275 | #######..#.##.#######\n\ |
| 276 | #.....#..####.#.....#\n\ |
| 277 | #.###.#.#.....#.###.#\n\ |
| 278 | #.###.#.##....#.###.#\n\ |
| 279 | #.###.#.#.###.#.###.#\n\ |
| 280 | #.....#.#...#.#.....#\n\ |
| 281 | #######.#.#.#.#######\n\ |
| 282 | ........#..##........\n\ |
| 283 | #.#####..#..#.#####..\n\ |
| 284 | ...#.#.##.#.#..#.##..\n\ |
| 285 | ..#...##.#.#.#..#####\n\ |
| 286 | ....#....#.....####..\n\ |
| 287 | ...######..#.#..#....\n\ |
| 288 | ........#.#####..##..\n\ |
| 289 | #######..##.#.##.....\n\ |
| 290 | #.....#.#.#####...#.#\n\ |
| 291 | #.###.#.#...#..#.##..\n\ |
| 292 | #.###.#.##..#..#.....\n\ |
| 293 | #.###.#.#.##.#..#.#..\n\ |
| 294 | #.....#........##.##.\n\ |
| 295 | #######.####.#..#.#.." |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 300 | fn test_annex_i_micro_qrrust-analyzer cargo qrcode 0.14.1 tests/test_annex_i_micro_qr().() { |
| 301 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().(b"01234567", Versionrust-analyzer cargo qrcode 0.14.1 types/Version#::Microrust-analyzer cargo qrcode 0.14.1 types/Version#Micro#(2), EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Lrust-analyzer cargo qrcode 0.14.1 types/EcLevel#L#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 302 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 303 | &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Deref]deref().code.to_debug_strrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]to_debug_str().('#', '.'), |
| 304 | "\ |
| 305 | #######.#.#.#\n\ |
| 306 | #.....#.###.#\n\ |
| 307 | #.###.#..##.#\n\ |
| 308 | #.###.#..####\n\ |
| 309 | #.###.#.###..\n\ |
| 310 | #.....#.#...#\n\ |
| 311 | #######..####\n\ |
| 312 | .........##..\n\ |
| 313 | ##.#....#...#\n\ |
| 314 | .##.#.#.#.#.#\n\ |
| 315 | ###..#######.\n\ |
| 316 | ...#.#....##.\n\ |
| 317 | ###.#..##.###" |
| 318 | ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[cfg(all(test, feature = "image"))] |
| 323 | mod image_testsrust-analyzer cargo qrcode 0.14.1 image_tests/ { |
| 324 | use craterust-analyzer cargo qrcode 0.14.1 crate/::{EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#, Versionrust-analyzer cargo qrcode 0.14.1 types/Version#}; |
| 325 | use imagerust-analyzer cargo image 0.25.0 crate/::{load_from_memoryrust-analyzer cargo image 0.25.0 dynimage/load_from_memory()., Lumarust-analyzer cargo image 0.25.0 color/Luma#, Rgbrust-analyzer cargo image 0.25.0 color/Rgb#}; |
| 326 | |
| 327 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 328 | fn test_annex_i_qr_as_imagerust-analyzer cargo qrcode 0.14.1 image_tests/test_annex_i_qr_as_image().() { |
| 329 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::newrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]new().(b"01234567").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 330 | let image = code.renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().::<Lumarust-analyzer cargo image 0.25.0 color/Luma#<u8>>().buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 331 | let expected = load_from_memoryrust-analyzer cargo image 0.25.0 dynimage/load_from_memory().(include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("test_annex_i_qr_as_image.png")).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_luma8rust-analyzer cargo image 0.25.0 dynimage/impl#[DynamicImage]into_luma8().(); |
| 332 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().(), expected.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().()); |
| 333 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().()); |
| 334 | } |
| 335 | |
| 336 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 337 | fn test_annex_i_micro_qr_as_imagerust-analyzer cargo qrcode 0.14.1 image_tests/test_annex_i_micro_qr_as_image().() { |
| 338 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().(b"01234567", Versionrust-analyzer cargo qrcode 0.14.1 types/Version#::Microrust-analyzer cargo qrcode 0.14.1 types/Version#Micro#(2), EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Lrust-analyzer cargo qrcode 0.14.1 types/EcLevel#L#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 339 | let image = code |
| 340 | .renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().() |
| 341 | .min_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]min_dimensions().(200, 200) |
| 342 | .dark_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]dark_color().(Rgbrust-analyzer cargo image 0.25.0 color/Rgb#([128, 0, 0])) |
| 343 | .light_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]light_color().(Rgbrust-analyzer cargo image 0.25.0 color/Rgb#([255, 255, 128])) |
| 344 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 345 | let expected = load_from_memoryrust-analyzer cargo image 0.25.0 dynimage/load_from_memory().(include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("test_annex_i_micro_qr_as_image.png")).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().into_rgb8rust-analyzer cargo image 0.25.0 dynimage/impl#[DynamicImage]into_rgb8().(); |
| 346 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().(), expected.dimensionsrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]dimensions().()); |
| 347 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(image.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().(), expected.into_rawrust-analyzer cargo image 0.25.0 buffer_/impl#[`ImageBuffer<P, Container>`]into_raw().()); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | #[cfg(all(test, feature = "svg"))] |
| 352 | mod svg_testsrust-analyzer cargo qrcode 0.14.1 svg_tests/ { |
| 353 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::svgrust-analyzer cargo qrcode 0.14.1 render/svg/::Colorrust-analyzer cargo qrcode 0.14.1 render/svg/Color# as SvgColorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#; |
| 354 | use craterust-analyzer cargo qrcode 0.14.1 crate/::{EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#, Versionrust-analyzer cargo qrcode 0.14.1 types/Version#}; |
| 355 | |
| 356 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 357 | fn test_annex_i_qr_as_svgrust-analyzer cargo qrcode 0.14.1 svg_tests/test_annex_i_qr_as_svg().() { |
| 358 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::newrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]new().(b"01234567").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 359 | let image = code.renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().::<SvgColorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#>().buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 360 | let expected = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("test_annex_i_qr_as_svg.svg"); |
| 361 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&image, expected); |
| 362 | } |
| 363 | |
| 364 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 365 | fn test_annex_i_micro_qr_as_svgrust-analyzer cargo qrcode 0.14.1 svg_tests/test_annex_i_micro_qr_as_svg().() { |
| 366 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().(b"01234567", Versionrust-analyzer cargo qrcode 0.14.1 types/Version#::Microrust-analyzer cargo qrcode 0.14.1 types/Version#Micro#(2), EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Lrust-analyzer cargo qrcode 0.14.1 types/EcLevel#L#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 367 | let image = code |
| 368 | .renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().() |
| 369 | .min_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]min_dimensions().(200, 200) |
| 370 | .dark_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]dark_color().(SvgColorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#("#800000")) |
| 371 | .light_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]light_color().(SvgColorrust-analyzer cargo qrcode 0.14.1 render/svg/Color#("#ffff80")) |
| 372 | .buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 373 | let expected = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("test_annex_i_micro_qr_as_svg.svg"); |
| 374 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&image, expected); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | #[cfg(all(test, feature = "pic"))] |
| 379 | mod pic_testsrust-analyzer cargo qrcode 0.14.1 pic_tests/ { |
| 380 | use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::picrust-analyzer cargo qrcode 0.14.1 render/pic/::Colorrust-analyzer cargo qrcode 0.14.1 render/pic/Color# as PicColorrust-analyzer cargo qrcode 0.14.1 render/pic/Color#; |
| 381 | use craterust-analyzer cargo qrcode 0.14.1 crate/::{EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#, QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#, Versionrust-analyzer cargo qrcode 0.14.1 types/Version#}; |
| 382 | |
| 383 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 384 | fn test_annex_i_qr_as_picrust-analyzer cargo qrcode 0.14.1 pic_tests/test_annex_i_qr_as_pic().() { |
| 385 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::newrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]new().(b"01234567").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 386 | let image = code.renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().::<PicColorrust-analyzer cargo qrcode 0.14.1 render/pic/Color#>().buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 387 | let expected = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("test_annex_i_qr_as_pic.pic"); |
| 388 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&image, expected); |
| 389 | } |
| 390 | |
| 391 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 392 | fn test_annex_i_micro_qr_as_picrust-analyzer cargo qrcode 0.14.1 pic_tests/test_annex_i_micro_qr_as_pic().() { |
| 393 | let code = QrCoderust-analyzer cargo qrcode 0.14.1 QrCode#::with_versionrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]with_version().(b"01234567", Versionrust-analyzer cargo qrcode 0.14.1 types/Version#::Microrust-analyzer cargo qrcode 0.14.1 types/Version#Micro#(2), EcLevelrust-analyzer cargo qrcode 0.14.1 types/EcLevel#::Lrust-analyzer cargo qrcode 0.14.1 types/EcLevel#L#).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 394 | let image = code.renderrust-analyzer cargo qrcode 0.14.1 impl#[QrCode]render().::<PicColorrust-analyzer cargo qrcode 0.14.1 render/pic/Color#>().min_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]min_dimensions().(1, 1).buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().(); |
| 395 | let expected = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("test_annex_i_micro_qr_as_pic.pic"); |
| 396 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&image, expected); |
| 397 | } |
| 398 | } |