2024-01-08 23:37:00 +08:00
|
|
|
#include <winrt/Windows.Foundation.h>
|
|
|
|
#include <winrt/Windows.Storage.Pickers.h>
|
|
|
|
#include <winrt/Windows.Storage.Streams.h>
|
|
|
|
#include <winrt/Windows.Graphics.Imaging.h>
|
|
|
|
#include <winrt/Windows.Media.FaceAnalysis.h>
|
|
|
|
#include <winrt/Windows.Media.Ocr.h>
|
|
|
|
|
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
|
|
|
#include <winrt/Windows.Devices.Enumeration.h>
|
|
|
|
#include <winrt/Windows.Media.Devices.h>
|
|
|
|
|
|
|
|
#include <winrt/Windows.Security.Cryptography.h>
|
|
|
|
#include <winrt/Windows.Globalization.h>
|
|
|
|
using namespace winrt;
|
|
|
|
|
|
|
|
using namespace Windows::Foundation;
|
|
|
|
using namespace Windows::Storage;
|
|
|
|
using namespace Windows::Storage::Streams;
|
|
|
|
using namespace Windows::Graphics::Imaging;
|
|
|
|
using namespace Windows::Media::Ocr;
|
|
|
|
|
|
|
|
using namespace Windows::Devices::Enumeration;
|
|
|
|
using namespace Windows::Media::Devices;
|
|
|
|
|
|
|
|
using namespace Windows::Security::Cryptography;
|
|
|
|
using namespace Windows::Globalization;
|
|
|
|
using namespace Windows::Foundation::Collections;
|
2024-11-05 15:46:45 +08:00
|
|
|
DECLARE_API bool check_language_valid(wchar_t *language)
|
2024-04-02 15:36:52 +08:00
|
|
|
{
|
2024-01-08 23:37:00 +08:00
|
|
|
OcrEngine ocrEngine = OcrEngine::TryCreateFromUserProfileLanguages();
|
|
|
|
std::wstring l = language;
|
2024-04-02 15:36:52 +08:00
|
|
|
try
|
|
|
|
{
|
2024-01-08 23:37:00 +08:00
|
|
|
Language language1(l);
|
|
|
|
return ocrEngine.IsLanguageSupported(language1);
|
|
|
|
}
|
2024-04-02 15:36:52 +08:00
|
|
|
catch (...)
|
|
|
|
{
|
2024-01-08 23:37:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 03:53:55 +08:00
|
|
|
DECLARE_API void getlanguagelist(void (*cb)(LPCWSTR))
|
2024-04-02 15:36:52 +08:00
|
|
|
{
|
2024-01-08 23:37:00 +08:00
|
|
|
OcrEngine ocrEngine = OcrEngine::TryCreateFromUserProfileLanguages();
|
|
|
|
auto languages = ocrEngine.AvailableRecognizerLanguages();
|
2024-11-15 03:53:55 +08:00
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
for (auto &&language : languages)
|
2024-01-08 23:37:00 +08:00
|
|
|
{
|
|
|
|
auto lang = language.LanguageTag();
|
2024-07-14 15:09:37 +08:00
|
|
|
cb(lang.c_str());
|
2024-01-08 23:37:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-11-05 15:46:45 +08:00
|
|
|
DECLARE_API void OCR(void *ptr, size_t size, wchar_t *lang, wchar_t *space, void (*cb)(int, int, int, int, LPCWSTR))
|
2024-01-08 23:37:00 +08:00
|
|
|
{
|
2024-05-16 19:55:36 +08:00
|
|
|
IBuffer buffer = CryptographicBuffer::CreateFromByteArray(
|
|
|
|
winrt::array_view<uint8_t>(static_cast<uint8_t *>(ptr), size));
|
|
|
|
InMemoryRandomAccessStream memoryStream;
|
|
|
|
memoryStream.WriteAsync(buffer).get();
|
|
|
|
BitmapDecoder decoder = BitmapDecoder::CreateAsync(memoryStream).get();
|
2024-01-08 23:37:00 +08:00
|
|
|
|
|
|
|
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
|
|
|
|
std::wstring l = lang;
|
|
|
|
Language language(l);
|
|
|
|
OcrEngine ocrEngine = OcrEngine::TryCreateFromLanguage(language);
|
|
|
|
OcrResult ocrResult = ocrEngine.RecognizeAsync(softwareBitmap).get();
|
2024-04-02 15:36:52 +08:00
|
|
|
auto res = ocrResult.Lines();
|
2024-01-08 23:37:00 +08:00
|
|
|
for (auto line : res)
|
|
|
|
{
|
|
|
|
std::wstring xx = L"";
|
|
|
|
bool start = true;
|
2024-04-02 15:36:52 +08:00
|
|
|
unsigned int x1 = -1, x2 = 0, y1 = -1, y2 = 0;
|
|
|
|
|
|
|
|
for (auto word : line.Words())
|
|
|
|
{
|
|
|
|
if (!start)
|
2024-07-14 15:09:37 +08:00
|
|
|
xx += space;
|
2024-01-08 23:37:00 +08:00
|
|
|
start = false;
|
|
|
|
xx += word.Text();
|
2024-11-05 15:46:45 +08:00
|
|
|
auto &&rect = word.BoundingRect();
|
2024-07-01 22:14:01 +08:00
|
|
|
x1 = std::min((unsigned int)rect.X, x1);
|
|
|
|
x2 = std::max(x2, (unsigned int)(rect.X + rect.Width));
|
|
|
|
y1 = std::min((unsigned int)rect.Y, y1);
|
|
|
|
y2 = std::max(y2, (unsigned int)(rect.Y + rect.Height));
|
2024-01-08 23:37:00 +08:00
|
|
|
}
|
2024-11-15 03:53:55 +08:00
|
|
|
cb(x1, y1, x2, y2, xx.c_str());
|
2024-01-08 23:37:00 +08:00
|
|
|
}
|
|
|
|
}
|