-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
381 lines (349 loc) · 13.7 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Timers;
using System.Runtime.InteropServices;
namespace BlinkV0._01
{
//时间文件读取
public class DataFile
{
private int intCorrectMinute;
private int intCorrectSecond;
private int intRelaxMinute;
private int intRelaxSecond;
private string strDir;
public DataFile()
{
string strLine;
strDir = AppDomain.CurrentDomain.BaseDirectory + "TimeData.txt";
try
{
FileStream TimeData = new FileStream(strDir, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(TimeData);
strLine = sr.ReadLine();
intCorrectMinute = int.Parse(strLine);
strLine = sr.ReadLine();
intCorrectSecond = int.Parse(strLine);
strLine = sr.ReadLine();
intRelaxMinute = int.Parse(strLine);
strLine = sr.ReadLine();
intRelaxSecond = int.Parse(strLine);
sr.Close();
}
catch (IOException ex)
{
MessageBox.Show("出现了一个IO异常!");
MessageBox.Show(ex.ToString());
return;
}
}
public int CorrectMinute
{
get
{ return intCorrectMinute; }
}
public int CorrectSecond
{
get
{ return intCorrectSecond; }
}
public int CorrectSeconds
{
get
{ return intCorrectMinute * 60 + intCorrectSecond; }
}
public int RelaxMinute
{
get
{ return intRelaxMinute; }
}
public int RelaxSecond
{
get
{ return intRelaxSecond; }
}
public int RelaxSeconds
{
get
{ return intRelaxMinute * 60 + intRelaxSecond; }
}
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
[DllImport(@"BlinkDll.dll", EntryPoint = "dllmain", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int dllmain(ref int EyeX, ref int EyeY);
[DllImport(@"BlinkDll.dll", EntryPoint = "init", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void init();
public static string StrTime;
public static int CorrectCount = 0;
public delegate void Correction(string message);
public delegate void Detection();
public delegate void AnalyseArg();
public static class Args
{
public static int longMilliseconds = 10000;
public static int shortMilliseconds = 5000;
public static int preAnaMilliseconds = 6000;
public static int Threshold = 7;
public static string Message1 = "提醒";
}
private static class Flag
{
public static bool Message = false;
public static MessageBoxResult MessageResult = MessageBoxResult.None;
public static bool Foucus = false;
}
private static class Coord
{
public static int EyeX = -1;
public static int EyeY = -1;
public static int EyeW = -1;
public static int EyeH = -1;
public static int lastEyeX = -1;
public static int lastEyeY = -1;
public static int stableX = -1;
public static int stableY = -1;
}
private static class Counter
{
public static int sTimeCount = 0;
public static int msTimeCount = 0;
public static int longTimeCount = 0;
public static int shortTimeCount = 0;
}
//“检测”事件
public class Events
{
public event Detection TimeToDetect;
public event AnalyseArg TimeToAnalyse;
public event Correction LongTimeDetected;
public event Correction ShortTimeDetected;
private Timer TdTimer;
MainWindow mainWindow;
public Events(MainWindow w)
{
this.mainWindow = w;
TdTimer = new Timer(100);
TdTimer.Elapsed += new ElapsedEventHandler(CheckForDetectSignal);
TdTimer.Elapsed += new ElapsedEventHandler(CheckForAnalyseSignal);
TdTimer.Elapsed += new ElapsedEventHandler(CheckForLongTimeSignal);
TdTimer.Elapsed += new ElapsedEventHandler(CheckForShortTimeSignal);
}
public void Detect()
{
TdTimer.Start();
}
public void DetectFinished()
{
TdTimer.Stop();
}
private void CheckForDetectSignal(Object source, ElapsedEventArgs e)
{
if (TimeToDetect != null)
TimeToDetect();
}
private void CheckForAnalyseSignal(Object source, ElapsedEventArgs e)
{
if (TimeToAnalyse != null)
TimeToAnalyse();
}
private void CheckForLongTimeSignal(Object source, ElapsedEventArgs e)
{
if (LongTimeDetected != null && Flag.Message == false && Counter.longTimeCount > Args.longMilliseconds)
{
Counter.longTimeCount = 0;
Flag.MessageResult = MessageBoxResult.None;
CorrectCount++;
LongTimeDetected("保持同一坐姿时间太长了,休息一下吧:)");
this.mainWindow.TimeLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.CountLabel.Content = CorrectCount.ToString();
}));
}
}
private void CheckForShortTimeSignal(Object source, ElapsedEventArgs e)
{
if (ShortTimeDetected != null && Flag.Message == false && Counter.shortTimeCount > Args.shortMilliseconds)
{
Counter.shortTimeCount = 0;
Flag.MessageResult = MessageBoxResult.None;
CorrectCount++;
ShortTimeDetected("坐姿不端正哦,调整一下吧:)");
this.mainWindow.TimeLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.CountLabel.Content = CorrectCount.ToString();
}));
}
}
}
//各种事件处理方法
public class Handlers
{
MainWindow mainWindow;
public Handlers(MainWindow w)
{
this.mainWindow = w;
}
//坐标检测函数
public void EyeDetect()
{
Coord.lastEyeX = Coord.EyeX;
Coord.lastEyeY = Coord.EyeY;
dllmain(ref Coord.EyeX, ref Coord.EyeY);
}
//前期分析函数
public void PreAnalyse()
{
if (Coord.stableX > 0)
return;
if (Coord.EyeX <= 0)
{
Flag.Foucus = false;
this.mainWindow.StatusLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.StatusLabel.Content = "正在检测坐姿,请以舒服的姿势坐端正,并眨眼";
}));
return;
}
else
Flag.Foucus = true;
if (Counter.longTimeCount > Args.preAnaMilliseconds)
{
Coord.stableX = Coord.EyeX;
Coord.stableY = Coord.EyeY;
this.mainWindow.StatusLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.StatusLabel.Content = "坐姿保持很好!";
}));
return;
}
else if (Math.Sqrt(Math.Pow(Coord.EyeX - Coord.lastEyeX, 2) + Math.Pow(Coord.EyeY - Coord.lastEyeY, 2)) < 5)
Counter.longTimeCount += 100;
else
Counter.longTimeCount = 0;
}
//坐标分析函数
public void Analyse()
{
if (Coord.EyeX <= 0)
{
Flag.Foucus = false;
return;
}
else
Flag.Foucus = true;
if (Flag.Message == true || Coord.stableX <= 0)
return;
if (Math.Sqrt(Math.Pow(Coord.EyeX - Coord.lastEyeX, 2) + Math.Pow(Coord.EyeY - Coord.lastEyeY, 2)) < Args.Threshold
&& Math.Sqrt(Math.Pow(Coord.EyeX - Coord.stableX, 2) + Math.Pow(Coord.EyeY - Coord.stableY, 2)) <= Args.Threshold)
Counter.longTimeCount += 100;
else
Counter.longTimeCount = 0;
if (Math.Sqrt(Math.Pow(Coord.EyeX - Coord.lastEyeX, 2) + Math.Pow(Coord.EyeY - Coord.lastEyeY, 2)) < Args.Threshold
&& Math.Sqrt(Math.Pow(Coord.EyeX - Coord.stableX, 2) + Math.Pow(Coord.EyeY - Coord.stableY, 2)) > Args.Threshold)
{
Counter.longTimeCount = 0;
Counter.shortTimeCount += 100;
this.mainWindow.StatusLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.StatusLabel.Content = "坐姿不端正哦!请注意调整!";
}));
return;
}
else
{
Counter.shortTimeCount = 0;
this.mainWindow.StatusLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.StatusLabel.Content = "坐姿保持很好!";
}));
return;
}
}
//运行时间
public void RunTime()
{
Counter.msTimeCount += 100;
if (Counter.msTimeCount / 1000 == 1)
{
Counter.sTimeCount++;
TimeSpan time = new TimeSpan(0, 0, Counter.sTimeCount);
StrTime = time.ToString();
this.mainWindow.TimeLabel.Dispatcher.Invoke(new Action(
delegate
{
mainWindow.TimeLabel.Content = StrTime;
}));
Counter.msTimeCount = 0;
}
}
//提醒界面
public void DisplayMessage(string message)
{
Flag.Message = true;
Flag.MessageResult = MessageBox.Show(message, Args.Message1);
if (Flag.MessageResult != MessageBoxResult.OK)
Flag.Message = true;
else if (Flag.MessageResult == MessageBoxResult.OK)
Flag.Message = false;
}
}
//主逻辑
public MainWindow()
{
InitializeComponent();
init();
DataFile TimeArg = new DataFile();
Args.longMilliseconds = TimeArg.RelaxSeconds * 1000;
Args.shortMilliseconds = TimeArg.CorrectSeconds * 1000;
var w = (MainWindow)Window.GetWindow(this);
Events myDetection = new Events(w);
Handlers myHandlers = new Handlers(w);
myDetection.TimeToDetect += new Detection(myHandlers.EyeDetect);
myDetection.TimeToDetect += new Detection(myHandlers.RunTime);
myDetection.TimeToAnalyse += new AnalyseArg(myHandlers.PreAnalyse);
myDetection.TimeToAnalyse += new AnalyseArg(myHandlers.Analyse);
myDetection.LongTimeDetected += new Correction(myHandlers.DisplayMessage);
myDetection.ShortTimeDetected += new Correction(myHandlers.DisplayMessage);
myDetection.Detect();
}
//“设置”修改主窗口参数
public void ChangeArgs()
{
DataFile TimeArg = new DataFile();
Args.longMilliseconds = TimeArg.RelaxSeconds * 1000;
Args.shortMilliseconds = TimeArg.CorrectSeconds * 1000;
}
//设置键
private void Preference_Click(object sender, RoutedEventArgs e)
{
var w = (MainWindow)Window.GetWindow(this);
SubWindow sw = new SubWindow(w);
sw.ChangArgEvent += new SubWindow.ChangArgs(ChangeArgs);
sw.Show();
return;
}
}
}