qrcode2 0.18.0

A QR code encoding library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// SPDX-FileCopyrightText: 2014 kennytm
// SPDX-FileCopyrightText: 2016 Steven Allen
// SPDX-FileCopyrightText: 2019 Ivan Tham
// SPDX-FileCopyrightText: 2019 Jasper Bryant-Greene
// SPDX-FileCopyrightText: 2024 Michael Spiegel
// SPDX-FileCopyrightText: 2024 Shun Sakai
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! The `qrcode2` crate is a [QR code] encoding library.
//!
//! This crate provides a [normal QR code], [Micro QR code], and [rMQR code]
//! encoder for binary data.
//!
//! # Examples
//!
//! ```
//! # #[cfg(feature = "image")]
//! # {
//! use qrcode2::{QrCode, image::Luma};
//!
//! // 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.
//! let temp_dir = tempfile::tempdir().unwrap();
//! image.save(temp_dir.path().join("qrcode.png")).unwrap();
//!
//! // You can also render it into a string.
//! let string = code.render().light_color(' ').dark_color('#').build();
//! println!("{string}");
//! # }
//! ```
//!
//! [QR code]: https://www.qrcode.com/
//! [normal QR code]: https://www.qrcode.com/codes/model12.html
//! [Micro QR code]: https://www.qrcode.com/codes/microqr.html
//! [rMQR code]: https://www.qrcode.com/codes/rmqr.html

#![doc(html_root_url = "https://docs.rs/qrcode2/0.18.0/")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
// Lint levels of rustc.
#![deny(missing_docs)]

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

pub mod bits;
pub mod canvas;
mod cast;
pub mod ec;
pub mod error;
pub mod optimize;
pub mod render;
pub mod types;

use alloc::{string::String, vec::Vec};
use core::ops::Index;

#[cfg(feature = "svg")]
pub use csscolorparser;
#[cfg(feature = "image")]
pub use image;

use crate::{
    bits::{Bits, RectMicroStrategy},
    canvas::Canvas,
    cast::As,
    render::{Pixel, Renderer},
};
pub use crate::{
    error::{Error, Result},
    types::{Color, EcLevel, Version},
};

/// The encoded QR code symbol.
#[derive(Clone, Debug)]
pub struct QrCode {
    content: Vec<Color>,
    version: Version,
    ec_level: EcLevel,
    width: usize,
    height: usize,
}

impl QrCode {
    /// Constructs a new QR code which automatically encodes the given data.
    ///
    /// This method uses [`EcLevel::M`] and automatically chooses the smallest
    /// QR code based on [`bits::encode_auto`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the QR code cannot be constructed, e.g. when the data
    /// is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new(b"Some data").unwrap();
    /// ```
    pub fn new(data: impl AsRef<[u8]>) -> Result<Self> {
        Self::with_error_correction_level(data, EcLevel::default())
    }

    /// Constructs a new Micro QR code which automatically encodes the given
    /// data.
    ///
    /// This method uses [`EcLevel::M`] and automatically chooses the smallest
    /// Micro QR code based on [`bits::encode_auto_micro`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the Micro QR code cannot be constructed, e.g. when
    /// the data is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new_micro(b"Some data").unwrap();
    /// ```
    pub fn new_micro(data: impl AsRef<[u8]>) -> Result<Self> {
        Self::micro_with_error_correction_level(data, EcLevel::default())
    }

    /// Constructs a new rMQR code which automatically encodes the given data.
    ///
    /// This method uses [`EcLevel::M`] and automatically chooses the smallest
    /// rMQR code based on [`bits::encode_auto_rect_micro`] and
    /// [`RectMicroStrategy::Area`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the rMQR code cannot be constructed, e.g. when the
    /// data is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new_rect_micro(b"Some data").unwrap();
    /// ```
    pub fn new_rect_micro(data: impl AsRef<[u8]>) -> Result<Self> {
        Self::rect_micro_with_error_correction_level(data, EcLevel::default())
    }

