Kinect for windows version 2的c++例子代码(下载量很少哦,可见影响不大),例子使用了windows API创建的窗口,依旧挺复杂的~ 之前在facebook上看到台湾的孩子用OpenCV简化的版本代码只有一百来行(这个你自己去找或者写出来吧~他们没提供给我)。下半年开始研究Kinect2,现在仅仅收集Kinect2新闻和资料~
This project is an example of how to listen for K4W v2 API events using C++. This project was built with VS.Net 2013 and K4Wv2 API alpha bits issued 11/2013.
This project is a simple example of how to listen for Kinect v2 API events using C++. This project was meant to be a tutorial of sorts to show how to write modern C++ code to listen for Kinect for windows v2 Frame arrived events.
This sample was built using the alpha Kinect v2 API library.
The sample is a win32 based project which utilizes a menu command to “Start” the Kinect v2 Sensor. The sensor is started and the infrared events are received and written out the output window. You can use Sysinternal DebugView to see the events as they arrive in real time.
This sample shows how to use a Message Loop based on the “gamers” loop design and the MsgWaitForMultipleObjects non-blocking parameters to listen for normal window events such as pain events, mouse clicks, menu-commands, as well as the Kinect v2 sensor events.
All code is written with the default Visual Studio 2013 C++ windows 32 project template, and C++11 modern libraries and conventions.
Project Information URL: https://k4wv2eventsample.codeplex.com/
Project Download URL: https://k4wv2eventsample.codeplex.com/releases/
Project Source URL: https://k4wv2eventsample.codeplex.com/SourceControl/latest
我将两个最重要的文件代码打出来:
WorkingWithKinectEvents.h
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
#pragma once #include "resource.h" #include "common.h" #include <Kinect.h> #include <memory> #include <algorithm> using namespace std; struct KinectEvents { public: std::unique_ptr<IKinectSensor> pKinect; std::unique_ptr<IInfraredFrameSource> pSource; std::unique_ptr<UINT16*> pInfraredData; std::unique_ptr<IInfraredFrameReader> pReader; WAITABLE_HANDLE hIREvent; UINT mLengthInPixels; bool mIsStarted; std::unique_ptr<IMultiSourceFrameReader> pMultiSourceFrameReader; WAITABLE_HANDLE hMSEvent; KinectEvents() : pKinect(nullptr), pSource(nullptr), pInfraredData(nullptr), pReader(nullptr), hIREvent(NULL), mLengthInPixels(0), mIsStarted(false), pMultiSourceFrameReader(nullptr), hMSEvent(NULL) { TRACE(L"KinectEvents Constructed"); //Initialize Kinect IKinectSensor * pSensor = pKinect.get(); HRESULT hr = GetDefaultKinectSensor(&pSensor); if (SUCCEEDED(hr)) { TRACE(L"Default Kinect Retreived - HR: %d", hr); //we have a kinect sensor pKinect.reset(pSensor); KinectStatus status; hr = pKinect->get_Status(&status); TRACE(L"Kinect is valid device - status: %dn", status); } //if (SUCCEEDED(hr)) //{ // IMultiSourceFrameReader * preader = nullptr; // hr = pKinect->OpenMultiSourceFrameReader( // FrameSourceTypes::FrameSourceTypes_Infrared , // &preader); // if (SUCCEEDED(hr)) { // TRACE(L"Multi source frame reader retrieved"); // pMultiSourceFrameReader.reset(preader); // } //} // } ~KinectEvents() { TRACE(L"KinectEvents Destructed"); if (hIREvent) { TRACE(L"Handle %d - being released...", hIREvent); HRESULT hr = pReader->UnsubscribeFrameArrived(hIREvent); if (SUCCEEDED(hr)) TRACE(L"Handle to InfraredFrame Event Successfully Released"); else TRACE(L"Handle to InfraredFrame Event Not Released"); } hIREvent = NULL; TRACE(L"Handle to InfraredFrame set to NULL"); if (hMSEvent) { TRACE(L"Handle %d - being released...", hMSEvent); HRESULT hr = pMultiSourceFrameReader->UnsubscribeMultiSourceFrameArrived(hMSEvent); if (SUCCEEDED(hr)) TRACE(L"Handle to MultiSource Frame Event Successfully Released"); else TRACE(L"Handle to MultiSource Frame Event Not Released"); } hMSEvent = NULL; TRACE(L"Handle to MultiSource Frame Event set to NULL"); pReader.release(); pReader = nullptr; TRACE(L"InfraredFrame Reader Released"); pInfraredData.release(); pInfraredData = nullptr; TRACE(L"InfraredFrame Data buffer Released"); pSource.release(); pSource = nullptr; TRACE(L"InfraredFrameSource Released"); pMultiSourceFrameReader.release(); pMultiSourceFrameReader = nullptr; TRACE(L"Multi Source Frame Reader Released"); if (pKinect) { HRESULT hr = pKinect->Close(); TRACE(L"Closing Kinect - HR: %d", hr); HR(hr); TRACE(L"HR : %d", hr); pKinect.release(); pKinect = nullptr; TRACE(L"Kinect resources released."); } } void Start() { ASSERT(pKinect); if (!mIsStarted) { ICoordinateMapper * m_pCoordinateMapper = nullptr; HRESULT hr = pKinect->get_CoordinateMapper(&m_pCoordinateMapper); TRACE(L"Retrieved CoordinateMapper- HR: %d", hr); IBodyFrameSource* pBodyFrameSource = nullptr; if (SUCCEEDED(hr)) { hr = pKinect->get_BodyFrameSource(&pBodyFrameSource); TRACE(L"Retrieved Body Frame Source - HR: %d", hr); } IBodyFrameReader * pBodyFrameReader = nullptr; if (SUCCEEDED(hr)) { hr = pBodyFrameSource->OpenReader(&pBodyFrameReader); TRACE(L"Opened Kinect Reader - HR: %d", hr); } IInfraredFrameSource * pIRSource = nullptr; if (SUCCEEDED(hr)) { hr = pKinect->get_InfraredFrameSource(&pIRSource); TRACE(L"Retrieved IR Frame Source - HR: %d", hr); } if (SUCCEEDED(hr)){ TRACE(L"Kinect has not started yet... Opening"); hr = pKinect->Open(); TRACE(L"Opened Kinect - HR: %d", hr); } //if (SUCCEEDED(hr)){ // hr = pMultiSourceFrameReader->SubscribeMultiSourceFrameArrived(&hMSEvent); // TRACE(L"Subscribed to MultisourceFrameEvent - HR: %d - EventID: %d", hr, hMSEvent); //} //IMultiSourceFrame * pMsFrame = nullptr; //if (SUCCEEDED(hr)) //{ // hr = pMultiSourceFrameReader->AcquireLatestFrame(&pMsFrame); // TRACE(L"Retreived latest IR FRAME - HR: %d", hr); //} //IInfraredFrameReference * pIrRef = nullptr; //if (SUCCEEDED(hr)) //{ // hr = pMsFrame->get_InfraredFrameReference(&pIrRef); // TRACE(L"Retreived IR FRAME reference - HR: %d", hr); //} //pMsFrame->Release(); //IInfraredFrame * pIrFrame = nullptr; //if (SUCCEEDED(hr)) //{ // hr = pIrRef->AcquireFrame(&pIrFrame); // TRACE(L"Retreived IR FRAME - HR: %d", hr); //} //pIrRef->Release(); ////Allocate a buffer IFrameDescription * pIRFrameDesc = nullptr; //if (SUCCEEDED(hr)) //{ // hr = pIrFrame->get_FrameDescription(&pIRFrameDesc); // TRACE(L"Retreived IR FRAME Description - HR: %d", hr); //} //IInfraredFrameSource * pIRSource = nullptr; if (SUCCEEDED(hr)){ // hr = pIrFrame->get_InfraredFrameSource(&pIRSource); pSource.reset(pIRSource); hr = pIRSource->get_FrameDescription(&pIRFrameDesc); TRACE(L"Retreived IR FRAME Source - HR: %d", hr); } //pIrFrame->Release(); UINT lengthInPixels = 0; if (SUCCEEDED(hr)){ // pSource.reset(pIRSource); hr = pIRFrameDesc->get_LengthInPixels(&lengthInPixels); TRACE(L"Retreived IR FRAME Description Pixel Length", hr); } auto ret = pIRFrameDesc->Release(); TRACE(L"IR FrameDescription Released %d", ret); IInfraredFrameReader * pIRReader = nullptr; if (SUCCEEDED(hr)){ TRACE(L"Length In Pixels: %d", lengthInPixels); mLengthInPixels = lengthInPixels; pInfraredData = make_unique<UINT16*>(new UINT16[lengthInPixels]); hr = pSource->OpenReader(&pIRReader); TRACE(L"Opened IR Reader"); } if (SUCCEEDED(hr)){ pReader.reset(pIRReader); hr = pReader->SubscribeFrameArrived(&hIREvent); TRACE(L"Reader Accessed Successfully"); TRACE(L"Subscribe to Frame Arrived Event call - HR: %d", hr); } //pIRReader->Release(); if (SUCCEEDED(hr)){ TRACE(L"Successfully Subscribed to Frame Arrived EventID: %d", (UINT)hIREvent); } //pReader->Release(); //pSource->Release(); //pMultiSourceFrameReader->Release(); mIsStarted = true; } } void InfraredFrameArrived(IInfraredFrameArrivedEventArgs* pArgs) { TRACE(L"IR Framed event arrived"); ASSERT(pArgs); IInfraredFrameReference * pFrameRef = nullptr; HRESULT hr = pArgs->get_FrameReference(&pFrameRef); if (SUCCEEDED(hr)){ //we have a frame reference //Now Acquire the frame TRACE(L"We have a frame reference - HR: %d", hr); bool processFrameValid = false; IInfraredFrame* pFrame = nullptr; TIMESPAN relativeTime = 0; hr = pFrameRef->AcquireFrame(&pFrame); if (SUCCEEDED(hr)){ TRACE(L"We have acquired a frame - HR : %d", hr); //Now copy the frames data to the buffer hr = pFrame->CopyFrameDataToArray(mLengthInPixels, *pInfraredData); if (SUCCEEDED(hr)){ TRACE(L"We have successfully copied ir frame data to buffer"); processFrameValid = true; hr = pFrame->get_RelativeTime(&relativeTime); TRACE(L"Relative Time: - HR: %dt Time: %d", hr, relativeTime); } auto ret = pFrame->Release(); TRACE(L"IR Frame released: %d", ret); } auto ret = pFrameRef->Release(); TRACE(L"IR Frame Reference released: %d", ret); if (processFrameValid) ProcessFrame(mLengthInPixels, *pInfraredData, relativeTime); } } void ProcessFrame(UINT length, UINT16 * pBuffer, TIMESPAN relativeTime) { TRACE(L"Process Frame Called.nBufferLength: %dntTimeSpan: %d", length, relativeTime); } }; void StartKinect(); |
WorkingWithKinectEvents.cpp
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
// WorkingWithKinectEvents.cpp : Defines the entry point for the application. // #include "stdafx.h" #include "WorkingWithKinectEvents.h" #define MAX_LOADSTRING 100 // Global Variables: HINSTANCE hInst; // current instance TCHAR szTitle[MAX_LOADSTRING]; // The title bar text TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name KinectEvents ke; // Forward declarations of functions included in this code module: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_WORKINGWITHKINECTEVENTS, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WORKINGWITHKINECTEVENTS)); //// Main message loop: //while (GetMessage(&msg, NULL, 0, 0)) //{ // if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) // { // TranslateMessage(&msg); // DispatchMessage(&msg); // } //} while (true) { while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { DispatchMessage(&msg); } if (ke.hIREvent) { //TRACE(L"Kinect Event ID: %d" ,(int)ke.hIREvent); //now check for IR Events HANDLE handles[] = { reinterpret_cast<HANDLE>(ke.hIREvent) }; // , reinterpret_cast<HANDLE>(ke.hMSEvent) }; switch (MsgWaitForMultipleObjects(_countof(handles), handles, false, 1000, QS_ALLINPUT)) { case WAIT_OBJECT_0: { IInfraredFrameArrivedEventArgs* pArgs = nullptr; TRACE(L"IR Frame Event Signaled."); if (ke.pReader) { HRESULT hr = ke.pReader->GetFrameArrivedEventData(ke.hIREvent, &pArgs); TRACE(L"Retreive Frame Arrive Event Data -HR: %d", hr); if (SUCCEEDED(hr)) { TRACE(L"Retreived Frame Arrived Event Data"); ke.InfraredFrameArrived(pArgs); pArgs->Release(); TRACE(L"Frame Arrived Event Data Released"); } } } break; } } if (WM_QUIT == msg.message) { break; } } return (int) msg.wParam; } // // FUNCTION: MyRegisterClass() // // PURPOSE: Registers the window class. // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WORKINGWITHKINECTEVENTS)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WORKINGWITHKINECTEVENTS); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex); } // // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } void StartKinect() { ke.Start(); } // // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case ID_FILE_STARTKINECT: StartKinect(); break; case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // Message handler for about box. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } |