-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOcrCaptureOverlay.cs
More file actions
320 lines (267 loc) · 11 KB
/
OcrCaptureOverlay.cs
File metadata and controls
320 lines (267 loc) · 11 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Shapes;
namespace NoteUI;
public sealed class OcrRegionCapturedEventArgs : EventArgs
{
public OcrRegionCapturedEventArgs(byte[] pixels, int width, int height)
{
Pixels = pixels;
Width = width;
Height = height;
}
public byte[] Pixels { get; }
public int Width { get; }
public int Height { get; }
}
public sealed class OcrCaptureOverlay : Window
{
public event EventHandler<OcrRegionCapturedEventArgs>? RegionCaptured;
public event EventHandler? CaptureCanceled;
private readonly Grid _rootGrid;
private readonly Image _screenshotImage;
private readonly Rectangle _selectionRect;
private readonly Border _hintBadge;
private readonly TextBlock _hintText;
private byte[]? _screenPixels;
private int _screenW;
private int _screenH;
private bool _isDragging;
private int _startX, _startY, _endX, _endY;
private IntPtr _crossCursorHandle;
public OcrCaptureOverlay()
{
ExtendsContentIntoTitleBar = true;
_rootGrid = new Grid
{
Background = new SolidColorBrush(Colors.Transparent),
IsTabStop = true
};
_rootGrid.KeyDown += RootGrid_KeyDown;
_screenshotImage = new Image { Stretch = Stretch.Fill };
var dimmer = new Rectangle
{
Fill = new SolidColorBrush(ColorHelper.FromArgb(0x77, 0, 0, 0))
};
var overlayCanvas = new Canvas();
_selectionRect = new Rectangle
{
Visibility = Visibility.Collapsed,
Stroke = new SolidColorBrush(ColorHelper.FromArgb(255, 46, 168, 255)),
StrokeThickness = 2,
Fill = new SolidColorBrush(ColorHelper.FromArgb(42, 255, 255, 255))
};
_hintText = new TextBlock
{
Text = Lang.T("ocr_select_zone"),
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12
};
_hintBadge = new Border
{
Background = new SolidColorBrush(ColorHelper.FromArgb(0xCC, 16, 16, 16)),
CornerRadius = new CornerRadius(6),
Padding = new Thickness(10, 6, 10, 6),
IsHitTestVisible = false,
Child = _hintText
};
overlayCanvas.Children.Add(_selectionRect);
overlayCanvas.Children.Add(_hintBadge);
Canvas.SetLeft(_hintBadge, 14);
Canvas.SetTop(_hintBadge, 14);
_rootGrid.Children.Add(_screenshotImage);
_rootGrid.Children.Add(dimmer);
_rootGrid.Children.Add(overlayCanvas);
Content = _rootGrid;
CaptureScreen();
Activated += OnActivated;
}
private void OnActivated(object sender, WindowActivatedEventArgs e)
{
Activated -= OnActivated;
if (AppWindow.Presenter is OverlappedPresenter presenter)
{
presenter.IsAlwaysOnTop = true;
presenter.SetBorderAndTitleBar(false, false);
}
AppWindow.MoveAndResize(new Windows.Graphics.RectInt32(0, 0, _screenW, _screenH));
var crossCursor = LoadCursor(IntPtr.Zero, IDC_CROSS);
SetCursor(crossCursor);
_crossCursorHandle = crossCursor;
_rootGrid.PointerPressed += RootGrid_PointerPressed;
_rootGrid.PointerMoved += RootGrid_PointerMoved;
_rootGrid.PointerReleased += RootGrid_PointerReleased;
_rootGrid.Focus(FocusState.Programmatic);
}
private void RootGrid_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
CancelCapture();
e.Handled = true;
}
}
private void RootGrid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var point = e.GetCurrentPoint(_rootGrid);
if (point.Properties.IsRightButtonPressed)
{
CancelCapture();
e.Handled = true;
return;
}
if (!point.Properties.IsLeftButtonPressed) return;
_isDragging = true;
_startX = Math.Clamp((int)point.Position.X, 0, _screenW - 1);
_startY = Math.Clamp((int)point.Position.Y, 0, _screenH - 1);
_endX = _startX;
_endY = _startY;
_selectionRect.Visibility = Visibility.Visible;
UpdateSelectionVisual();
e.Handled = true;
}
private void RootGrid_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (_crossCursorHandle != IntPtr.Zero) SetCursor(_crossCursorHandle);
if (!_isDragging) return;
var point = e.GetCurrentPoint(_rootGrid);
_endX = Math.Clamp((int)point.Position.X, 0, _screenW - 1);
_endY = Math.Clamp((int)point.Position.Y, 0, _screenH - 1);
UpdateSelectionVisual();
e.Handled = true;
}
private void RootGrid_PointerReleased(object sender, PointerRoutedEventArgs e)
{
if (!_isDragging) return;
_isDragging = false;
var point = e.GetCurrentPoint(_rootGrid);
_endX = Math.Clamp((int)point.Position.X, 0, _screenW - 1);
_endY = Math.Clamp((int)point.Position.Y, 0, _screenH - 1);
UpdateSelectionVisual();
if (!TryGetSelectionRect(out var x, out var y, out var width, out var height))
{
_selectionRect.Visibility = Visibility.Collapsed;
_hintText.Text = Lang.T("ocr_too_small");
return;
}
var pixels = ExtractRegionPixels(x, y, width, height);
RegionCaptured?.Invoke(this, new OcrRegionCapturedEventArgs(pixels, width, height));
Close();
e.Handled = true;
}
private void UpdateSelectionVisual()
{
var x = Math.Min(_startX, _endX);
var y = Math.Min(_startY, _endY);
var width = Math.Abs(_endX - _startX);
var height = Math.Abs(_endY - _startY);
Canvas.SetLeft(_selectionRect, x);
Canvas.SetTop(_selectionRect, y);
_selectionRect.Width = Math.Max(1, width);
_selectionRect.Height = Math.Max(1, height);
_hintText.Text = width > 3 && height > 3
? $"{width} × {height}"
: "Sélectionnez une zone";
}
private bool TryGetSelectionRect(out int x, out int y, out int width, out int height)
{
x = Math.Min(_startX, _endX);
y = Math.Min(_startY, _endY);
width = Math.Abs(_endX - _startX);
height = Math.Abs(_endY - _startY);
if (width < 4 || height < 4) return false;
if (x + width > _screenW) width = _screenW - x;
if (y + height > _screenH) height = _screenH - y;
return width > 0 && height > 0;
}
private byte[] ExtractRegionPixels(int x, int y, int width, int height)
{
if (_screenPixels == null || width <= 0 || height <= 0)
return [];
var output = new byte[width * height * 4];
var rowSize = width * 4;
for (var row = 0; row < height; row++)
{
var srcOffset = ((y + row) * _screenW + x) * 4;
var dstOffset = row * rowSize;
Buffer.BlockCopy(_screenPixels, srcOffset, output, dstOffset, rowSize);
}
return output;
}
private void CancelCapture()
{
CaptureCanceled?.Invoke(this, EventArgs.Empty);
Close();
}
// ── GDI screen capture ──────────────────────────────────────
[DllImport("user32.dll")] private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")] private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")] private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")] private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int w, int h);
[DllImport("gdi32.dll")] private static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
[DllImport("gdi32.dll")] private static extern bool BitBlt(IntPtr hdcDst, int xd, int yd, int w, int h, IntPtr hdcSrc, int xs, int ys, uint rop);
[DllImport("gdi32.dll")] private static extern bool DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr obj);
[DllImport("gdi32.dll")] private static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint start, uint lines, byte[] bits, ref BITMAPINFO bi, uint usage);
[DllImport("user32.dll")] private static extern int GetSystemMetrics(int index);
[DllImport("user32.dll")] private static extern IntPtr SetCursor(IntPtr hCursor);
[DllImport("user32.dll")] private static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
private const int SM_CXSCREEN = 0, SM_CYSCREEN = 1;
private const uint SRCCOPY = 0x00CC0020;
private const int IDC_CROSS = 32515;
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFOHEADER
{
public uint biSize; public int biWidth, biHeight;
public ushort biPlanes, biBitCount;
public uint biCompression, biSizeImage;
public int biXPelsPerMeter, biYPelsPerMeter;
public uint biClrUsed, biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
private struct BITMAPINFO { public BITMAPINFOHEADER bmiHeader; public uint bmiColors; }
private void CaptureScreen()
{
_screenW = GetSystemMetrics(SM_CXSCREEN);
_screenH = GetSystemMetrics(SM_CYSCREEN);
IntPtr hdcScreen = IntPtr.Zero, hdcMem = IntPtr.Zero, hBitmap = IntPtr.Zero, hOld = IntPtr.Zero;
try
{
hdcScreen = GetDC(IntPtr.Zero);
hdcMem = CreateCompatibleDC(hdcScreen);
hBitmap = CreateCompatibleBitmap(hdcScreen, _screenW, _screenH);
hOld = SelectObject(hdcMem, hBitmap);
BitBlt(hdcMem, 0, 0, _screenW, _screenH, hdcScreen, 0, 0, SRCCOPY);
var bmi = new BITMAPINFO
{
bmiHeader = new BITMAPINFOHEADER
{
biSize = (uint)Marshal.SizeOf<BITMAPINFOHEADER>(),
biWidth = _screenW, biHeight = -_screenH,
biPlanes = 1, biBitCount = 32, biCompression = 0
}
};
_screenPixels = new byte[_screenW * _screenH * 4];
GetDIBits(hdcMem, hBitmap, 0, (uint)_screenH, _screenPixels, ref bmi, 0);
var wb = new WriteableBitmap(_screenW, _screenH);
using var stream = wb.PixelBuffer.AsStream();
stream.Write(_screenPixels, 0, _screenPixels.Length);
wb.Invalidate();
_screenshotImage.Source = wb;
}
finally
{
if (hOld != IntPtr.Zero && hdcMem != IntPtr.Zero) SelectObject(hdcMem, hOld);
if (hBitmap != IntPtr.Zero) DeleteObject(hBitmap);
if (hdcMem != IntPtr.Zero) DeleteDC(hdcMem);
if (hdcScreen != IntPtr.Zero) ReleaseDC(IntPtr.Zero, hdcScreen);
}
}
}