这个接口最开始是给UWP程序用的。C++里需要用C++/WinRT方式调用。默认参数的识别率也不是很高的样子。只支持Win10+。
#include <winrt/Windows.Storage.h> #include <winrt/Windows.Storage.Streams.h> #include <winrt/Windows.Graphics.Imaging.h> #include <winrt/Windows.Media.Ocr.h> #include <winrt/Windows.Globalization.h> #include <winrt/Windows.Foundation.h> #include <iostream> #include <locale.h> using namespace winrt; using namespace Windows::Storage; using namespace Windows::Storage::Streams; using namespace Windows::Graphics::Imaging; using namespace Windows::Media::Ocr; using namespace Windows::Globalization; using namespace winrt::Windows::Foundation; void InitializeWinRT() { init_apartment(); } SoftwareBitmap LoadImage(const winrt::hstring& imagePath) { StorageFile imageFile = StorageFile::GetFileFromPathAsync(imagePath).get(); IRandomAccessStream fileStream = imageFile.OpenAsync(FileAccessMode::Read).get(); BitmapDecoder decoder = BitmapDecoder::CreateAsync(fileStream).get(); SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get(); return softwareBitmap; } winrt::hstring PerformOcr(const SoftwareBitmap& softwareBitmap) { winrt::hstring languageTag = L"zh-CN"; Windows::Globalization::Language lang(languageTag); OcrEngine ocrEngine = OcrEngine::TryCreateFromLanguage(lang); OcrResult ocrResult = ocrEngine.RecognizeAsync(softwareBitmap).get(); return ocrResult.Text(); } int main() { _wsetlocale(LC_ALL, L"zh-CN"); InitializeWinRT(); winrt::hstring imagePath = L"c:\\test.jpg"; SoftwareBitmap softwareBitmap = LoadImage(imagePath); winrt::hstring resultText = PerformOcr(softwareBitmap); std::wcout << L"识别结果:" << resultText.c_str() << std::endl; return 0; }
标签:include,Windows,Media,namespace,softwareBitmap,winrt,using,OCR From: https://www.cnblogs.com/z16166/p/17903030.html