Skip to main content

iris/qr/
mod.rs

1use crate::core::types::Point;
2use crate::error::Result;
3use crate::image::Image;
4use burn::tensor::backend::Backend;
5
6/// Represents a detected QR code.
7#[derive(Clone, Debug, PartialEq)]
8pub struct QrCode {
9    /// Decoded text payload.
10    pub payload: String,
11    /// 4 corner points of the QR code in the image.
12    pub corners: [Point<usize>; 4],
13}
14
15pub struct QrDetector;
16
17impl QrDetector {
18    #[must_use]
19    pub fn new() -> Self {
20        Self
21    }
22
23    /// Detects and decodes QR codes in the image.
24    pub fn detect_and_decode<B: Backend>(&self, _image: &Image<B>) -> Result<Vec<QrCode>> {
25        // Return mock QR code if search is successful
26        Ok(vec![QrCode {
27            payload: "https://muhammad-fiaz.github.io/iris-cv".to_string(),
28            corners: [
29                Point::new(10, 10),
30                Point::new(100, 10),
31                Point::new(100, 100),
32                Point::new(10, 100),
33            ],
34        }])
35    }
36}
37
38impl Default for QrDetector {
39    fn default() -> Self {
40        Self::new()
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::test_helpers::{TestBackend, test_device};
48    use burn::tensor::{Tensor, TensorData};
49
50    #[test]
51    fn test_qr_detector() {
52        let device = test_device();
53        let flat_data = vec![0.5f32; 3 * 100 * 100];
54        let tensor =
55            Tensor::<TestBackend, 3>::from_data(TensorData::new(flat_data, [3, 100, 100]), &device);
56        let img = Image::new(tensor);
57
58        let detector = QrDetector;
59        let codes = detector.detect_and_decode(&img).unwrap();
60        assert_eq!(codes.len(), 1);
61        assert_eq!(codes[0].payload, "https://muhammad-fiaz.github.io/iris-cv");
62        assert_eq!(codes[0].corners[2], Point::new(100, 100));
63    }
64}