-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
226 lines (216 loc) · 6.83 KB
/
Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib,"crypt32")
#pragma comment(lib,"wininet")
#include <windows.h>
#include <wininet.h>
#include "json11.hpp"
#define API_KEY L"Input your API KEY"
TCHAR szClassName[] = TEXT("Window");
LPBYTE SendGoogleCloudVision(HWND hWnd, LPBYTE lpByte64)
{
HINTERNET hSession = InternetOpen(TEXT("DOWNLOAD"), INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
if (!hSession)
{
MessageBox(hWnd, TEXT("WININETの初期化に失敗しました"), 0, MB_ICONERROR);
return 0;
}
HINTERNET hConnection = InternetConnect(hSession, L"vision.googleapis.com", INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnection)
{
InternetCloseHandle(hSession);
MessageBox(hWnd, TEXT("サーバーへの接続に失敗しました"), 0, MB_ICONERROR);
return 0;
}
WCHAR szPath[1024];
wsprintf(szPath, L"v1/images:annotate?key=%s", API_KEY);
HINTERNET hRequest = HttpOpenRequest(hConnection, TEXT("POST"), szPath, 0, 0, 0, INTERNET_FLAG_SECURE, 0);
if (!hRequest)
{
InternetCloseHandle(hConnection);
InternetCloseHandle(hSession);
MessageBox(hWnd, TEXT("HTTPリクエストの作成に失敗しました"), 0, MB_ICONERROR);
return 0;
}
wchar_t header[] = L"Content-Type: application/json; charset=utf-8";
LPSTR data = (LPSTR)GlobalAlloc(0, 1024 + GlobalSize(lpByte64));
lstrcpyA(data, "{\"requests\":[{\"features\":{\"type\":\"DOCUMENT_TEXT_DETECTION\",\"maxResults\":1},\"imageContext\":{\"languageHints\":\"ja\"},\"image\":{\"content\":\"");
lstrcatA(data, (LPCSTR)lpByte64);
lstrcatA(data, "\"}}]}");
HttpSendRequest(hRequest, header, (DWORD)wcslen(header), (LPVOID)(data), (DWORD)strlen(data));
GlobalFree(data);
DWORD dwSize = 0;
LPBYTE lpByte = (LPBYTE)GlobalAlloc(GMEM_FIXED, 1);
DWORD dwRead;
static BYTE szBuf[1024 * 4];
LPBYTE lpTmp;
for (;;)
{
if (!InternetReadFile(hRequest, szBuf, (DWORD)sizeof(szBuf), &dwRead) || !dwRead) break;
lpTmp = (LPBYTE)GlobalReAlloc(lpByte, (SIZE_T)(dwSize + dwRead), GMEM_MOVEABLE);
if (lpTmp == NULL) break;
lpByte = lpTmp;
CopyMemory(lpByte + dwSize, szBuf, dwRead);
dwSize += dwRead;
}
lpByte[dwSize] = 0;
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnection);
InternetCloseHandle(hSession);
return lpByte;
}
LPWSTR a2w(LPCSTR lpszText)
{
const DWORD dwSize = MultiByteToWideChar(CP_UTF8, 0, lpszText, -1, 0, 0);
LPWSTR lpszReturnText = (LPWSTR)GlobalAlloc(0, dwSize * sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, lpszText, -1, lpszReturnText, dwSize);
return lpszReturnText;
}
LPWSTR GetTextFromJson(LPCSTR lpszJson)
{
std::string err;
json11::Json v = json11::Json::parse(lpszJson, err);
if (err.size() == 0 && !v.is_null())
{
json11::Json responses = v["responses"];
if (!responses.is_null() && responses.is_array()) {
json11::Json response0 = responses[0];
if (!response0.is_null()) {
json11::Json response0 = responses[0];
if (!response0.is_null()) {
json11::Json fullTextAnnotation = response0["fullTextAnnotation"];
if (!fullTextAnnotation.is_null()) {
json11::Json text = fullTextAnnotation["text"];
if (!text.is_null()) {
return a2w(text.string_value().c_str());
}
}
}
}
}
}
return 0;
}
LPBYTE base64encode(LPCBYTE lpData, DWORD dwSize)
{
DWORD dwResult = 0;
if (CryptBinaryToStringA(lpData, dwSize, CRYPT_STRING_BASE64, 0, &dwResult)) {
LPBYTE lpszBase64 = (LPBYTE)GlobalAlloc(GMEM_FIXED, dwResult + 1);
if (CryptBinaryToStringA(lpData, dwSize, CRYPT_STRING_BASE64, (LPSTR)lpszBase64, &dwResult)) {
return lpszBase64;
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;
static HFONT hFont;
switch (msg)
{
case WM_CREATE:
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), 0, WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 0, 0, hWnd, 0, ((LPCREATESTRUCT)lParam)->hInstance, 0);
SendMessage(hEdit, EM_LIMITTEXT, 0, 0);
hFont = CreateFontW(-20, 0, 0, 0, FW_NORMAL, 0, 0, 0, SHIFTJIS_CHARSET, 0, 0, 0, 0, L"MS Shell Dlg");
SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, 0);
DragAcceptFiles(hWnd, TRUE);
break;
case WM_SIZE:
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
case WM_DROPFILES:
{
const UINT nFileCount = DragQueryFile((HDROP)wParam, 0xFFFFFFFF, NULL, 0);
for (;;) {
SetWindowText(hEdit, 0);
WCHAR szFilePath[MAX_PATH];
DragQueryFile((HDROP)wParam, 0, szFilePath, _countof(szFilePath));
HANDLE hFile = CreateFile(szFilePath, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBox(hWnd, TEXT("ファイルが開けません"), 0, MB_OK);
break;
}
else {
LPBYTE lpByte64 = 0;
LPBYTE lpByteFile = (LPBYTE)GlobalAlloc(GPTR, GetFileSize(hFile, 0) + 1);
if (lpByteFile) {
DWORD dwReadSize;
ReadFile(hFile, lpByteFile, GetFileSize(hFile, 0), &dwReadSize, 0);
lpByte64 = base64encode(lpByteFile, dwReadSize);
GlobalFree(lpByteFile);
}
CloseHandle(hFile);
if (lpByte64) {
LPBYTE lpByte = SendGoogleCloudVision(hWnd, lpByte64);
GlobalFree(lpByte64);
if (lpByte) {
LPWSTR lpszText = GetTextFromJson((LPCSTR)lpByte);
if (lpszText) {
LPWSTR context = 0;
wchar_t* p = wcstok_s(lpszText, L"\n", &context);
SendMessage(hEdit, WM_SETREDRAW, FALSE, 0);
while (p) {
SendMessageW(hEdit, EM_REPLACESEL, 0, (LPARAM)p);
SendMessageW(hEdit, EM_REPLACESEL, 0, (LPARAM)L"\r\n");
p = wcstok_s(0, L"\n", &context);
}
SendMessage(hEdit, EM_SETSEL, 0, -1);
SendMessage(hEdit, WM_SETREDRAW, TRUE, 0);
GlobalFree(lpszText);
}
GlobalFree(lpByte);
}
SetFocus(hEdit);
}
}
break;
}
DragFinish((HDROP)wParam);
}
break;
case WM_DESTROY:
DeleteObject(hFont);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPreInst, LPWSTR pCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
0,
LoadCursor(0,IDC_ARROW),
(HBRUSH)(COLOR_WINDOW + 1),
0,
szClassName
};
RegisterClass(&wndclass);
HWND hWnd = CreateWindow(
szClassName,
TEXT("Google Cloud Vision OCR をドラッグ&ドロップされたテキスト情報を取得"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
0,
0,
hInstance,
0
);
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}