-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
177 lines (136 loc) · 4.88 KB
/
MainWindow.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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media.Imaging;
using Ocr.Annotations;
using Tesseract;
using Rect = Tesseract.Rect;
namespace Ocr
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _lastResult;
private string _lastError;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string Error
{
get
{
return _lastError;
}
set
{
if (_lastError != value)
{
_lastError = value;
OnPropertyChanged();
}
}
}
public string Result
{
get
{
return _lastResult;
}
set
{
if (_lastResult != value)
{
_lastResult = value;
OnPropertyChanged();
}
}
}
private void OnChoseImageButtonClick(object sender, RoutedEventArgs e)
{
Error = string.Empty;
Result = string.Empty;
var source = LoadImage();
if (source != null)
{
Image.Source = source;
ScanImage(source);
}
}
private void OnScanImageButtonClick(object sender, RoutedEventArgs e)
{
}
private void OnScanRegionsButtonClick(object sender, RoutedEventArgs e)
{
}
private void ScanImage(BitmapImage source)
{
try
{
var enginePath = @"F:\Workbench\Test Projects\Ocr\Tesseract - Backup\Tesseract-OCR";
var api = new TesseractEngine(enginePath, "eng", EngineMode.Default);
// Open input image with leptonica library
var image = Pix.LoadFromFile(source.UriSource.ToString());
var page = api.Process(image);
page.RegionOfInterest = new Rect(25, 25, 600, 400);
var text = "--- Region 1 --------------------------------\n\n" + page.GetText().Trim() + "\n\n";
page.RegionOfInterest = new Rect(30, 540, 640, 360);
text += "--- Region 2 --------------------------------\n\n" + page.GetText().Trim() + "\n\n";
page.RegionOfInterest = new Rect(760, 100, 620, 370);
text += "--- Region 3 --------------------------------\n\n" + page.GetText().Trim() + "\n\n";
page.RegionOfInterest = new Rect(760, 600, 650, 250);
text += "--- Region 4 --------------------------------\n\n" + page.GetText().Trim() + "\n\n";
Result = text;
}
catch (Exception exception)
{
var error = "Error when scanning image:" + Environment.NewLine + exception.Message;
if (exception.InnerException != null)
error += Environment.NewLine + exception.InnerException.Message;
Result = error;
}
}
private BitmapImage LoadImage()
{
try
{
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "Images (*.png, *.jpg)|*.png;*.jpg";
bool? result = dlg.ShowDialog();
if (result.HasValue && result.Value)
{
var filename = dlg.FileName.Trim();
FilePathTextBox.Text = filename;
var source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri(filename, UriKind.Relative);
source.CacheOption = BitmapCacheOption.OnLoad;
source.EndInit();
return source;
}
return null;
}
catch (Exception exception)
{
var error = "Error when loading image:" + Environment.NewLine + exception.Message;
if (exception.InnerException != null)
error += Environment.NewLine + exception.InnerException.Message;
Result = error;
return null;
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}