rlvgl_core/plugins/
qrcode.rs

1//! Generate QR codes as pixel buffers.
2use crate::widget::Color;
3use alloc::vec::Vec;
4pub use qrcode::types::QrError;
5use qrcode::{QrCode, types::Color as QrColor};
6
7/// Generate a QR code bitmap for the provided data.
8pub fn generate(data: &[u8]) -> Result<(Vec<Color>, u32, u32), QrError> {
9    let code = QrCode::new(data)?;
10    let width = code.width() as u32;
11    let modules = code.into_colors();
12    let mut pixels = Vec::with_capacity((width * width) as usize);
13    for m in modules {
14        match m {
15            QrColor::Dark => pixels.push(Color(0, 0, 0, 255)),
16            QrColor::Light => pixels.push(Color(255, 255, 255, 255)),
17        }
18    }
19    Ok((pixels, width, width))
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25
26    #[test]
27    fn generate_simple_qr() {
28        let (pixels, w, h) = generate(b"hello").unwrap();
29        assert_eq!(w, h);
30        assert_eq!(pixels.len(), (w * h) as usize);
31        assert_eq!(pixels[0], Color(0, 0, 0, 255));
32    }
33}