The OcrEngine is the primary entry point for performing Optical Character Recognition (OCR) in uniocr. You initialize it by specifying an OcrProvider and can optionally configure it with OcrOptions using a builder-like pattern.
Supported Providers
OcrProvider::Auto: Automatically selects the best available provider for the current platform (macOS uses Apple OCR, Windows uses Microsoft OCR, others use Tesseract).OcrProvider::MacOS: Uses Apple's native OCR (macOS only).OcrProvider::Windows: Uses Microsoft's native OCR (Windows only).OcrProvider::Tesseract: Uses the Tesseract engine.OcrProvider::Custom { credentials }: Uses a custom provider with provided Credentials.
Recognition Methods
recognize_image(&self, image: &DynamicImage): Performs OCR on an in-memory image::DynamicImage.recognize_file(&self, path: &str): Performs OCR on an image file at the specified path.recognize_batch(&self, paths: Vec<&str>): Performs OCR on a list of image file paths sequentially.
Returns a Result containing a tuple: (String, String, Option<f64>) representing (detected_text, language_code, confidence_score).
use uniocr::{OcrEngine, OcrProvider, OcrOptions};
use image::DynamicImage;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize engine with Auto provider and custom options
let engine = OcrEngine::new(OcrProvider::Auto)?
.with_options(OcrOptions::default()
.languages(vec![Language::English])
.confidence_threshold(0.8)
);
// Recognize an image file
let (text, lang, confidence) = engine.recognize_file("path/to/image.png").await?;
println!("Text: {}, Lang: {}, Confidence: {:?}", text, lang, confidence);
Ok(())
}