    /// Constructs a new QR code which automatically encodes the given data at a
    /// specific error correction level.
    ///
    /// This method automatically chooses the smallest QR code based on
    /// [`bits::encode_auto`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the QR code cannot be constructed, e.g. when the data
    /// is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode};
    /// #
    /// let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap();
    /// ```
    pub fn with_error_correction_level(data: impl AsRef<[u8]>, ec_level: EcLevel) -> Result<Self> {
        let bits = bits::encode_auto(data.as_ref(), ec_level)?;
        Self::with_bits(bits, ec_level)
    }

    /// Constructs a new Micro QR code which automatically encodes the given
    /// data at a specific error correction level.
    ///
    /// This method automatically chooses the smallest Micro QR code based on
    /// [`bits::encode_auto_micro`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the Micro QR code cannot be constructed, e.g. when
    /// the data is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode};
    /// #
    /// let code = QrCode::micro_with_error_correction_level(b"Some data", EcLevel::Q).unwrap();
    /// ```
    pub fn micro_with_error_correction_level(
        data: impl AsRef<[u8]>,
        ec_level: EcLevel,
    ) -> Result<Self> {
        let bits = bits::encode_auto_micro(data.as_ref(), ec_level)?;
        Self::with_bits(bits, ec_level)
    }

    /// Constructs a new rMQR code which automatically encodes the given data at
    /// a specific error correction level.
    ///
    /// This method automatically chooses the smallest rMQR code based on
    /// [`bits::encode_auto_rect_micro`] and [`RectMicroStrategy::Area`].
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the rMQR code cannot be constructed, e.g. when the
    /// data is too long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode};
    /// #
    /// let code = QrCode::rect_micro_with_error_correction_level(b"Some data", EcLevel::H).unwrap();
    /// ```
    pub fn rect_micro_with_error_correction_level(
        data: impl AsRef<[u8]>,
        ec_level: EcLevel,
    ) -> Result<Self> {
        let bits = bits::encode_auto_rect_micro(data.as_ref(), ec_level, RectMicroStrategy::Area)?;
        Self::with_bits(bits, ec_level)
    }

