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