上一次挑战杯我们用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 = appQT += qml quick widgets coreSOURCES += main.cpp \kinect/kinectsensor.cppRESOURCES += 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)\incLIBS += $$(KINECTSDK20_DIR)\Lib\x86\kinect20.lib
#kinect_V_20 pluginCONFIG += 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 <windows.h>
#include <Shlobj.h>// Kinect Header files
#include <Kinect.h>#include <QObject>
#include <QString>
#include <QMouseEvent>
#include <QApplication>
#include <QWidget>
#include <QPoint>
#include <QDebug>
#include <QWindow>
#include <QCursor>
#include <QBitmap>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);}/// <summary>/// Main processing function/// </summary>Q_INVOKABLE void updatebody();/// <summary>/// Initializes the default Kinect sensor/// </summary>/// <returns>S_OK on success, otherwise failure code</returns>Q_INVOKABLE HRESULT                 InitializeDefaultSensor();/// <summary>/// Handle new body data/// <param name="nTime">timestamp of frame</param>/// <param name="nBodyCount">body data count</param>/// <param name="ppBodies">body data in frame</param>/// </summary>
//    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 KinectIKinectSensor*          m_pKinectSensor;ICoordinateMapper*      m_pCoordinateMapper;IBody *currentPBody;//hand stateHandState 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 positionColorSpacePoint leftHandPoint;ColorSpacePoint rightHandPoint;// Body readerIBodyFrameReader*       m_pBodyFrameReader;QCursor *cursor;int winx;int winy;};
#endif // KINECTSENSOR_H

kinectsensor.cpp

#include "kinectsensor.h"
#include <QDebug>// Safe release for interfaces
template<class Interface>
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 positionIBody* 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 readerIBodyFrameSource* 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;a<BODY_COUNT;a++){currentPBody=ppbodies[a];if(currentPBody){HRESULT hr;BOOLEAN btracked;hr=currentPBody->get_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下qt5 kinect 2.0开发与环境配置相关推荐

  1. Windows下使用VS2008+CUDA3.0开发的详细配置 (Setup CUDA 3.0 on VS2008 in Windows)

    操作系统(OS):                      Windows 7 集成开发环境(IDE):               Microsoft Visual Studio 2008 SP1 ...

  2. Windows下搭建Eclipse+Android4.0开发环境

    官方搭建步骤: http://developer.android.com/index.html 搭建好开发环境之前须要下载以下几个文件包: 一.安装Java执行环境JRE(没这个Eclipse执行不起 ...

  3. Windows下学习Objective-C 2.0

    为什么要在windows下学习objective c 学习一门移动端的语言,为后面的工作做准备 穷,目前买不起Mac.只能在Windows下曲线学习. 如何在Windows下搭建Objective-c ...

  4. Android之Windows下搭建React Native Android开发环境(差不多搞了一天)

    Android之Windows下搭建React Native Android开发环境               穷逼买不起mac,但是他们都说React Native Android只能在mac下面 ...

  5. windows下搭建Apache+Mysql+PHP开发环境

    原文:windows下搭建Apache+Mysql+PHP开发环境 要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); Apache2.2; MySQL Server 5 ...

  6. python3.7安装步骤-Windows下Python 3.7.0的安装步骤,Python370

    Windows下Python 3.7.0的安装步骤 由于Python版本太新的话,能会出现不稳定的情况,所以在这里,作者建议用版本不太新的Python.在这里,我用的是Python3.7.0版本(其他 ...

  7. linux下swift编程教程视频教程,Ubuntu 14.04下搭建 Swift 3.0 开发环境教学视频+PPT

    分享Ubuntu 14.04下搭建 Swift 3.0 开发环境教学视频+PPT. Linux版本:Ubuntu: 14.04 LTS Swift: 3.0 到https://swift.org/do ...

  8. QT环境配置:QT5.8.0与VS2013环境配置

    QT环境配置:QT5.8.0与VS2013环境配置 1.下载VS2013,QT5.8.0,qt-vs-tools-msvc2013-2.1.1. 1)VS2013版本为:Visual Studio U ...

  9. 在windows上模拟linux环境,Windows下使用NCL(Cygwin模拟Linux环境)

    参考自:http://bbs.lasg.ac.cn/bbs/thread-37043-1-1.html 1.下载 所需文件均可在此下载:http://yunpan.cn/cQsvAEe3Axs2Z   ...

最新文章

  1. Mysql5.7使用注意事项随笔
  2. java 一般方法_一般覆盖Java中的方法
  3. 如何理解拜占庭将军问题?
  4. VMware:警惕 vSphere Web Client中的新漏洞
  5. 视觉定位VBL 视觉里程计VO 视觉SLAM 区别与联系
  6. mysql连接不用数据库名称_C++连接MySQL数据库
  7. 关于DoIP 协议的理解
  8. 棋牌游戏算法——麻将系列总结
  9. 书评精益创业-新创企业的成长思维 (上)
  10. Ubuntu 16.04下使用Wine安装Windows版的微信(不太完美)
  11. 2021年11月视频行业用户洞察
  12. html5 拉弓,瞄准 拉弓 射出梦想
  13. ios开发中各种版本、设备的区分的代码
  14. layui数据表格的字体颜色
  15. self.跟self-什么区别?
  16. 58同城post登陆参数分析,典型的eval加密js案例
  17. Kubernets集群管理-升级 kubernetes 集群版本到v1.21.14
  18. wifi的sta + ap模式
  19. 神经网络学习小记录45——Keras常用学习率下降方式汇总
  20. h5微信f分享链接给对方获取对方手机号_微信生日贺卡链接制作

热门文章

  1. Windoes 10 笔记本上安装telnet方法
  2. Mysql搭建主从服务器
  3. 如何培养自己奇特的创意设计思维?
  4. 移动平均滤波_Kalman滤波理论与MATLAB实现引言
  5. php中mb substr,php中中文截取函数mb_substr()详细
  6. abaqus画一个球 python_简单几步,100行代码用Python画一个蝙蝠侠的logo
  7. 始化mysql系统库_安装和初始化mysql-8.0.11-winx64
  8. Java基础:成员变量的继承与覆盖
  9. 下列哪个是java的标识符_下列哪个不属于Java的正确标识符?A、publicB、sizeofC、cLAssD、_new...
  10. 【模板】单源最短路径(弱化版)