|
| 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 | +} |
0 commit comments