apple_vision/detect_barcodes/
mod.rs1use 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
13pub type BarcodeSymbology = sdk::BarcodeSymbology;
15
16pub type BarcodeCompositeType = sdk::BarcodeCompositeType;
18
19#[derive(Debug, Clone, PartialEq)]
21pub struct DetectedBarcode {
22 pub payload: String,
25 pub symbology: String,
28 pub confidence: f32,
30 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
41pub 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 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 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 let raw = unsafe { &*typed.add(i) };
79 let payload = if raw.payload.is_null() {
80 String::new()
81 } else {
82 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 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 unsafe { ffi::vn_detected_barcodes_free(array, count) };
109 Ok(out)
110}