-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
239 lines (200 loc) · 7.86 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
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.Windows.Threading;
using System.IO;
using OpenNUI.CSharp.Library;
namespace OpenNUI.Samples.ColorBasics
{
/// <summary>
/// Interaction logic for MainWindow
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Instant of NuiApplication for Connect OpenNUI Service
/// </summary>
NuiApplication nuiApp = null;
/// <summary>
/// Instant of NuiSensor
/// </summary>
NuiSensor useSensor = null;
/// <summary>
/// the Timer for getting frames from OpenNUI Service
/// </summary>
private System.Timers.Timer openNUIFrameTimer;
/// <summary>
/// worker for openNUIFrameTimer
/// </summary>
Action act;
/// <summary>
/// bitmap for drawing colorframe
/// </summary>
WriteableBitmap bitmap = null;
/// <summary>
/// Initializes a new instance of the MainWindow class.
/// </summary>
public MainWindow()
{
// create nuiApp instance and setting NuiApplication's name
nuiApp = new NuiApplication("OpenNUI.Samples.ColorBasics");
// register delegate when nui sensor connected, disconnected
nuiApp.OnSensorConnected += nuiApp_OnSensorConnected;
nuiApp.OnSensorDisconnected += nuiApp_OnSensorDisconnected;
try
{
nuiApp.Start();
}
// when exception catched. it means OpenNUI dosen't installed
catch
{
//show error messagebox
MessageBox.Show("OpenNUI dosen't installed", "error", MessageBoxButton.OK, MessageBoxImage.Error);
//program exit
Environment.Exit(0);
}
// worker for openNUIFrameTimer
act = new Action(delegate()
{
//do not work when usesensor is null
if (useSensor == null)
return;
//get the color frame
ImageData colorFrame = useSensor.GetColorFrame();
//do not work when color frame is null (faild to GetColorFrame())
if (colorFrame == null)
return;
int numPixels = colorFrame.FrameData.Length / sizeof(uint);
bitmap.Lock();
unsafe
{
fixed (byte* pSrcData = &colorFrame.FrameData[0])
{
uint* pCurrent = (uint*)pSrcData;
uint* pBitmapData = (uint*)bitmap.BackBuffer;
for (int n = 0; n < numPixels; n++)
{
uint x = *(pCurrent++);
*(pBitmapData + n) =
(x & 0xFF000000) |
(x & 0x00FF0000) |
(x & 0x0000FF00) |
(x & 0x000000FF);
}
}
}
bitmap.AddDirtyRect(new Int32Rect(0, 0, colorFrame.Description.Width, colorFrame.Description.Height));
bitmap.Unlock();
});
// create openNUIFrameTimer instance
openNUIFrameTimer = new System.Timers.Timer();
//set the timer interval to 60fps
openNUIFrameTimer.Interval = 1000 / 60;
//set the timer elapsed callback
openNUIFrameTimer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
{
try
{
//use invoke for access to the this wpf window's ui
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, act);
}
catch { }
};
//set the timer autoreset to true for loop
openNUIFrameTimer.AutoReset = true;
//openNUIFrameTimer start
openNUIFrameTimer.Start();
// initialize the components (controls) of the window
InitializeComponent();
}
/// <summary>
/// Handles the nui sensor disconnected
/// </summary>
/// <param name="sensor">connected sensor</param>
void nuiApp_OnSensorDisconnected(NuiSensor sensor)
{
if (useSensor == sensor)
{
//set usesensor to null when usesensor is disconnected
useSensor = null;
//set bitmap to null
bitmap = null;
//use invoke for access to the this wpf window's ui
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
//init sensor's name,vendor textbox
sensorNameTB.Text = "";
sensorVendorTB.Text = "";
}));
}
}
/// <summary>
/// Handles the nui sensor connected
/// </summary>
/// <param name="sensor">connected sensor</param>
void nuiApp_OnSensorConnected(NuiSensor sensor)
{
if (useSensor == null)
{
//set usesensor to now connected sensor when usesensor is empty
useSensor = sensor;
//open color frame
useSensor.OpenColorFrame();
//use invoke for access to the this wpf window's ui
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
//create bitmap instance
bitmap = new WriteableBitmap(
useSensor.ColorInfo.Width,
useSensor.ColorInfo.Height, 96, 96, PixelFormats.Bgra32, null);
//set the colorImage's source to colorframe's bitmap
colorImage.Source = bitmap;
//update sensor's name,vendor textbox
sensorNameTB.Text = useSensor.Name;
sensorVendorTB.Text = useSensor.Vendor;
}));
}
}
/// <summary>
/// Handles the user clicking on the screenshot button
/// </summary>
/// <param name="sender">object sending the event</param>
/// <param name="e">event arguments</param>
private void Screenshot_MouseDown(object sender, MouseButtonEventArgs e)
{
if (this.bitmap != null)
{
// create a png bitmap encoder which knows how to save a .png file
BitmapEncoder encoder = new PngBitmapEncoder();
// create frame from the writable bitmap and add to encoder
encoder.Frames.Add(BitmapFrame.Create(this.bitmap));
string time = System.DateTime.Now.ToString("hh'-'mm'-'ss", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string path = System.IO.Path.Combine(myPhotos, "OpenNUIScreenshot-Color-" + time + ".png");
// write the new file to disk
try
{
// FileStream is IDisposable
using (FileStream fs = new FileStream(path, FileMode.Create))
{
encoder.Save(fs);
}
}
catch (IOException)
{
}
}
}
}
}