配置了下,原来在Qt中是可以使用KinectV2编程的,我之前一直以为必须中visual studio囧~
这次放出最简单的KinectV2数据在Qt中显示的例子。有个地方很怪,kinect数据本来是按照b、g、r、a排列,可以用这个Qt QImage方法显示(qt教程中说The image is stored using a 32-bit RGB format (0xffRRGGBB).这个32bit的数字,在windows x86下内存恰好按照从低到高排序,也就是0xBB 0xGG 0xRR 0xff,因此恰恰对应上了)。由于Kinect中的a数据默认是0,所以一般请不要使用,不然就全透明无法显示了。。
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 |
#include "widget.h" #include "ui_widget.h" #include "inc/Kinect.h" #include <QTimer> template<class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) { if (pInterfaceToRelease != NULL) { pInterfaceToRelease->Release(); pInterfaceToRelease = NULL; } } static const int cColorWidth = 1920; static const int cColorHeight = 1080; IKinectSensor* m_pKinectSensor;// Current Kinect IColorFrameReader* m_pColorFrameReader;// Color reader RGBQUAD* m_pColorRGBX; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); m_pKinectSensor = NULL; m_pColorFrameReader = NULL; m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight];// create heap storage for color pixel data in RGBX format init(); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateKinectData())); timer->start(33); } Widget::~Widget() { delete ui; if (m_pColorRGBX) { delete [] m_pColorRGBX; m_pColorRGBX = NULL; } SafeRelease(m_pColorFrameReader);// done with color frame reader if (m_pKinectSensor) { m_pKinectSensor->Close();// close the Kinect Sensor } SafeRelease(m_pKinectSensor); } void Widget::updateKinectData() { if (!m_pColorFrameReader) { return; } IColorFrame* pColorFrame = NULL; HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame); if (SUCCEEDED(hr)) { IFrameDescription* pFrameDescription = NULL; int nWidth = 0; int nHeight = 0; ColorImageFormat imageFormat = ColorImageFormat_None; UINT nBufferSize = 0; RGBQUAD *pBuffer = NULL; if (SUCCEEDED(hr)) { hr = pColorFrame->get_FrameDescription(&pFrameDescription); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Width(&nWidth); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Height(&nHeight); } if (SUCCEEDED(hr)) { hr = pColorFrame->get_RawColorImageFormat(&imageFormat); } if (SUCCEEDED(hr)) { if (imageFormat == ColorImageFormat_Bgra) { hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer)); } else if (m_pColorRGBX) { pBuffer = m_pColorRGBX; nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD); hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra); } else { hr = E_FAIL; } } if (SUCCEEDED(hr)) { ui->label->setPixmap(QPixmap::fromImage(QImage((uchar*)pBuffer, nWidth , nHeight, QImage::Format_RGB32)).scaledToWidth(800)); //ProcessColor(pBuffer, nWidth, nHeight); } SafeRelease(pFrameDescription); } SafeRelease(pColorFrame); } void Widget::init() { HRESULT hr; hr = GetDefaultKinectSensor(&m_pKinectSensor); if (FAILED(hr)) { return; } if (m_pKinectSensor) { // Initialize the Kinect and get the color reader IColorFrameSource* pColorFrameSource = NULL; hr = m_pKinectSensor->Open(); if (SUCCEEDED(hr)) { hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource); } if (SUCCEEDED(hr)) { hr = pColorFrameSource->OpenReader(&m_pColorFrameReader); } SafeRelease(pColorFrameSource); } if (!m_pKinectSensor || FAILED(hr)) { printf("No ready Kinect found! \n"); return; } } |
运行效果: