Skip to main content

qrcode_decode/
lib.rs

1//! QR code decoding (scan-to-data).
2//!
3//! This module defines the [`QrDecoder`] trait and the value types a decoder
4//! returns ([`DecodedQrCode`]). The trait takes a borrowed grayscale view
5//! ([`GrayPixels`]) so it stays decoupled from the `image` crate: a decoder
6//! working from a camera frame or an embedded framebuffer can implement
7//! [`QrDecoder`] without pulling in `image`.
8//!
9//! Encoding is the primary mission of this crate; decoding is bridged via an
10//! opt-in adapter (see the `rqrr` feature). Implementing [`QrDecoder`]
11//! for your own decoder is always available.
12
13#![cfg_attr(not(feature = "std"), no_std)]
14#![deny(missing_docs)]
15
16extern crate alloc;
17
18#[cfg(not(feature = "std"))]
19#[allow(unused_imports)]
20use alloc::vec::Vec;
21
22use qrcode_core::{EcLevel, Version};
23
24#[cfg(feature = "rqrr")]
25pub mod rqrr;
26pub mod sa_parse;
27
28/// A borrowed grayscale (luma) pixel view: the universal input to a
29/// [`QrDecoder`].
30///
31/// One byte per pixel, row-major, `0` = black / `255` = white. Decoupled from
32/// the `image` crate so custom decoders need no image dependency.
33#[non_exhaustive]
34#[derive(Debug, Clone, Copy)]
35pub struct GrayPixels<'a> {
36    /// Image width in pixels.
37    width: u32,
38    /// Image height in pixels.
39    height: u32,
40    /// Row-major luma bytes (`len == width * height`).
41    data: &'a [u8],
42}
43
44impl<'a> GrayPixels<'a> {
45    /// Creates a view over `data` (which must hold `width * height` bytes).
46    #[must_use]
47    pub fn new(width: u32, height: u32, data: &'a [u8]) -> Self {
48        Self { width, height, data }
49    }
50
51    /// The image width in pixels.
52    #[must_use]
53    pub const fn width(&self) -> u32 {
54        self.width
55    }
56
57    /// The image height in pixels.
58    #[must_use]
59    pub const fn height(&self) -> u32 {
60        self.height
61    }
62
63    /// The luma byte at `(x, y)` (`0` = black, `255` = white).
64    ///
65    /// # Panics
66    ///
67    /// Panics if `(x, y)` is out of bounds.
68    #[must_use]
69    pub fn get(&self, x: u32, y: u32) -> u8 {
70        self.data[(y as usize) * (self.width as usize) + (x as usize)]
71    }
72}
73
74#[cfg(feature = "image")]
75impl<'a> From<&'a image::GrayImage> for GrayPixels<'a> {
76    fn from(img: &'a image::GrayImage) -> Self {
77        Self::new(img.width(), img.height(), img.as_raw())
78    }
79}
80
81/// A QR code recovered from an image by a [`QrDecoder`].
82///
83/// `#[non_exhaustive]`: fields may grow in 1.x (e.g. encoding mode, mask,
84/// position) without a breaking change; construct via [`DecodedQrCode::new`]
85/// and read via the accessors.
86#[non_exhaustive]
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub struct DecodedQrCode {
89    /// The decoded payload bytes.
90    data: Vec<u8>,
91    /// The QR version.
92    version: Version,
93    /// The error-correction level.
94    ec_level: EcLevel,
95}
96
97impl DecodedQrCode {
98    /// Creates a decoded QR code from its recovered fields.
99    #[must_use]
100    pub fn new(data: Vec<u8>, version: Version, ec_level: EcLevel) -> Self {
101        Self { data, version, ec_level }
102    }
103
104    /// The decoded payload bytes.
105    #[must_use]
106    pub fn data(&self) -> &[u8] {
107        &self.data
108    }
109
110    /// The QR version.
111    #[must_use]
112    pub fn version(&self) -> Version {
113        self.version
114    }
115
116    /// The error-correction level.
117    #[must_use]
118    pub fn ec_level(&self) -> EcLevel {
119        self.ec_level
120    }
121}
122
123/// A QR code decoder: turns a grayscale image back into data.
124///
125/// Implement this for your own decoder (e.g. wrapping a native binding); use
126/// the bundled `RqrrDecoder` (behind the `rqrr` feature) for the
127/// `rqrr` crate.
128///
129/// `decode` returns a [`Vec`] because an image may contain more than one QR
130/// code; the order is decoder-defined.
131pub trait QrDecoder {
132    /// The error type returned on failure.
133    type Error;
134
135    /// Decodes all QR codes found in `image`.
136    ///
137    /// # Errors
138    ///
139    /// Returns `Self::Error` if decoding fails.
140    fn decode(&self, image: GrayPixels<'_>) -> Result<Vec<DecodedQrCode>, Self::Error>;
141}