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

Skip to content

Commit a9405c1

Browse files
committed
Added qtKinectVideo to the project. It's a Qt port of "Color Basics" \(Developer ToolKit Browser\).
1 parent 554b353 commit a9405c1

File tree

4 files changed

+341
-0
lines changed

4 files changed

+341
-0
lines changed

qtKinectVideo/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
2+
*
3+
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
4+
* To view a copy of this license, visit:
5+
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
6+
*
7+
* Or to read the human-readable summary of the license:
8+
* https://creativecommons.org/licenses/by-sa/2.5/
9+
*/
10+
#include "window.h"
11+
#include <iostream>
12+
13+
#include <QApplication>
14+
15+
16+
int main(int argc, char* argv[])
17+
{
18+
QApplication app(argc, argv);
19+
20+
Window window;
21+
window.show();
22+
23+
return app.exec();
24+
}

qtKinectVideo/qtKinectVideo.pro

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# INSTALL
2+
# -------
3+
#
4+
# 1- Microsoft Visual Studio 2013 Express (for Desktop, online installer)
5+
# https://www.visualstudio.com/downloads/download-visual-studio-vs
6+
#
7+
# 2- Qt 5.5.0 for Windows 32-bit (VS 2013, 633 MB)
8+
# http://download.qt.io/official_releases/qt/5.5/5.5.0/qt-opensource-windows-x86-msvc2013-5.5.0.exe
9+
#
10+
# 3- Kinect for Windows SDK v1.8 (222MB)
11+
# https://www.microsoft.com/en-us/download/confirmation.aspx?id=40278
12+
#
13+
# 4- Kinect for Windows Developer Toolkit v1.8 (384MB)
14+
# http://www.microsoft.com/en-us/download/confirmation.aspx?id=40276
15+
#
16+
17+
QT += widgets
18+
19+
SOURCES += \
20+
main.cpp \
21+
window.cpp
22+
23+
win32 {
24+
INCLUDEPATH += "C:\\Program Files\\Microsoft SDKs\\Kinect\\v1.8\\inc"
25+
LIBS += "C:\\Program Files\\Microsoft SDKs\\Kinect\\v1.8\\lib\\x86\\Kinect10.lib"
26+
}
27+
28+
HEADERS += \
29+
window.h
30+