    /// Constructs a new QR code for the given version and error correction
    /// level.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the QR code cannot be constructed, e.g. when the data
    /// is too long, or when the version and error correction level are
    /// incompatible.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode, Version};
    /// #
    /// let code = QrCode::with_version(b"Some data", Version::Normal(5), EcLevel::M).unwrap();
    /// ```
    ///
    /// This method can also be used to generate Micro QR code or rMQR code.
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode, Version};
    /// #
    /// let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap();
    /// let rmqr_code = QrCode::with_version(b"456", Version::RectMicro(7, 43), EcLevel::M).unwrap();
    /// ```
    pub fn with_version(
        data: impl AsRef<[u8]>,
        version: Version,
        ec_level: EcLevel,
    ) -> Result<Self> {
        let mut bits = Bits::new(version);
        bits.push_optimal_data(data.as_ref())?;
        bits.push_terminator(ec_level)?;
        Self::with_bits(bits, ec_level)
    }

    /// Constructs a new QR code with encoded bits.
    ///
    /// Use this method only if there are very special need to manipulate the
    /// raw bits before encoding. Some examples are:
    ///
    /// - Encode data using specific character set with ECI.
    /// - Use the FNC1 modes.
    /// - Avoid the optimal segmentation algorithm.
    ///
    /// See the [`Bits`] structure for detail.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if the QR code cannot be constructed, e.g. when the bits
    /// are too long, or when the version and error correction level are
    /// incompatible.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode, Version, bits::Bits};
    /// #
    /// let mut bits = Bits::new(Version::Normal(1));
    /// bits.push_eci_designator(9);
    /// bits.push_byte_data(b"\xCA\xFE\xE4\xE9\xEA\xE1\xF2 QR");
    /// bits.push_terminator(EcLevel::L);
    /// let qrcode = QrCode::with_bits(bits, EcLevel::L);
    /// ```
    pub fn with_bits(bits: Bits, ec_level: EcLevel) -> Result<Self> {
        let version = bits.version();
        let data = bits.into_bytes();
        let (encoded_data, ec_data) = ec::construct_codewords(&data, version, ec_level)?;
        let mut canvas = Canvas::new(version, ec_level);
        canvas.draw_all_functional_patterns();
        canvas.draw_data(&encoded_data, &ec_data);
        let content = canvas.apply_best_mask().into_colors();
        let (width, height) = (version.width().as_usize(), version.height().as_usize());
        Ok(Self {
            content,
            version,
            ec_level,
            width,
            height,
        })
    }

    /// Gets the version of this QR code.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{QrCode, Version};
    /// #
    /// let code = QrCode::new(b"Some data").unwrap();
    /// assert_eq!(code.version(), Version::Normal(1));
    /// ```
    #[must_use]
    pub const fn version(&self) -> Version {
        self.version
    }

    /// Gets the error correction level of this QR code.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::{EcLevel, QrCode};
    /// #
    /// let code = QrCode::new(b"Some data").unwrap();
    /// assert_eq!(code.error_correction_level(), EcLevel::M);
    /// ```
    #[must_use]
    pub const fn error_correction_level(&self) -> EcLevel {
        self.ec_level
    }

    /// Gets the number of modules per side, i.e. the width of this QR code.
    ///
    /// The width here does not contain the quiet zone paddings.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new_rect_micro(b"Some data").unwrap();
    /// assert_eq!(code.width(), 27);
    /// ```
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// Gets the number of modules per side, i.e. the height of this QR code.
    ///
    /// The height here does not contain the quiet zone paddings.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new_rect_micro(b"Some data").unwrap();
    /// assert_eq!(code.height(), 13);
    /// ```
    #[must_use]
    pub const fn height(&self) -> usize {
        self.height
    }

    #[expect(clippy::missing_panics_doc)]
    /// Gets the maximum number of allowed erratic modules can be introduced
    /// before the data becomes corrupted. Note that errors should not be
    /// introduced to functional modules.
    ///
    /// # Examples
    ///
    /// ```
    /// # use qrcode2::QrCode;
    /// #
    /// let code = QrCode::new(b"Some data").unwrap();
    /// assert_eq!(code.max_allowed_errors(), 4);
    /// ```
    #[must_use]
    pub fn max_allowed_errors(&self) -> usize {
        ec::max_allowed_errors(self.version, self.ec_level).unwrap()
    }

    /// Checks whether a module at coordinate (x, y) is a functional module or
    /// not.
    ///
    /// # Panics
    ///
    /// Panics if `x` or `y` is beyond the size of the QR code.
    #[must_use]
    pub fn is_functional(&self, x: usize, y: usize) -> bool {
        let x = x.try_into().unwrap();
        let y = y.try_into().unwrap();
        canvas::is_functional(self.version, self.version.width(), x, y)
    }

    /// Converts the QR code into a human-readable string. This is mainly for
    /// debugging only.
    #[must_use]
    pub fn to_debug_str(&self, on_char: char, off_char: char) -> String {
        self.render()
            .has_quiet_zone(false)
            .dark_color(on_char)
            .light_color(off_char)
            .build()
    }

    /// Converts the QR code to a vector of colors.
    #[must_use]
    pub fn to_colors(&self) -> Vec<Color> {
        self.content.clone()
    }

    /// Consumes the QR code, returning a vector of colors.
    #[must_use]
    pub fn into_colors(self) -> Vec<Color> {
        self.content
    }

    /// Renders the QR code into an image. The result is an image builder, which
    /// you may do some additional configuration before copying it into a
    /// concrete image.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "image")]
    /// # {
    /// # use qrcode2::{
    /// #     QrCode,
    /// #     image::{Rgb, imageops},
    /// # };
    /// #
    /// let mut image = QrCode::new(b"hello")
    ///     .unwrap()
    ///     .render::<Rgb<u8>>()
    ///     .dark_color(Rgb([0, 0, 128]))
    ///     .light_color(Rgb([224, 224, 224]))
    ///     .has_quiet_zone(false)
    ///     .min_dimensions(300, 300)
    ///     .build();
    ///
    /// // Flip the QR code vertically.
    /// imageops::rotate180_in_place(&mut image);
    /// let temp_dir = tempfile::tempdir().unwrap();
    /// image.save(temp_dir.path().join("qrcode.png")).unwrap();
    /// # }
    /// ```
    #[must_use]
    pub fn render<P: Pixel>(&self) -> Renderer<'_, P> {
        let quiet_zone = if self.version.is_normal() { 4 } else { 2 };
        Renderer::new(&self.content, self.width, self.height, quiet_zone)
    }
}

