windows下qt5 kinect 2.0开发与环境配置

上一次挑战杯我们用kinect1.8做了一个体感3d试衣系统,那时就想用qt来开发了,由于那时候很多东西都不懂,怎么也不知道怎样去配置环境,只能在vs2013中开发,结合qt开发界面。直到出了kinect2.0后才配置成功完全用qt 来开发Kinect!而且完美运行。注意一点就是使用的qt必须是
qt forwindows msvc的才能正常编译。
kinect 2.0开发系统要求:win8.1以上
kinect 1.8开发系统要求:win7以上

工程文件的配置
kinectBasicBuildingExplorer.pro

TEMPLATE = app

QT += qml quick widgets core

SOURCES += main.cpp \
    kinect/kinectsensor.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

#kinect_V_20 plugin
INCLUDEPATH += $$(KINECTSDK20_DIR)\inc

LIBS += $$(KINECTSDK20_DIR)\Lib\x86\kinect20.lib
#kinect_V_20 plugin

CONFIG += no_lflags_merge
#某些winapi 的库要用到,(那时候一个个的调才成功,呵)
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\kernel32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\user32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\gdi32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\winspool.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\comdlg32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\advapi32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\shell32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\oleaut32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\uuid.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\odbc32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\odbccp32.lib"
LIBS += "C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\ole32.lib"
LIBS += "-LC:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86"

HEADERS += \
    kinect/kinectsensor.h

实现kinect功能代码,本代码功能是实现了右手当作鼠标来操控程序,当手握紧相当于鼠标按下左键,当手控制鼠标悬停在某个按钮的时候,相当于鼠标的点击事件。

kinectsensor.h

#ifndef KINECTSENSOR_H
#define KINECTSENSOR_H

// Windows Header Files必须用到这两个头文件才能使用Kienct api,而且要在Kinect.h之前引用
#include 
#include 


// Kinect Header files
#include 


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class KinectSensor : public QObject
{
    Q_OBJECT
public:
    explicit KinectSensor(QObject *parent = 0){
        m_pKinectSensor=NULL;
        m_pCoordinateMapper=NULL;
        m_pBodyFrameReader=NULL;
        currentPBody=NULL;


        InitializeDefaultSensor();

//        bitmap=new QBitmap("qrc:/imgSource/cursor.png");
        cursor=new QCursor(Qt::CrossCursor);
        cursor->setShape(Qt::DragMoveCursor);
    }

    /// 
    /// Main processing function
    /// 
     Q_INVOKABLE void updatebody();

    /// 
    /// Initializes the default Kinect sensor
    /// 
    /// S_OK on success, otherwise failure code
    Q_INVOKABLE HRESULT                 InitializeDefaultSensor();

    /// 
    /// Handle new body data
    /// timestamp of frame
    /// body data count
    /// body data in frame
    /// 
//    Q_INVOKABLE void                    ProcessBody(INT64 nTime, int nBodyCount, IBody** ppBodies);

    Q_INVOKABLE bool getCurrentBody(IBody **ppbodies);
    Q_INVOKABLE  void getBodyJoint();
    Q_INVOKABLE  void mapJointsToXYCoord();


    Q_INVOKABLE   QString getLeftHandState();
    Q_INVOKABLE  QString getRightHandState();

    Q_INVOKABLE float getLeftHandx(){
        return leftHandPoint.X;
    }
    Q_INVOKABLE float getLeftHandy(){
        return leftHandPoint.Y;
    }
    Q_INVOKABLE float getRightHandx(){
        return rightHandPoint.X;
    }
    Q_INVOKABLE float getRightHandy(){
        return rightHandPoint.Y;
    }

    Q_INVOKABLE void setWinPos(int x,int y){
        winx=x;
        winy=y;
    }

    Q_INVOKABLE void refreshMousePos(int mousex,int mousey){
        cursor->setPos(mousex,mousey);
    }
    Q_INVOKABLE void sendMouseLeftPressEvent(){
        QPoint pos;
        pos.setX(cursor->pos().x());
        pos.setY(cursor->pos().y());
        QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
        QApplication::sendEvent(QApplication::focusWindow(),mevent);
        delete mevent;
    }
    Q_INVOKABLE void sendMouseLeftReleaseEvent(){
        QPoint pos;
        pos.setX(cursor->pos().x());
        pos.setY(cursor->pos().y());
        QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
        QApplication::sendEvent(QApplication::focusWindow(),mevent);
        delete mevent;
    }

    Q_INVOKABLE void sendMouseDragEvent(){
        QPoint pos;
        pos.setX(cursor->pos().x());
        pos.setY(cursor->pos().y());
         QMouseEvent *mevent=new QMouseEvent(QEvent::DragMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
         QApplication::sendEvent(QApplication::focusWindow(),mevent);
         delete mevent;
    }

    Q_INVOKABLE void sentMouseRightPressEvent(){

    }
    Q_INVOKABLE void sentMouseRightReleaseEvent(){

    }
//    Q_INVOKABLE void sentMouseMoveEvent(){
//        QApplication::sendEvent(QApplication::focusObject(),mouseMoveEvent);
//    }
    Q_INVOKABLE void setnMouseLeftClickEvent(){
        QPoint pos;
        pos.setX(cursor->pos().x());
        pos.setY(cursor->pos().y());
         QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonDblClick, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
         QApplication::sendEvent(QApplication::focusWindow(),mevent);
         delete mevent;
//        QApplication::sendEvent(QApplication::focusObject(),mouseLeftClickEvent);
    }

    Q_INVOKABLE double getGuestureWidth(){
        return (topRightJoint.Position.X-topLeftJoint.Position.X);
    }
    Q_INVOKABLE double getCurrentMousePositionx(){
        return (rightHandJoint.Position.X-topRightJoint.Position.X)/(((topRightJoint.Position.X-topLeftJoint.Position.X)));
    }
    Q_INVOKABLE double getCurrentMousePositiony(){
        return (rightHandJoint.Position.Y-((bottomRightJoint.Position.Y+topRightJoint.Position.Y)/2+topRightJoint.Position.Y)/2)/(neckJoint.Position.Y-((bottomRightJoint.Position.Y+topRightJoint.Position.Y)/2+topRightJoint.Position.Y)/2);
    }

    Q_INVOKABLE bool hasTrackingBody(){
        if(currentPBody){
            return true;
        }else{
            return false;
        }
    }

signals:

public slots:

private:

    // Current Kinect
    IKinectSensor*          m_pKinectSensor;
    ICoordinateMapper*      m_pCoordinateMapper;

    IBody *currentPBody;

    //hand state
    HandState leftHandState=HandState_Unknown;
    HandState rightHandState=HandState_Unknown;

    //hand joint (get both hands's position)
    Joint leftHandJoint;
    Joint rightHandJoint;

    Joint topRightJoint;
    Joint bottomRightJoint;
    Joint topLeftJoint;
    Joint headJoint;
    Joint neckJoint;

    //hand color position
    ColorSpacePoint leftHandPoint;
    ColorSpacePoint rightHandPoint;

    // Body reader
    IBodyFrameReader*       m_pBodyFrameReader;

    QCursor *cursor;

    int winx;
    int winy;



};
#endif // KINECTSENSOR_H

kinectsensor.cpp

#include "kinectsensor.h"
#include 


// Safe release for interfaces
template
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
    if (pInterfaceToRelease != NULL)
    {
        pInterfaceToRelease->Release();
        pInterfaceToRelease = NULL;
    }
}

