forked from jamesashley1/K4Wv2UnityBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectv2Wrapper.cpp
144 lines (109 loc) · 2.13 KB
/
Kinectv2Wrapper.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
#include "Kinectv2Wrapper.h"
IKinectSensor* context;
IColorFrameReader* m_pColorFrameReader;
byte *pBuffer = NULL;
UINT nBufferSize = 0;
RGBQUAD* m_pColorRGBX;
static const int cColorWidth = 1920;
static const int cColorHeight = 1080;
byte* Get_Color()
{
return pBuffer;
}
bool Kinect_Init()
{
HRESULT hr;
hr = GetDefaultKinectSensor(&context);
if (FAILED(hr))
{
return false;
}
if (context)
{
// Initialize the Kinect and get the color reader
IColorFrameSource* pColorFrameSource = NULL;
pBuffer = new byte[cColorWidth * cColorHeight * 4];
hr = context->Open();
if (SUCCEEDED(hr))
{
hr = context->get_ColorFrameSource(&pColorFrameSource);
}
else{
return false;
}
if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);
}
if (pColorFrameSource)
{
pColorFrameSource->Release();
pColorFrameSource = NULL;
}
}
if (!context || FAILED(hr))
{
return false;
}
return true;
}
bool Kinect_Deinit(){
if (m_pColorFrameReader)
{
m_pColorFrameReader->Release();
m_pColorFrameReader = NULL;
}
if (context)
{
context->Close();
}
if (context)
{
context->Release();
context = NULL;
}
return true;
}
int Poll_Color(){
ColorImageFormat imageFormat = ColorImageFormat_Bgra;
if (!m_pColorFrameReader)
{
return 0;
}
IColorFrame* pColorFrame = NULL;
HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
if (SUCCEEDED(hr))
{
INT64 nTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
}
if (SUCCEEDED(hr))
{
nBufferSize = cColorWidth * cColorHeight * 4;
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, pBuffer, ColorImageFormat_Bgra);
}
}
if (pColorFrame)
{
pColorFrame->Release();
pColorFrame = NULL;
}
return (int)nBufferSize;
}