KinectV2 in Qt 在Qt中使用KinectV2编程

固定链接: http://brightguo.com/kinectv2-in-qt-color/

配置了下,原来在Qt中是可以使用KinectV2编程的,我之前一直以为必须中visual studio囧~

CSDN资源文件下载

Github代码

这次放出最简单的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,所以一般请不要使用,不然就全透明无法显示了。。

#include "widget.h"
#include "ui_widget.h"
#include "inc/Kinect.h"
#include 
template
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(&pBuffer));
            }
            else if (m_pColorRGBX)
            {
                pBuffer = m_pColorRGBX;
                nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
                hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast(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;
    }
}


运行效果:

KinectV2 in Qt 在Qt中使用KinectV2编程_第1张图片


你可能感兴趣的:(Kinect,Qt,C++)