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;
11use crate::sdk;
12
13/// Public alias for `VNBarcodeSymbology`.
14pub type BarcodeSymbology = sdk::BarcodeSymbology;
15
16/// Public alias for `VNBarcodeCompositeType`.
17pub type BarcodeCompositeType = sdk::BarcodeCompositeType;
18
19/// One detected barcode.
20#[derive(Debug, Clone, PartialEq)]
21pub struct DetectedBarcode {
22    /// The decoded payload (URL, text, contact data, …). May be empty
23    /// if Vision couldn't decode it.
24    pub payload: String,
25    /// Apple's symbology identifier (e.g. `"VNBarcodeSymbologyQR"`,
26    /// `"VNBarcodeSymbologyEAN13"`, `"VNBarcodeSymbologyAztec"`).
27    pub symbology: String,
28    /// Confidence in `0.0..=1.0`.
29    pub confidence: f32,
30    /// Normalised bounding box (origin at bottom-left, Vision convention).
31    pub bounding_box: BoundingBox,
32}
33
34impl DetectedBarcode {
35    #[must_use]
36    pub fn symbology_kind(&self) -> Option<BarcodeSymbology> {
37        BarcodeSymbology::from_str(&self.symbology)
38    }
39}
40
41/// Detect every barcode in the image at `path`.
42///
43/// Supports QR, EAN-13/8, UPC-A/E, Code 128/39/93, Aztec, PDF417, `DataMatrix`, …
44/// (the full Vision symbology set).
45///
46/// # Errors
47///
48/// See [`VisionError`].
49pub fn detect_barcodes_in_path(
50    path: impl AsRef<Path>,
51) -> Result<Vec<DetectedBarcode>, VisionError> {
52    let path_str = path
53        .as_ref()
54        .to_str()
55        .ok_or_else(|| VisionError::InvalidArgument("non-UTF-8 path".into()))?;
56    let path_c = CString::new(path_str)
57        .map_err(|e| VisionError::InvalidArgument(format!("path NUL byte: {e}")))?;
58
59    let mut array: *mut c_void = ptr::null_mut();
60    let mut count: usize = 0;
61    let mut err_msg: *mut c_char = ptr::null_mut();
62
63    // SAFETY: all pointer arguments are valid stack locations or null-initialised out-params; strings are valid C strings for the duration of the call.
64    let status = unsafe {
65        ffi::vn_detect_barcodes_in_path(path_c.as_ptr(), &mut array, &mut count, &mut err_msg)
66    };
67    if status != ffi::status::OK {
68        // SAFETY: the error pointer is either null or a bridge-allocated C string; `from_swift` frees it.
69        return Err(unsafe { from_swift(status, err_msg) });
70    }
71    if array.is_null() || count == 0 {
72        return Ok(Vec::new());
73    }
74    let typed = array.cast::<ffi::DetectedBarcodeRaw>();
75    let mut out = Vec::with_capacity(count);
76    for i in 0..count {
77        // SAFETY: the pointer is valid for the reported element count; the index is in bounds.
78        let raw = unsafe { &*typed.add(i) };
79        let payload = if raw.payload.is_null() {
80            String::new()
81        } else {
82            // SAFETY: the C string pointer is non-null (checked above) and valid for the duration of this borrow.
83            unsafe { core::ffi::CStr::from_ptr(raw.payload) }
84                .to_string_lossy()
85                .into_owned()
86        };
87        let symbology = if raw.symbology.is_null() {
88            String::new()
89        } else {
90            // SAFETY: the C string pointer is non-null (checked above) and valid for the duration of this borrow.
91            unsafe { core::ffi::CStr::from_ptr(raw.symbology) }
92                .to_string_lossy()
93                .into_owned()
94        };
95        out.push(DetectedBarcode {
96            payload,
97            symbology,
98            confidence: raw.confidence,
99            bounding_box: BoundingBox {
100                x: raw.bbox_x,
101                y: raw.bbox_y,
102                width: raw.bbox_w,
103                height: raw.bbox_h,
104            },
105        });
106    }
107    // SAFETY: the pointer/count pair was allocated by the bridge and is freed exactly once here.
108    unsafe { ffi::vn_detected_barcodes_free(array, count) };
109    Ok(out)
110}