'How to get a file by path in C++/WinRT + how to properly handle async calls without crashing?
I'm new to C++ and Windows and trying to write a WinRT/C++ app that can grab a file via path and run it through the built-in Media::Ocr model, but I can't see to even properly get the file into my application as a StorageFile. Here's how I've been trying to do it (to no avail):
runModel()
---------
IAsyncAction runModel() {
string _pth = "pic.png";
hstring pth = to_hstring(_pth);
StorageFile file = nullptr;
co_await LoadFileAsync(pth);
}
IAsyncAction LoadFileAsync (hstring pth) {
StorageFile file = co_await Windows::Storage::StorageFile::GetFileFromPathAsync(pth);
co_await LoadImageAsync(file);
}
IAsyncAction LoadImageAsync(StorageFile const& file) {
auto stream = co_await file.OpenAsync(Windows::Storage::FileAccessMode::Read);
auto decoder = co_await BitmapDecoder::CreateAsync(stream);
auto bitmap = co_await decoder.GetSoftwareBitmapAsync();
auto imgSource = WriteableBitmap(bitmap.PixelWidth(), bitmap.PixelHeight());
bitmap.CopyToBuffer(imgSource.PixelBuffer());
OcrEngine ocrEngine = nullptr;
ocrEngine = OcrEngine::TryCreateFromUserProfileLanguages();
auto ocrResult = co_await ocrEngine.RecognizeAsync(bitmap);
hstring hresult = ocrResult.Text();
std::cout << hresult.begin();
}
I'd appreciate any help of the following:
- Loading the file in as a StorageFile
- The right way to handle these async calls. I tried to follow https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/concurrency but I couldn't seem to get it to work without this error: !isStaThread()
- How to properly do the OCR
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
