彩色圖像獲取和深度獲取的程序十分相似窖铡,前面的部分不多說了,只說不同的地方。
Update函數(shù)里面需要把Kinect獲取的彩色數(shù)據(jù)(Yuy2格式)轉(zhuǎn)換成BGRA格式吊圾,這是為了方便處理:
// 獲取彩色圖像格式
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
}
if (SUCCEEDED(hr))
{
// 如果是BGRA格式顶捷,就直接把pBuffer指向pColorFrame的數(shù)據(jù)頭挂绰,否則需要轉(zhuǎn)化成BGRA
if (imageFormat == ColorImageFormat_Bgra)
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));
}
else if (m_pColorRGBX)
{
pBuffer = m_pColorRGBX;
nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
}
之后同樣由ProcessColor函數(shù)來處理彩色數(shù)據(jù):
void CColorBasics::ProcessColor(INT64 nTime, RGBQUAD* pBuffer, int nWidth, int nHeight)
{
// 計(jì)算和顯示幀率、時(shí)間
if (m_hWnd)
{
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;
}
}
// Make sure we've received valid data 確定接收了有效數(shù)據(jù)
if (pBuffer && (nWidth == cColorWidth) && (nHeight == cColorHeight))
{
// Draw the data with Direct2D 繪制服赎、顯示彩色圖
m_pDrawColor->Draw(reinterpret_cast<BYTE*>(pBuffer), cColorWidth * cColorHeight * sizeof(RGBQUAD));
// 存儲(chǔ)彩色圖葵蒂,保存為bmp文件
if (m_bSaveScreenshot)
{
WCHAR szScreenshotPath[MAX_PATH];
// Retrieve the path to My Photos
GetScreenshotFileName(szScreenshotPath, _countof(szScreenshotPath));
// Write out the bitmap to disk
HRESULT hr = SaveBitmapToFile(reinterpret_cast<BYTE*>(pBuffer), nWidth, nHeight, sizeof(RGBQUAD) * 8, szScreenshotPath);
WCHAR szStatusMessage[64 + MAX_PATH];
if (SUCCEEDED(hr))
{
// Set the status bar to show where the screenshot was saved
StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L"Screenshot saved to %s", szScreenshotPath);
}
else
{
StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L"Failed to write screenshot to %s", szScreenshotPath);
}
SetStatusMessage(szStatusMessage, 5000, true);
// toggle off so we don't save a screenshot again next frame
m_bSaveScreenshot = false;
}
}
}