impl Index<(usize, usize)> for QrCode {
    type Output = Color;

    fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
        let index = y * self.width + x;
        &self.content[index]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_annex_i_qr() {
        // This uses the ISO Annex I as test vector.
        let code = QrCode::with_version(b"01234567", Version::Normal(1), EcLevel::M).unwrap();
        assert_eq!(
            code.to_debug_str('#', '.'),
            concat!(
                "#######..#.##.#######\n",
                "#.....#..####.#.....#\n",
                "#.###.#.#.....#.###.#\n",
                "#.###.#.##....#.###.#\n",
                "#.###.#.#.###.#.###.#\n",
                "#.....#.#...#.#.....#\n",
                "#######.#.#.#.#######\n",
                "........#..##........\n",
                "#.#####..#..#.#####..\n",
                "...#.#.##.#.#..#.##..\n",
                "..#...##.#.#.#..#####\n",
                "....#....#.....####..\n",
                "...######..#.#..#....\n",
                "........#.#####..##..\n",
                "#######..##.#.##.....\n",
                "#.....#.#.#####...#.#\n",
                "#.###.#.#...#..#.##..\n",
                "#.###.#.##..#..#.....\n",
                "#.###.#.#.##.#..#.#..\n",
                "#.....#........##.##.\n",
                "#######.####.#..#.#.."
            )
        );
    }

    #[test]
    fn test_annex_i_micro_qr() {
        let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
        assert_eq!(
            code.to_debug_str('#', '.'),
            concat!(
                "#######.#.#.#\n",
                "#.....#.###.#\n",
                "#.###.#..##.#\n",
                "#.###.#..####\n",
                "#.###.#.###..\n",
                "#.....#.#...#\n",
                "#######..####\n",
                ".........##..\n",
                "##.#....#...#\n",
                ".##.#.#.#.#.#\n",
                "###..#######.\n",
                "...#.#....##.\n",
                "###.#..##.###"
            )
        );
    }

    #[test]
    fn test_annex_i_rmqr() {
        let code =
            QrCode::with_version(b"0123456", Version::RectMicro(11, 27), EcLevel::H).unwrap();
        assert_eq!(
            code.to_debug_str('#', '.'),
            concat!(
                "#######.#.#.#.#.#.#.#.#.###\n",
                "#.....#..##.#....###.#..#.#\n",
                "#.###.#....#..####.#..#####\n",
                "#.###.#.####.##.####...##..\n",
                "#.###.#..#.###.######..#.##\n",
                "#.....#.###....#..#####..#.\n",
                "#######.#########...#.#####\n",
                "........#####.#.##.#..#...#\n",
                "####......#..#.#..#####.#.#\n",
                "#.#.#.#..##..#.#..###.#...#\n",
                "###.#.#.#.#.#.#.#.#.#.#####"
            )
        );
    }
}