-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window1.xaml.cs
672 lines (539 loc) · 21.6 KB
/
Window1.xaml.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;
using Tesseract;
// Notes:
// - The Canvas must have a non-null background to make it generate mouse events.
namespace Protonox
{
/// <summary>
/// Global hotkeys
/// </summary>
internal static class Win32
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalAddAtom(string lpString);
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalDeleteAtom(short nAtom);
public const int MOD_ALT = 1;
public const int MOD_CONTROL = 2;
public const int MOD_SHIFT = 4;
public const int MOD_WIN = 8;
public const uint VK_KEY_C = 0x43;
public const uint VK_SPACE = 0x20;
public const int WM_HOTKEY = 0x312;
}
/// <summary>
/// Get an area of the screen as a Bitmap
/// </summary>
public static class ScreenShot
{
public static System.Drawing.Bitmap GetBitmap(System.Drawing.Point SourcePoint, System.Drawing.Point DestinationPoint, System.Drawing.Rectangle SelectionRectangle)
{
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(SelectionRectangle.Width, SelectionRectangle.Height);
System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
graphics.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
return bitmap;
}
}
/// <summary>
/// Interaction logic for Window1.xaml
/// http://csharphelper.com/blog/2014/12/let-user-move-resize-rectangle-wpf-c/
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
#region Variables
// Represenet the application status (max/min)
private bool overlayStatus = true;
private Point _minRectSize = new Point(5, 5);
private readonly double _screenWidth = SystemParameters.PrimaryScreenWidth;
private readonly double _screenHeight = SystemParameters.PrimaryScreenHeight;
private Point _point;
public Point RectPoint
{
get { return this._point; }
private set
{
this._point = value;
this.OnPropertyChanged("RectPoint");
}
}
public double AreaWidth { get; private set; }
public double AreaHeight { get; private set; }
// True if a drag is in progress.
private bool _isDragging = false;
private bool _isRectDrawn = false;
private bool _isLeftClickDown = false;
// The drag's last point.
private Point _lastMousePoint;
// The part of the rectangle under the mouse.
private HitType _mouseHitType = HitType.None;
// Hotkeys
private HwndSource hk_hWndSource;
private short hk_atom;
private Thread _workerThread;
const string sourceLang = "en";
const string targetLang = "hu";
#endregion
public Window1()
{
InitializeComponent();
this.DataContext = this;
this.PreviewKeyDown += new KeyEventHandler(HandleKeyDown_window);
ResetRectangle();
}
public void SetOverlayStatus(bool status)
{
if (status)
WindowState = WindowState.Maximized;
else
WindowState = WindowState.Minimized;
overlayStatus = status;
}
#region Interface Inotify
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region Rectangle Editor
// The part of the rectangle the mouse is over.
private enum HitType
{
None, Body, UL, UR, LR, LL, L, R, T, B
};
// Return a HitType value to indicate what is at the point.
private HitType SetHitType( Point point )
{
double left = Canvas.GetLeft( RecodBorderSizeObject );
double top = Canvas.GetTop( RecodBorderSizeObject );
double right = left + RecodBorderSizeObject.Width;
double bottom = top + RecodBorderSizeObject.Height;
if ( point.X < left ) return HitType.None;
if ( point.X > right ) return HitType.None;
if ( point.Y < top ) return HitType.None;
if ( point.Y > bottom ) return HitType.None;
const double GAP = 10;
if ( point.X - left < GAP )
{
// Left edge.
if ( point.Y - top < GAP ) return HitType.UL;
if ( bottom - point.Y < GAP ) return HitType.LL;
return HitType.L;
}
if ( right - point.X < GAP )
{
// Right edge.
if ( point.Y - top < GAP ) return HitType.UR;
if ( bottom - point.Y < GAP ) return HitType.LR;
return HitType.R;
}
if ( point.Y - top < GAP ) return HitType.T;
if ( bottom - point.Y < GAP ) return HitType.B;
return HitType.Body;
}
// Set a mouse cursor appropriate for the current hit type.
private void SetMouseCursor()
{
// See what cursor we should display.
Cursor desired_cursor = Cursors.Arrow;
switch ( _mouseHitType )
{
case HitType.None:
desired_cursor = Cursors.Arrow;
break;
case HitType.Body:
desired_cursor = Cursors.ScrollAll;
break;
case HitType.UL:
case HitType.LR:
desired_cursor = Cursors.SizeNWSE;
break;
case HitType.LL:
case HitType.UR:
desired_cursor = Cursors.SizeNESW;
break;
case HitType.T:
case HitType.B:
desired_cursor = Cursors.SizeNS;
break;
case HitType.L:
case HitType.R:
desired_cursor = Cursors.SizeWE;
break;
}
// Display the desired cursor.
if ( Cursor != desired_cursor ) Cursor = desired_cursor;
}
private void ResetRectangle()
{
Canvas.SetLeft(RecodBorderSizeObject, 0);
Canvas.SetTop(RecodBorderSizeObject, 0);
RecodBorderSizeObject.Width = 0;
RecodBorderSizeObject.Height = 0;
this.RectPoint = new Point(0, 0);
this.AreaWidth = 0;
this.AreaHeight = 0;
Cursor = Cursors.Arrow;
}
private bool IsValidRectangle()
{
return (AreaWidth > 0 && AreaHeight > 0);
}
#endregion
#region Global Keybinding
private void RegisterHotkeys()
{
WindowInteropHelper wih = new WindowInteropHelper(this);
hk_hWndSource = HwndSource.FromHwnd(wih.Handle);
hk_hWndSource.AddHook(MainWindowProc);
// create an atom for registering the hotkey
hk_atom = Win32.GlobalAddAtom("ProtonoxTrans");
if (hk_atom == 0)
throw new Win32Exception(Marshal.GetLastWin32Error());
// register the CTRL+SHIFT+Space hotkey
//if (!Win32.RegisterHotKey(wih.Handle, atom, Win32.MOD_CONTROL | Win32.MOD_SHIFT, Win32.VK_SPACE))
if (!Win32.RegisterHotKey(wih.Handle, hk_atom, Win32.MOD_CONTROL, Win32.VK_SPACE))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
private void ReleaseHotkeys()
{
if (hk_atom != 0)
Win32.UnregisterHotKey(hk_hWndSource.Handle, hk_atom);
}
private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case Win32.WM_HOTKEY:
SetOverlayStatus(!overlayStatus);
handled = true;
break;
}
return IntPtr.Zero;
}
#endregion
#region Tesseract Commands
private void StartWorkerThread()
{
if (_workerThread != null)
_workerThread.Abort();
int th = (int)RecodBorderSizeObject.BorderThickness.Left;
_workerThread = new Thread(ExtractTextFromBitmap);
_workerThread.Start(th);
}
public void ExtractTextFromBitmap(object pth)
{
int th = Convert.ToInt32(pth);
int x = (int)RectPoint.X;
int y = (int)RectPoint.Y;
int w = (int)AreaWidth;
int h = (int)AreaHeight;
System.Drawing.Point startPoint = new System.Drawing.Point(x + th, y + th);
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(startPoint.X, startPoint.Y, w - 2*th, h - 2*th);
System.Drawing.Bitmap bitmap = ScreenShot.GetBitmap(startPoint, System.Drawing.Point.Empty, rectangle);
//bitmap.Save("text.png", System.Drawing.Imaging.ImageFormat.Png);
var ocr = new TesseractEngine(@".\tessdata", "eng", EngineMode.TesseractAndCube);
var page = ocr.Process(bitmap);
string recText = page.GetText();
ProcessText(ref recText);
ExecuteNodeJS(sourceLang, targetLang, recText);
}
private void ProcessText(ref string recText)
{
//text = text.Replace(". *", " ");
recText = recText.Replace("L ", "1. ");
recText = recText.Replace("\nl. ", "\n1. ");
recText = recText.Replace("\n1.", "\n\n1.");
recText = recText.Replace("*", " ");
//text = text.Replace(". \n", ". ");
recText = recText.Replace('[', ' ');
recText = recText.Replace(']', ' ');
recText = recText.Replace('»', '.');
recText = recText.Replace("\n\n", "\n");
recText = recText.Replace(" ", " ");
//text = text.Replace(" \n", "\n");
recText = recText.Trim();
/*
string pattern = "([.][*])|([!][*])|([?][*])";
string replacement = "\n";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(text, replacement);
*/
}
//Update GUI element from an another Thread
public delegate void UpdateTextCallback(string message);
private void UpdateOuputText(string message)
{
outputText.Content = message;
}
#endregion
private void ExecuteNodeJS(string srcLang, string targetLang, string text)
{
try
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\nodejs\node.exe";
string argument = string.Format("main.js --srclang=\"{0}\" --targetlang=\"{1}\" --text=\"{2}\"", srcLang, targetLang, text);
process.StartInfo.Arguments = @argument;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
_stdOutput = string.Empty;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
outputText.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateOuputText), new object[] { _stdOutput.Trim() });
}
catch(Exception exception)
{
_stdOutput = "NodeJs not installed!";
outputText.Dispatcher.Invoke(new UpdateTextCallback(this.UpdateOuputText), new object[] { _stdOutput.Trim() + "\n" + exception.Message });
}
//Stop the Thread
_workerThread.Abort();
_workerThread = null;
}
private static string _stdOutput;
private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
_stdOutput += outLine.Data + "\n";
}
#region Window Events
private void Window_Loaded(object sender, RoutedEventArgs e)
{
RegisterHotkeys();
}
private void Window_Closed(object sender, EventArgs e)
{
ReleaseHotkeys();
}
private void HandleOnResize(object sender, SizeChangedEventArgs e)
{
if (WindowState == WindowState.Maximized)
overlayStatus = true;
else
overlayStatus = false;
}
#endregion
#region Keyboard & Mouse Events
private void HandleKeyDown_window(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
Close();
}
else if (e.Key == Key.D1)
{
outputText.SetValue(Grid.RowProperty, 0);
outputText.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Top);
outputText.SetValue(Grid.RowSpanProperty, 1);
}
else if (e.Key == Key.D2)
{
outputText.SetValue(Grid.RowProperty, 1);
outputText.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Bottom);
outputText.SetValue(Grid.RowSpanProperty, 1);
}
else if (e.Key == Key.D3)
{
outputText.SetValue(Grid.RowProperty, 2);
outputText.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Bottom);
outputText.SetValue(Grid.RowSpanProperty, 1);
}
}
private void HandleMouseDown_canvas1(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
_isLeftClickDown = true;
if (IsValidRectangle() && e.ClickCount == 2)
{
StartWorkerThread();
return;
}
_mouseHitType = SetHitType(Mouse.GetPosition(CanvasMain));
SetMouseCursor();
if (_isRectDrawn && _mouseHitType == HitType.None)
{
ResetRectangle();
_isRectDrawn = false;
}
_lastMousePoint = Mouse.GetPosition(CanvasMain);
if (_isRectDrawn)
_isDragging = true;
else
{
//Set rect top left corner to cursor
this.RectPoint = new Point(_lastMousePoint.X, _lastMousePoint.Y);
Canvas.SetLeft(RecodBorderSizeObject, _lastMousePoint.X);
Canvas.SetTop(RecodBorderSizeObject, _lastMousePoint.Y);
}
}
else if (e.ChangedButton == MouseButton.Right)
{
SetOverlayStatus(false);
}
}
private void HandleMouseUp_canvas1(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
_isDragging = false;
_isLeftClickDown = false;
if (IsValidRectangle())
_isRectDrawn = true;
SetMouseCursor();
}
}
private void HandleMouseMove_canvas1(object sender, MouseEventArgs e)
{
if (!_isLeftClickDown)
{
_mouseHitType = SetHitType(Mouse.GetPosition(CanvasMain));
SetMouseCursor();
return;
}
if (!_isRectDrawn)
{
// See how much the mouse has moved.
Point mousePoint = Mouse.GetPosition(CanvasMain);
double deltaX = mousePoint.X - _lastMousePoint.X;
double deltaY = mousePoint.Y - _lastMousePoint.Y;
if (deltaX > _minRectSize.X && deltaY > _minRectSize.Y)
{
RecodBorderSizeObject.Width = deltaX;
RecodBorderSizeObject.Height = deltaY;
this.AreaWidth = deltaX;
this.AreaHeight = deltaY;
}
_mouseHitType = SetHitType(Mouse.GetPosition(CanvasMain));
SetMouseCursor();
return;
}
if (!_isDragging)
{
_mouseHitType = SetHitType(Mouse.GetPosition(CanvasMain));
SetMouseCursor();
}
else
{
// See how much the mouse has moved.
Point mousePoint = Mouse.GetPosition(CanvasMain);
double deltaX = mousePoint.X - _lastMousePoint.X;
double deltaY = mousePoint.Y - _lastMousePoint.Y;
// Get the rectangle's current position.
double newX = Canvas.GetLeft(RecodBorderSizeObject);
double newY = Canvas.GetTop(RecodBorderSizeObject);
double newWidth = RecodBorderSizeObject.Width;
double newHeight = RecodBorderSizeObject.Height;
// Bound checks
if (newX <= 0)
newX = 0;
if (newY <= 0)
newY = 0;
if (newWidth > _screenWidth)
newWidth = _screenWidth;
if (newHeight > _screenHeight)
newHeight = _screenHeight;
if (newX + newWidth >= _screenWidth)
newX -= newX + newWidth - _screenWidth;
if (newY + newHeight >= _screenHeight)
newY -= newY + newHeight - _screenHeight;
// Update the rectangle.
switch (_mouseHitType)
{
case HitType.Body:
newX += deltaX;
newY += deltaY;
break;
case HitType.UL:
newX += deltaX;
newY += deltaY;
newWidth -= deltaX;
newHeight -= deltaY;
break;
case HitType.UR:
newY += deltaY;
newWidth += deltaX;
newHeight -= deltaY;
break;
case HitType.LR:
newWidth += deltaX;
newHeight += deltaY;
break;
case HitType.LL:
newX += deltaX;
newWidth -= deltaX;
newHeight += deltaY;
break;
case HitType.L:
newX += deltaX;
newWidth -= deltaX;
break;
case HitType.R:
newWidth += deltaX;
break;
case HitType.B:
newHeight += deltaY;
break;
case HitType.T:
newY += deltaY;
newHeight -= deltaY;
break;
}
// Don't use negative width or height.
if ((newWidth > _minRectSize.X) && (newHeight > _minRectSize.Y))
{
// Update the rectangle.
Canvas.SetLeft(RecodBorderSizeObject, newX);
Canvas.SetTop(RecodBorderSizeObject, newY);
RecodBorderSizeObject.Width = newWidth;
RecodBorderSizeObject.Height = newHeight;
this.RectPoint = new Point(newX, newY);
this.AreaWidth = newWidth;
this.AreaHeight = newHeight;
// Save the mouse's new location.
_lastMousePoint = mousePoint;
}
}
}
#endregion
#region Button Click Events
private void HandleOnClick_ButtonExit(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void HandleOnClick_ButtonTrans(object sender, RoutedEventArgs e)
{
if (IsValidRectangle())
StartWorkerThread();
}
private void HandleOnClick_ButtonToggle(object sender, RoutedEventArgs e)
{
SetOverlayStatus(!overlayStatus);
}
#endregion
}
}