54 lines
2.0 KiB
C++
54 lines
2.0 KiB
C++
#include "ocr.h"
|
|
#include <iostream>
|
|
int main()
|
|
{
|
|
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
try
|
|
{
|
|
BITMAPINFO info{};
|
|
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
info.bmiHeader.biWidth = 800;
|
|
info.bmiHeader.biHeight = -150;
|
|
info.bmiHeader.biPlanes = 1;
|
|
info.bmiHeader.biBitCount = 32;
|
|
void *data{};
|
|
HBITMAP bitmap = CreateDIBSection(nullptr, &info, DIB_RGB_COLORS, &data, nullptr, 0);
|
|
if (!bitmap)
|
|
throw std::runtime_error("Cannot create OCR fixture");
|
|
HDC dc = CreateCompatibleDC(nullptr);
|
|
auto old = SelectObject(dc, bitmap);
|
|
RECT rect{0, 0, 800, 150};
|
|
FillRect(dc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
|
|
HFONT font = CreateFontW(-54, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
|
|
ANTIALIASED_QUALITY, 0, L"Arial");
|
|
auto oldFont = SelectObject(dc, font);
|
|
SetTextColor(dc, RGB(0, 0, 0));
|
|
SetBkColor(dc, RGB(255, 255, 255));
|
|
TextOutW(dc, 20, 40, L"HELLO KSWITCHER", 15);
|
|
GdiFlush();
|
|
ks::Pixels pixels{800, 150, ks::Bytes((BYTE *)data, (BYTE *)data + 800 * 150 * 4)};
|
|
SelectObject(dc, oldFont);
|
|
SelectObject(dc, old);
|
|
DeleteObject(font);
|
|
DeleteObject(bitmap);
|
|
DeleteDC(dc);
|
|
ks::Clip clip{{ks::imageDib(pixels)}};
|
|
auto decoded = ks::decodeImage(clip);
|
|
if (decoded.width != 800 || decoded.height != 150)
|
|
throw std::runtime_error("WIC decode size mismatch");
|
|
auto text = ks::recognizeText(clip);
|
|
std::cout << "OCR: " << ks::utf8(text) << '\n';
|
|
if (ks::upper(text).find(L"HELLO") == std::wstring::npos)
|
|
throw std::runtime_error("OCR fixture was not recognized");
|
|
std::cout << "PASS native Windows OCR, DIB decoding and async result retrieval\n";
|
|
CoUninitialize();
|
|
return 0;
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
std::cerr << e.what() << '\n';
|
|
CoUninitialize();
|
|
return 1;
|
|
}
|
|
}
|