visionkit-rs
Safe Rust bindings for Apple's VisionKit.framework on macOS.
Status: wraps the macOS
ImageAnalyzer,ImageAnalysis, andImageAnalysisOverlayViewsurface; four rows are partial (seeCOVERAGE.md). The iOS-only areas are availability metadata only.
Requirements
Build a macOS binary that uses this crate with a deployment target of macOS 13.0 or later. The
Swift bridge uses Swift concurrency. Rust builds binaries for macOS 11.0 on Apple silicon and for
macOS 10.12 on x86_64 by default. With these targets, the binary loads the Swift runtime
through @rpath and stops at launch with an error such as
Library not loaded: @rpath/libswift_Concurrency.dylib. Set MACOSX_DEPLOYMENT_TARGET=13.0 in
the environment, or set it for the project in .cargo/config.toml:
[]
= "13.0"
Cargo does not link the binary again when only this variable changes, so run cargo clean
after you set it.
Installation
[]
= "0.4"
The library is imported as visionkit. Optional features:
async: executor-agnostic futures invisionkit::async_api.apple-cf: analyze in-memoryapple_cf::cg::CGImageandapple_cf::cv::CVPixelBuffervalues.
= { = "0.4", = ["async", "apple-cf"] }
Quick start
use *;
Async API (async feature)
Enable with visionkit-rs = { version = "0.4", features = ["async"] }.
use ;
use ;
Note: VisionKit completes image analysis through the main queue. On the main thread block_on pumps the Obj-C main run loop until the future is woken; on other threads it parks, and the futures only complete while the main thread runs its run loop (an AppKit app, or a CLI whose main thread calls CFRunLoopRun). The futures also work with any other executor (Tokio, async-std, …) under the same condition.
Highlights
ImageAnalyzer::is_supportedandsupported_text_recognition_languagesImageAnalyzerConfiguration,ImageAnalysisTypes, andImageOrientation- File-path based analysis through every public macOS analyzer overload: URL,
NSImage,CGImage,CIImage, andCVPixelBuffer - In-memory
CGImageandCVPixelBufferanalysis (ImageAnalyzer::analyze_cg_image,analyze_pixel_buffer, and their async counterparts) with theapple-cffeature ImageAnalysis::transcriptandhas_resultsLiveTextInteractioncoverage for macOSImageAnalysisOverlayView, including delegates, menu tags, selection metadata, tracking/content views, fonts, and subject analysis. Delegate calls are answered from a configuration you set and recorded as events you poll; they do not call back into Rust.VNDocumentCameraViewController,DataScannerViewController,RecognizedText,Barcode, andRecognizedItemavailability metadata on macOS. These iOS-only APIs are not wrapped: their constructors always fail on macOS.
Threading
- VisionKit finishes
ImageAnalyzerwork on the main queue, although its Swift API is not bound to the main actor. A synchronous analysis on the main thread pumps the main run loop while it waits. On any other thread it needs the main thread to run its run loop (an AppKit app, or a CLI whose main thread callsCFRunLoopRun). If the main queue is not serviced within 10 seconds, the call returnsVisionKitError::MainRunLoopNotRunning. Any analysis times out after 60 seconds withVisionKitError::TimedOut. #[tokio::main]and other executors that block the main thread starve VisionKit. Run the executor on another thread and keep the main thread in its run loop, or call the synchronous API from the main thread.LiveTextInteractionand its companion types wrap main-actor AppKit views and must be created on the main thread: their constructors returnVisionKitError::NotOnMainThreadelsewhere. The types are notSend, so they stay on the main thread, and every call runs inline there. Subject queries pump the main run loop while they wait and time out after 10 seconds.
Availability
ImageAnalyzer,ImageAnalysis, andLiveTextInteractionare available on macOS 13+.LiveTextInteraction::text,selected_text,selected_attributed_text,selected_ranges, andLiveTextMenuTagrequire macOS 14+ because Apple introduced those overlay accessors after macOS 13.VNDocumentCameraViewController,DataScannerViewController,RecognizedText,Barcode, andRecognizedItemare iOS-only Apple APIs. On macOS, this crate exposes them as structured availability metadata instead of pretending they exist.
Examples
See COVERAGE.md for the audited VisionKit API matrix.
License
Licensed under either of Apache-2.0 or MIT at your option.