//KinectSensor::KinectSensor(QObject *parent) : QObject(parent)
//{

//}
void KinectSensor::updatebody(){

    if (!m_pBodyFrameReader)
    {
        return;
    }

    IBodyFrame* pBodyFrame = NULL;

    HRESULT hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame);

    if (SUCCEEDED(hr))
    {
        INT64 nTime = 0;

        hr = pBodyFrame->get_RelativeTime(&nTime);

        //body position
        IBody* ppBodies[BODY_COUNT]={0};

        if (SUCCEEDED(hr))
        {
            hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies);
        }

        if (SUCCEEDED(hr))
        {
//            ProcessBody(nTime, BODY_COUNT, ppBodies);
                getCurrentBody(ppBodies);

//            qDebug()<<"updating";
                 getBodyJoint();
                 mapJointsToXYCoord();

                 currentPBody->get_HandLeftState(&leftHandState);
                 currentPBody->get_HandRightState(&rightHandState);
        }

        for (int i = 0; i < _countof(ppBodies); ++i)
        {
            SafeRelease(ppBodies[i]);
        }
    }

    SafeRelease(pBodyFrame);

}


HRESULT KinectSensor::InitializeDefaultSensor(){
    HRESULT hr;
    hr = GetDefaultKinectSensor(&m_pKinectSensor);
    if (FAILED(hr))
    {
        return hr;
    }

    if (m_pKinectSensor)
    {
        // Initialize the Kinect and get coordinate mapper and the body reader
        IBodyFrameSource* pBodyFrameSource = NULL;

        hr = m_pKinectSensor->Open();

        if (SUCCEEDED(hr))
        {
            hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
        }

        if (SUCCEEDED(hr))
        {
            hr = m_pKinectSensor->get_BodyFrameSource(&pBodyFrameSource);
        }

        if (SUCCEEDED(hr))
        {
            hr = pBodyFrameSource->OpenReader(&m_pBodyFrameReader);
        }

        SafeRelease(pBodyFrameSource);
    }

    if (!m_pKinectSensor || FAILED(hr))
    {
//        SetStatusMessage(L"No ready Kinect found!", 10000, true);
        qDebug()<<"No ready Kinect found!";
        return E_FAIL;
    }
    




    return hr;

}

//void KinectSensor::ProcessBody(INT64 nTime, int nBodyCount, IBody** ppBodies){
//    if (m_hWnd){
//        HRESULT hr = EnsureDirect2DResources();

//        if (SUCCEEDED(hr) && m_pRenderTarget && m_pCoordinateMapper)
//        {
//            m_pRenderTarget->BeginDraw();
//            m_pRenderTarget->Clear();

