上一次挑戰杯咱們用kinect1.8作了一個體感3d試衣系統,那時就想用qt來開發了,因爲那時候不少東西都不懂,怎麼也不知道怎樣去配置環境,只能在vs2013中開發,結合qt開發界面。直到出了kinect2.0後才配置成功徹底用qt 來開發Kinect!並且完美運行。注意一點就是使用的qt必須是
qt forwindows msvc的才能正常編譯。
kinect 2.0開發系統要求:win8.1以上
kinect 1.8開發系統要求:win7以上c++
工程文件的配置
kinectBasicBuildingExplorer.proshell
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功能代碼,本代碼功能是實現了右手看成鼠標來操控程序,當手握緊至關於鼠標按下左鍵,當手控制鼠標懸停在某個按鈕的時候,至關於鼠標的點擊事件。windows
kinectsensor.hapi
#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 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.cppapp
#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 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;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網站