use windows::{
    core::*,
    Graphics::Imaging::BitmapDecoder,
    Media::Ocr::OcrEngine,
    Storage::{FileAccessMode, StorageFile},
};

pub fn ocr(path:&str)->Result<String>{
    futures::executor::block_on(main_async(path))
}
async fn main_async(path:&str) -> Result<String> {
    let mut message = std::env::current_dir().unwrap();
    message.push(path);

    let file =
        StorageFile::GetFileFromPathAsync(&HSTRING::from(message.to_str().unwrap()))?.await?;
    let stream = file.OpenAsync(FileAccessMode::Read)?.await?;

    let decode = BitmapDecoder::CreateAsync(&stream)?.await?;
    let bitmap = decode.GetSoftwareBitmapAsync()?.await?;

    let engine = OcrEngine::TryCreateFromUserProfileLanguages()?;
    let result = engine.RecognizeAsync(&bitmap)?.await?;
    Ok(result.Text()?.to_string())

}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_works() {
        if let Ok(txt) = ocr("image.png"){
            println!("{}",txt);
        }
    }
}
