美文网首页
Kinect学习(2)——ColorBasics-D2D

Kinect学习(2)——ColorBasics-D2D

作者: 暗夜望月 | 来源:发表于2017-03-02 19:20 被阅读0次

    彩色图像获取和深度获取的程序十分相似,前面的部分不多说了,只说不同的地方。

    Update函数里面需要把Kinect获取的彩色数据(Yuy2格式)转换成BGRA格式,这是为了方便处理:

            // 获取彩色图像格式
            if (SUCCEEDED(hr))
            {
                hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
            }
    
            if (SUCCEEDED(hr))
            {
                // 如果是BGRA格式,就直接把pBuffer指向pColorFrame的数据头,否则需要转化成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函数来处理彩色数据:

    void CColorBasics::ProcessColor(INT64 nTime, RGBQUAD* pBuffer, int nWidth, int nHeight) 
    {
        // 计算和显示帧率、时间
        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 确定接收了有效数据
        if (pBuffer && (nWidth == cColorWidth) && (nHeight == cColorHeight))
        {
            // Draw the data with Direct2D 绘制、显示彩色图
            m_pDrawColor->Draw(reinterpret_cast<BYTE*>(pBuffer), cColorWidth * cColorHeight * sizeof(RGBQUAD));
    
            // 存储彩色图,保存为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;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Kinect学习(2)——ColorBasics-D2D

          本文链接:https://www.haomeiwen.com/subject/fhlkgttx.html