Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd885b3

Browse files
committed
qtKinectVideo: improved messages, shutting down NUI, support for elevation change.
1 parent 14008a1 commit cd885b3

File tree

1 file changed

+87
-18
lines changed

1 file changed

+87
-18
lines changed

qtKinectVideo/window.cpp

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#include <QApplication>
1515

1616

17-
//HANDLE m_hNextColorFrameEvent;
18-
19-
2017
Window::Window()
2118
: _tick_ms(33), _ar_mode(Qt::IgnoreAspectRatio), _fps(0),
2219
_video_width(640), _video_height(480), _timer(NULL), _nui_sensor(NULL)
@@ -27,10 +24,17 @@ Window::Window()
2724
// Initialize device and prepare it to send RGB data
2825
if (!_initKinect())
2926
{
30-
std::cout << "* FAILED! Is your Kinect device connected?" << std::endl;
27+
std::cout << "* FAILED! Is your Kinect device connected? Is it powered?" << std::endl;
3128
return;
3229
}
3330

31+
std::cout << "* Special keys: " << std::endl;
32+
std::cout << "\tESC - Quit application" << std::endl;
33+
std::cout << "\tM - Change aspect ratio" << std::endl;
34+
std::cout << "\tUp - Increase elevation" << std::endl;
35+
std::cout << "\tDown - Decrease elevation" << std::endl;
36+
std::cout << "\tLEFT - Reset elevation to 0 degrees" << std::endl;
37+
3438
// Start timer to read frames from Kinect
3539
_timer = new QTimer();
3640
_timer->start(_tick_ms);
@@ -44,6 +48,10 @@ Window::~Window()
4448
_timer->stop();
4549
delete _timer;
4650
}
51+
52+
// Shutdown Kinect properly
53+
if (_nui_sensor)
54+
_nui_sensor->NuiShutdown();
4755
}
4856

4957
/* _tick(): called every few milliseconds.
@@ -53,7 +61,7 @@ void Window::_tick()
5361
{
5462
if (!_nui_sensor)
5563
{
56-
std::cout << "_tick !!! _initKinect() failed, didn't it?!" << std::endl;
64+
std::cout << "_tick !!! _initKinect() failed: ABORT!" << std::endl;
5765
return;
5866
}
5967

@@ -82,6 +90,7 @@ void Window::_tick()
8290
surface_desc.Width, surface_desc.Height,
8391
QImage::Format_RGB32);
8492

93+
8594
// Trigger paint event to redraw the window
8695
if (!_image.isNull())
8796
emit update();
@@ -95,7 +104,7 @@ void Window::_tick()
95104
bool Window::_initKinect()
96105
{
97106
int sensor_count = 0;
98-
if (NuiGetSensorCount(&sensor_count) < 0)
107+
if (NuiGetSensorCount(&sensor_count) < 0 || sensor_count < 1)
99108
{
100109
std::cout << "_initKinect !!! NuiGetSensorCount() failed" << std::endl;
101110
return false;
@@ -105,28 +114,39 @@ bool Window::_initKinect()
105114

106115
/* Look at each Kinect sensor */
107116

108-
for (int i = 0; i < sensor_count; ++i)
117+
bool sensor_found = false;
118+
for (int i = 0; i < sensor_count; i++)
109119
{
120+
std::cout << "* Checking sensor #" << i << " ... ";
121+
110122
// Create the sensor so we can check status, if we can't create it, move on to the next
111123
if (NuiCreateSensorByIndex(i, &_nui_sensor) < 0)
124+
{
125+
std::cout << " SKIPPED!" << std::endl;
112126
continue;
127+
}
113128

114129
// Get the status of the sensor, and if connected, then we can initialize it
115-
if (_nui_sensor->NuiStatus() == S_OK)
130+
if (_nui_sensor->NuiStatus() != S_OK)
116131
{
117-
std::cout << "* Selected sensor #" << i << std::endl;
132+
std::cout << " NOT READY!" << std::endl;
133+
}
134+
else
135+
{
136+
std::cout << " SELECTED!" << std::endl;
137+
sensor_found = true;
118138
break;
119139
}
120140

121141
// This sensor wasn't OK, so release it since we're not using it
122142
_nui_sensor->Release();
123-
}
143+
}
124144

125-
if (_nui_sensor == NULL)
126-
{
127-
std::cout << "_initKinect !!! No compatible sensors detected" << std::endl;
145+
if (_nui_sensor == NULL || !sensor_found)
146+
{
147+
std::cout << "_initKinect !!! No ready Kinect found." << std::endl;
128148
return false;
129-
}
149+
}
130150

131151
// Initialize the Kinect and specify that we'll be using color
132152
if (_nui_sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR) >= 0) // if (SUCCEEDED(hr))
@@ -212,24 +232,73 @@ void Window::keyPressEvent(QKeyEvent* event)
212232
{
213233
switch (event->key())
214234
{
235+
// ESC: exit application
236+
case Qt::Key_Escape:
237+
{
238+
std::cout << "* (ESC) Bye bye." << std::endl;
239+
QApplication::instance()->quit();
240+
}
241+
break;
242+
215243
// M: changes aspect ratio mode
216244
case Qt::Key_M:
217245
{
218246
if (_ar_mode == Qt::IgnoreAspectRatio)
247+
{
219248
_ar_mode = Qt::KeepAspectRatio;
249+
std::cout << "* (M) AR = keep aspect ratio" << std::endl;
250+
}
220251
else if (_ar_mode == Qt::KeepAspectRatio)
252+
{
221253
_ar_mode = Qt::KeepAspectRatioByExpanding;
254+
std::cout << "* (M) AR = keep aspect ratio by expanding" << std::endl;
255+
}
222256
else
257+
{
223258
_ar_mode = Qt::IgnoreAspectRatio;
259+
std::cout << "* (M) AR = ignore aspect ratio" << std::endl;
260+
}
224261
}
225262
break;
226263

227-
// ESC: exit application
228-
case Qt::Key_Escape:
264+
// Up arrow: increases elevation angle by 2 degrees
265+
case Qt::Key_Up:
229266
{
230-
std::cout << "* Bye bye." << std::endl;
231-
QApplication::instance()->quit();
267+
long angle = -1;
268+
if (NuiCameraElevationGetAngle(&angle) >= 0)
269+
{
270+
angle += 2;
271+
if (NuiCameraElevationSetAngle(angle) >= 0)
272+
std::cout << "* (UP) Current elevation angle: " << angle << std::endl;
273+
}
274+
}
275+
break;
276+
277+
// Down arrow: decreases elevation angle by 2 degrees
278+
case Qt::Key_Down:
279+
{
280+
long angle = -1;
281+
if (NuiCameraElevationGetAngle(&angle) >= 0)
282+
{
283+
angle -= 2;
284+
if (NuiCameraElevationSetAngle(angle) >= 0)
285+
std::cout << "* (DOWN) Current elevation angle: " << angle << std::endl;
286+
}
287+
}
288+
break;
289+
290+
// Left arrow: reset elevation angle
291+
case Qt::Key_Left:
292+
{
293+
long angle = -1;
294+
if (NuiCameraElevationGetAngle(&angle) >= 0)
295+
{
296+
angle = 0;
297+
if (NuiCameraElevationSetAngle(angle) >= 0)
298+
std::cout << "* (LEFT) Current elevation angle: " << angle << std::endl;
299+
}
232300
}
233301
break;
302+
234303
}
235304
}

0 commit comments

Comments
 (0)