Skip to main content

apple_vision/detect_barcodes/
mod.rs

1//! Barcode detection via `VNDetectBarcodesRequest` (Vision v0.4).
2
3use core::ffi::{c_char, c_void};
4use core::ptr;
5use std::ffi::CString;
6use std::path::Path;
7
8use crate::error::{from_swift, VisionError};
9use crate::ffi;
10use crate::recognize_text::BoundingBox;
11
12/// One detected barcode.
13#[derive(Debug, Clone, PartialEq)]
14pub struct DetectedBarcode {
15    /// The decoded payload (URL, text, contact data, …). May be empty
16    /// if Vision couldn't decode it.
17    pub payload: String,
18    /// Apple's symbology identifier (e.g. `"VNBarcodeSymbologyQR"`,
19    /// `"VNBarcodeSymbologyEAN13"`, `"VNBarcodeSymbologyAztec"`).
20    pub symbology: String,
21    /// Confidence in `0.0..=1.0`.
22    pub confidence: f32,
23    /// Normalised bounding box (origin at bottom-left, Vision convention).
24    pub bounding_box: BoundingBox,
25}
26
27/// Detect every barcode in the image at `path`.
28///
29/// Supports QR, EAN-13/8, UPC-A/E, Code 128/39/93, Aztec, PDF417, `DataMatrix`, …
30/// (the full Vision symbology set).
31///
32/// # Errors
33///
34/// See [`VisionError`].
35pub fn detect_barcodes_in_path(path: impl AsRef<Path>) -> Result<Vec<DetectedBarcode>, VisionError> {
36    let path_str = path
37        .as_ref()
38        .to_str()
39        .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
40    let path_c = CString::new(path_str)
41        .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
42
43    let mut array: *mut c_void = ptr::null_mut();
44    let mut count: usize = 0;
45    let mut err_msg: *mut c_char = ptr::null_mut();
46
47    let status = unsafe {
48        ffi::vn_detect_barcodes_in_path(path_c.as_ptr(), &mut array, &mut count, &mut err_msg)
49    };
50    if status != ffi::status::OK {
51        return Err(unsafe { from_swift(status, err_msg) });
52    }
53    if array.is_null() || count == 0 {
54        return Ok(Vec::new());
55    }
56    let typed = array.cast::<ffi::DetectedBarcodeRaw>();
57    let mut out = Vec::with_capacity(count);
58    for i in 0..count {
59        let raw = unsafe { &*typed.add(i) };
60        let payload = if raw.payload.is_null() {
61            String::new()
62        } else {
63            unsafe { core::ffi::CStr::from_ptr(raw.payload) }
64                .to_string_lossy()
65                .into_owned()
66        };
67        let symbology = if raw.symbology.is_null() {
68            String::new()
69        } else {
70            unsafe { core::ffi::CStr::from_ptr(raw.symbology) }
71                .to_string_lossy()
72                .into_owned()
73        };
74        out.push(DetectedBarcode {
75            payload,
76            symbology,
77            confidence: raw.confidence,
78            bounding_box: BoundingBox {
79                x: raw.bbox_x,
80                y: raw.bbox_y,
81                width: raw.bbox_w,
82                height: raw.bbox_h,
83            },
84        });
85    }
86    unsafe { ffi::vn_detected_barcodes_free(array, count) };
87    Ok(out)
88}