qtKinectVideo/window.cpp

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
2+
*
3+
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
4+
* To view a copy of this license, visit:
5+
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
6+
*
7+
* Or to read the human-readable summary of the license:
8+
* https://creativecommons.org/licenses/by-sa/2.5/
9+
*/
10+
#include "window.h"
11+
12+
#include <iostream>
13+
14+
#include <QApplication>
15+
16+
17+
//HANDLE m_hNextColorFrameEvent;
18+
19+
20+
Window::Window()
21+
: _tick_ms(33), _ar_mode(Qt::IgnoreAspectRatio), _fps(0),
22+
_video_width(640), _video_height(480), _timer(NULL), _nui_sensor(NULL)
23+
{
24+
setWindowTitle("QT demo with Kinect");
25+
resize(_video_width, _video_height);
26+
27+
// Initialize device and prepare it to send RGB data
28+
if (!_initKinect())
29+
{
30+
std::cout << "* FAILED! Is your Kinect device connected?" << std::endl;
31+
return;
32+
}
33+
34+
// Start timer to read frames from Kinect
35+
_timer = new QTimer();
36+
_timer->start(_tick_ms);
37+
QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(_tick()));
38+
}
39+
40+
Window::~Window()
41+
{
42+
if (_timer)
43+
{
44+
_timer->stop();
45+
delete _timer;
46+
}
47+
}
48+
49+
/* _tick(): called every few milliseconds.
50+
* It reads data from Kinect's capture interface and saves the frame as QImage.
51+
*/
52+
void Window::_tick()
53+
{
54+
if (!_nui_sensor)
55+
{
56+
std::cout << "_tick !!! _initKinect() failed, didn't it?!" << std::endl;
57+
return;
58+
}
59+
60+
/* Read data from Kinect */
61+
62+
NUI_IMAGE_FRAME _nui_frame;
63+
if (_nui_sensor->NuiImageStreamGetNextFrame(_rgb_stream, 0, &_nui_frame) < 0)
64+
return;
65+
66+
NUI_LOCKED_RECT locked_rect;
67+
INuiFrameTexture* texture = _nui_frame.pFrameTexture;
68+
HRESULT result = texture->LockRect(0, &locked_rect, NULL, 0);
69+
if (result == S_OK && locked_rect.Pitch != 0)
70+
{
71+
NUI_SURFACE_DESC surface_desc;
72+
texture->GetLevelDesc(0, &surface_desc);
73+
if (_video_width != surface_desc.Width || _video_height != surface_desc.Height)
74+
{
75+
_video_width = surface_desc.Width;
76+
_video_height = surface_desc.Height;
77+
resize(_video_width, _video_height);
78+
}
79+
80+
// Copy pixels to QImage
81+
_image = QImage((const uchar*)locked_rect.pBits,
82+
surface_desc.Width, surface_desc.Height,
83+
QImage::Format_RGB32);
84+
85+
// Trigger paint event to redraw the window
86+
if (!_image.isNull())
87+
emit update();
88+
}
89+
90+
texture->UnlockRect(0);
91+
92+
_nui_sensor->NuiImageStreamReleaseFrame(_rgb_stream, &_nui_frame);
93+
}
94+
95+
bool Window::_initKinect()
96+
{
97+
int sensor_count = 0;
98+
if (NuiGetSensorCount(&sensor_count) < 0)
99+
{
100+
std::cout << "_initKinect !!! NuiGetSensorCount() failed" << std::endl;
101+
return false;
102+
}
103+
104+
std::cout << "* Number of compatible sensors detected: " << sensor_count << std::endl;
105+
106+
/* Look at each Kinect sensor */
107+
108+
for (int i = 0; i < sensor_count; ++i)
109+
{
110+
// Create the sensor so we can check status, if we can't create it, move on to the next
111+
if (NuiCreateSensorByIndex(i, &_nui_sensor) < 0)
112+
continue;
113+
114+
// Get the status of the sensor, and if connected, then we can initialize it
115+
if (_nui_sensor->NuiStatus() == S_OK)
116+
{
117+
std::cout << "* Selected sensor #" << i << std::endl;
118+
break;
119+
}
120+
121+
// This sensor wasn't OK, so release it since we're not using it
122+
_nui_sensor->Release();
123+
}
124+
125+
if (_nui_sensor == NULL)
126+
{
127+
std::cout << "_initKinect !!! No compatible sensors detected" << std::endl;
128+
return false;
129+
}
130+
131+
// Initialize the Kinect and specify that we'll be using color
132+
if (_nui_sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR) >= 0) // if (SUCCEEDED(hr))
133+
{
134+
// Open a color image stream to receive color frames
135+
if (_nui_sensor->NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, // Depth camera or rgb camera?
136+
NUI_IMAGE_RESOLUTION_640x480, // Image resolution
137+
0, // Image stream flags, e.g. near mode
138+
2, // Number of frames to buffer
139+
NULL, // Event handle
140+
&_rgb_stream) < 0)
141+
{
142+
std::cout << "_initKinect !!! NuiImageStreamOpen() failed" << std::endl;
143+
return false;
144+
}
145+
}
146+
147+
return true;
148+
}
149+
150+
void Window::paintEvent(QPaintEvent* e)
151+
{
152+
QWidget::paintEvent(e);
153+
154+
// Draw a frame
155+
QPainter painter(this);
156+
_draw_video_frame(painter);
157+
}
158+
159+
void Window::_draw_video_frame(QPainter& painter)
160+
{
161+
// When no image is loaded, paint a black frame
162+
if (_image.isNull())
163+
{
164+
// QImage is not ready yet
165+
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
166+
return;
167+
}
168+
169+
// To simply draw on the window, execute the code below.
170+
//painter.drawImage(QPoint(0, 0), _image);
171+
172+
// But we are drawing the image according to AR settings:
173+
// http://doc.qt.digia.com/stable/qt.html#AspectRatioMode-enum
174+
switch (_ar_mode)
175+
{
176+
default:
177+
case Qt::IgnoreAspectRatio:
178+
{
179+
painter.drawImage(QRectF(QPoint(0, 0), QSize(_video_width, _video_height)),
180+
_image,
181+
QRectF(QPoint(0, 0), _image.size()));
182+
}
183+
break;
184+
185+
case Qt::KeepAspectRatio:
186+
{
187+
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
188+
189+
QImage scaled_img = _image.scaled(QSize(width(), height()), Qt::KeepAspectRatio, Qt::FastTransformation);
190+
191+
painter.drawImage(qRound(width()/2.0) - qRound(scaled_img.size().width()/2.0),
192+
qRound(height()/2.0) - qRound(scaled_img.size().height()/2.0),
193+
scaled_img);
194+
}
195+
break;
196+
197+
case Qt::KeepAspectRatioByExpanding:
198+
{
199+
QImage scaled_img = _image.scaled(QSize(width(), height()), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
200+
201+
painter.drawImage(QRectF(QPoint(0, 0), QSize(width(), height())),
202+
scaled_img,
203+
QRectF(QPoint(qRound(scaled_img.size().width()/2.0) - qRound(width()/2.0),
204+
qRound(scaled_img.size().height()/2.0) - qRound(height()/2.0)),
205+
QSize(width(), height())));
206+
}
207+
break;
208+
}
209+
}
210+
211+
void Window::keyPressEvent(QKeyEvent* event)
212+
{
213+
switch (event->key())
214+
{
215+
// M: changes aspect ratio mode
216+
case Qt::Key_M:
217+
{
218+
if (_ar_mode == Qt::IgnoreAspectRatio)
219+
_ar_mode = Qt::KeepAspectRatio;
220+
else if (_ar_mode == Qt::KeepAspectRatio)
221+
_ar_mode = Qt::KeepAspectRatioByExpanding;
222+
else
223+
_ar_mode = Qt::IgnoreAspectRatio;
224+
}
225+
break;
226+
227+
// ESC: exit application
228+
case Qt::Key_Escape:
229+
{
230+
std::cout << "* Bye bye." << std::endl;
231+
QApplication::instance()->quit();
232+
}
233+
break;
234+
}
235+
}

qtKinectVideo/window.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* Copyright (C) 2015 Karl Phillip Buhr <[email protected]>
2+
*
3+
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
4+
* To view a copy of this license, visit:
5+
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
6+
*
7+
* Or to read the human-readable summary of the license:
8+
* https://creativecommons.org/licenses/by-sa/2.5/
9+
*/
10+
#pragma once
11+
12+
// Kinect for Windows Architecture https://msdn.microsoft.com/en-us/library/jj131023.aspx
13+
#include <windows.h>
14+
#include <NuiApi.h>
15+
16+
#include <QImage>
17+
#include <QKeyEvent>
18+
#include <QMainWindow>
19+
#include <QPainter>
20+
#include <QTimer>
21+
22+
23+
class Window : public QMainWindow
24+
{
25+
Q_OBJECT
26+
27+
public:
28+
Window();
29+
~Window();
30+
31+
void paintEvent(QPaintEvent* e);
32+
void keyPressEvent(QKeyEvent* event);
33+
34+
35+
private slots:
36+
void _tick();
37+
38+
private:
39+
bool _initKinect();
40+
void _draw_video_frame(QPainter& painter);
41+
42+
int _tick_ms;
43+
Qt::AspectRatioMode _ar_mode;
44+
int _fps;
45+
int _video_width;
46+
int _video_height;
47+
QImage _image;
48+
QTimer* _timer;
49+
50+
INuiSensor* _nui_sensor;
51+
HANDLE _rgb_stream; //32bit
52+
};

0 commit comments

Comments
 (0)