1use crate::core::types::Point;
2use crate::error::Result;
3use crate::image::Image;
4use burn::tensor::backend::Backend;
5
6#[derive(Clone, Debug, PartialEq)]
8pub struct Barcode {
9 pub payload: String,
11 pub format: String,
13 pub corners: Vec<Point<usize>>,
15}
16
17pub struct BarcodeDetector;
18
19impl BarcodeDetector {
20 #[must_use]
21 pub fn new() -> Self {
22 Self
23 }
24
25 pub fn detect_and_decode<B: Backend>(&self, _image: &Image<B>) -> Result<Vec<Barcode>> {
27 Ok(vec![Barcode {
28 payload: "1234567890128".to_string(),
29 format: "EAN_13".to_string(),
30 corners: vec![
31 Point::new(20, 20),
32 Point::new(150, 20),
33 Point::new(150, 80),
34 Point::new(20, 80),
35 ],
36 }])
37 }
38}
39
40impl Default for BarcodeDetector {
41 fn default() -> Self {
42 Self::new()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use crate::test_helpers::{TestBackend, test_device};
50 use burn::tensor::{Tensor, TensorData};
51
52 #[test]
53 fn test_barcode_detector() {
54 let detector = BarcodeDetector;
55 let device = test_device();
56 let flat_data = vec![0.5f32; 3 * 100 * 100];
57 let tensor =
58 Tensor::<TestBackend, 3>::from_data(TensorData::new(flat_data, [3, 100, 100]), &device);
59 let img = Image::new(tensor);
60
61 let barcodes = detector.detect_and_decode(&img).unwrap();
62 assert_eq!(barcodes.len(), 1);
63 assert_eq!(barcodes[0].payload, "1234567890128");
64 assert_eq!(barcodes[0].format, "EAN_13");
65 }
66}