-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKinectInfraredCam.cpp
More file actions
133 lines (113 loc) · 3.62 KB
/
KinectInfraredCam.cpp
File metadata and controls
133 lines (113 loc) · 3.62 KB
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
#include <Shlobj.h>
#include "util.h"
#include "KinectInfraredCam.h"
bool g_flipImage = false;
//KinectCam::KinectCam() :
// m_hNextVideoFrameEvent(INVALID_HANDLE_VALUE),
// m_pVideoStreamHandle(INVALID_HANDLE_VALUE),
// m_pNuiSensor(NULL)
//{}
//KinectCam::~KinectCam() {
// KinectCam::Nui_UnInit();
//}
INuiSensor* KinectInfraredCam::m_pNuiSensor = nullptr;
HANDLE KinectInfraredCam::m_hNextVideoFrameEvent = INVALID_HANDLE_VALUE;
HANDLE KinectInfraredCam::m_pVideoStreamHandle = INVALID_HANDLE_VALUE;
HRESULT KinectInfraredCam::CreateFirstConnected() {
if (m_pNuiSensor != nullptr) {
// Already initialized
return S_OK;
}
INuiSensor* pNuiSensor;
HRESULT hr;
int iSensorCount = 0;
hr = NuiGetSensorCount(&iSensorCount);
if (FAILED(hr))
{
return hr;
}
// Look at each Kinect sensor
for (int i = 0; i < iSensorCount; ++i)
{
// Create the sensor so we can check status, if we can't create it, move on to the next
hr = NuiCreateSensorByIndex(i, &pNuiSensor);
if (FAILED(hr))
{
continue;
}
// Get the status of the sensor, and if connected, then we can initialize it
hr = pNuiSensor->NuiStatus();
if (S_OK == hr)
{
m_pNuiSensor = pNuiSensor;
break;
}
// This sensor wasn't OK, so release it since we're not using it
pNuiSensor->Release();
}
if (nullptr != m_pNuiSensor)
{
// Initialize the Kinect and specify that we'll be using color
hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR);
if (SUCCEEDED(hr))
{
// Create an event that will be signaled when color data is available
m_hNextVideoFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_pNuiSensor->NuiCameraElevationSetAngle(20);
// Open a color image stream to receive color frames
hr = m_pNuiSensor->NuiImageStreamOpen(
NUI_IMAGE_TYPE_COLOR_INFRARED,
NUI_IMAGE_RESOLUTION_640x480,
0,
2,
m_hNextVideoFrameEvent,
&m_pVideoStreamHandle);
}
}
if (nullptr == m_pNuiSensor || FAILED(hr))
{
return E_FAIL;
}
return hr;
}
// TODO: fix bug where alt + f4 doesnt close camera
// TODO: fix bug where setting elevation to 0 somehow fixes and causes a bug? (bug: ir camera doesnt get shut off, but it prevents weird shit from happening)
void KinectInfraredCam::Nui_UnInit()
{
if (m_pNuiSensor)
{
m_pNuiSensor->NuiCameraElevationSetAngle(0);
m_pNuiSensor->NuiShutdown();
}
if (m_hNextVideoFrameEvent != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hNextVideoFrameEvent);
m_hNextVideoFrameEvent = INVALID_HANDLE_VALUE;
}
SafeRelease(m_pNuiSensor);
}
void KinectInfraredCam::Nui_GetCamFrame(BYTE* frameBuffer, int frameSize)
{
HRESULT hr;
NUI_IMAGE_FRAME imageFrame;
if (WAIT_OBJECT_0 != WaitForSingleObject(m_hNextVideoFrameEvent, 100))
return;
hr = m_pNuiSensor->NuiImageStreamGetNextFrame(
m_pVideoStreamHandle,
0,
&imageFrame);
if (FAILED(hr))
{
return;
}
INuiFrameTexture* pTexture = imageFrame.pFrameTexture;
NUI_LOCKED_RECT LockedRect;
pTexture->LockRect(0, &LockedRect, NULL, 0);
if (LockedRect.Pitch != 0)
{
BYTE* pBuffer = (BYTE*)LockedRect.pBits;
memcpy(frameBuffer, pBuffer, frameSize);
}
pTexture->UnlockRect(0);
m_pNuiSensor->NuiImageStreamReleaseFrame(m_pVideoStreamHandle, &imageFrame);
}