use windows::core::{
Result,
HSTRING,
Interface as _,
};
use windows::Win32::UI::Shell::{
IInitializeWithWindow,
};
use windows::Graphics::Imaging::{
BitmapDecoder,
};
use windows::Media::Ocr::{
OcrEngine,
};
use windows::Storage::{
FileAccessMode,
};
use windows::Storage::Pickers::{
FileOpenPicker,
};
async fn exec_ocr() -> Result<String> {
let picker = FileOpenPicker::new()?;
picker.FileTypeFilter()?.Append(HSTRING::from(".png"))?;
let file = {
let dummy_window = windows_async::create_dummy_window();
unsafe { picker.cast::<IInitializeWithWindow>()?.Initialize(dummy_window.hwnd())?; }
picker.PickSingleFileAsync()?.await?
};
let bmp = BitmapDecoder::CreateWithIdAsync(
BitmapDecoder::PngDecoderId()?,
file.OpenAsync(FileAccessMode::Read)?.await?
)?.await?;
let bmp = bmp.GetSoftwareBitmapAsync()?.await?;
let ocr:OcrEngine = OcrEngine::TryCreateFromUserProfileLanguages()?;
let ocr_result = ocr.RecognizeAsync(bmp)?.await?;
let text = String::from_utf16_lossy(ocr_result.Text()?.as_wide());
Ok(text)
}
fn main() {
env_logger::init();
match windows_async::block_on(exec_ocr()) {
Ok(s) => {
println!("OCR text: {}", s);
},
Err(e) => {
println!("error: {:?}", e);
}
}
}