//            RECT rct;
//            GetClientRect(GetDlgItem(m_hWnd, IDC_VIDEOVIEW), &rct);
//            int width = rct.right;
//            int height = rct.bottom;

//            for (int i = 0; i < nBodyCount; ++i)
//            {
//                IBody* pBody = ppBodies[i];
//                if (pBody)
//                {
//                    BOOLEAN bTracked = false;
//                    hr = pBody->get_IsTracked(&bTracked);

//                    if (SUCCEEDED(hr) && bTracked)
//                    {
//                        Joint joints[JointType_Count];
//                        D2D1_POINT_2F jointPoints[JointType_Count];
//                        HandState leftHandState = HandState_Unknown;
//                        HandState rightHandState = HandState_Unknown;

//                        pBody->get_HandLeftState(&leftHandState);
//                        pBody->get_HandRightState(&rightHandState);

//                        hr = pBody->GetJoints(_countof(joints), joints);
//                        if (SUCCEEDED(hr))
//                        {
//                            for (int j = 0; j < _countof(joints); ++j)
//                            {
//                                jointPoints[j] = BodyToScreen(joints[j].Position, width, height);
//                            }

//                            DrawBody(joints, jointPoints);

//                            DrawHand(leftHandState, jointPoints[JointType_HandLeft]);
//                            DrawHand(rightHandState, jointPoints[JointType_HandRight]);
//                        }
//                    }
//                }
//            }

//            hr = m_pRenderTarget->EndDraw();

//            // Device lost, need to recreate the render target
//            // We'll dispose it now and retry drawing
//            if (D2DERR_RECREATE_TARGET == hr)
//            {
//                hr = S_OK;
//                DiscardDirect2DResources();
//            }
//        }

//        if (!m_nStartTime)
//        {
//            m_nStartTime = nTime;
//        }

//        double fps = 0.0;

//        LARGE_INTEGER qpcNow = {0};
//        if (m_fFreq)
//        {
//            if (QueryPerformanceCounter(&qpcNow))
//            {
//                if (m_nLastCounter)
//                {
//                    m_nFramesSinceUpdate++;
//                    fps = m_fFreq * m_nFramesSinceUpdate / double(qpcNow.QuadPart - m_nLastCounter);
//                }
//            }
//        }

//        WCHAR szStatusMessage[64];
//        StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L" FPS = %0.2f    Time = %I64d", fps, (nTime - m_nStartTime));

//        if (SetStatusMessage(szStatusMessage, 1000, false))
//        {
//            m_nLastCounter = qpcNow.QuadPart;
//            m_nFramesSinceUpdate = 0;
//        }
//    }

//}

bool KinectSensor::getCurrentBody(IBody **ppbodies){
    for(int a=0;aget_IsTracked(&btracked);
            if(SUCCEEDED(hr)&&btracked){
//                qDebug()<<"succeed!";
                return true;
            }
        }
    }
    return false;
}

void KinectSensor::getBodyJoint(){
    if(currentPBody){
        HRESULT hr;
        Joint joints[JointType_Count];
       hr=currentPBody->GetJoints(_countof(joints), joints);
       if(SUCCEEDED(hr)){
           leftHandJoint=joints[JointType_HandLeft];
           rightHandJoint=joints[JointType_HandRight];

           topRightJoint=joints[JointType_ShoulderRight];
           bottomRightJoint=joints[JointType_HipRight];
           topLeftJoint=joints[JointType_ShoulderLeft];
           headJoint=joints[JointType_Head];
           neckJoint=joints[JointType_Neck];
       }
    }
}

void KinectSensor::mapJointsToXYCoord(){
    m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
    if(m_pCoordinateMapper){
        m_pCoordinateMapper->MapCameraPointToColorSpace(leftHandJoint.Position,&leftHandPoint);
        m_pCoordinateMapper->MapCameraPointToColorSpace(rightHandJoint.Position,&rightHandPoint);
    }
}

QString KinectSensor::getLeftHandState(){

    if(leftHandState==HandState_Unknown){
        return "HandState_Unknown";
    }
    else if(leftHandState==HandState_NotTracked){
        return "HandState_NotTracked";
    }
    else if(leftHandState==HandState_Open){
        return "HandState_Open";
    }
    else if(leftHandState==HandState_Closed){
        return "HandState_Closed";
    }
    else if(leftHandState==HandState_Lasso){
        return "HandState_Lasso";
    }
    else {
        return "I don't know hell";
    }


}

QString KinectSensor::getRightHandState(){

    if(rightHandState==HandState_Unknown){
        return "HandState_Unknown";
    }
    else if(rightHandState==HandState_NotTracked){
        return "HandState_NotTracked";
    }
    else if(rightHandState==HandState_Open){
        return "HandState_Open";
    }
    else if(rightHandState==HandState_Closed){
        return "HandState_Closed";
    }
    else if(rightHandState==HandState_Lasso){
        return "HandState_Lasso";
    }
    else {
        return "I don't know hell";
    }
}

kinect 相关资源网站:kinect for windows

你可能感兴趣的:(windows,kinect,qt5,c++)