Kinect學習(3)——圖像存儲

分析Kinect圖像存儲前首先要了解BMP格式,這里就不再重復介紹了。可參考:BMP文件格式BMP文件格式詳解

不管是DepthBasics-D2D還是ColorBasics-D2D,對圖像的存儲都使用了相同的方法。對于DepthBasics-D2D,在ProcessDepth內是這樣的:

        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*>(m_pDepthRGBX), 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;
        }

而對于ProcessColor,唯一不同的是把SaveBitmapToFile函數的第一個參數 m_pDepthRGBX改成pBuffer。由程序前文可知,m_pDepthRGBXpBuffer分別指向一個512x424大小和1920x1080大小的RGBQUAD類型存儲空間,RGBQUAD表示一個四通道像素點,每個通道占1字節(Byte),其定義如下:

typedef struct tagRGBQUAD {
        BYTE    rgbBlue;
        BYTE    rgbGreen;
        BYTE    rgbRed;
        BYTE    rgbReserved;
} RGBQUAD;

也就是說,m_pDepthRGBXpBuffer分別指向了存儲一幀深度圖和彩色圖的存儲空間。顯然,對于SaveBitmapToFile來說這沒有什么區別。

SaveBitmapToFile函數如下:

HRESULT CDepthBasics::SaveBitmapToFile(BYTE* pBitmapBits, LONG lWidth, LONG lHeight, WORD wBitsPerPixel, LPCWSTR lpszFilePath)
{
    // 字節計數,對于深度數據是512x424x(32/8),對于彩色數據是1920x1080x(32/8)
    DWORD dwByteCount = lWidth * lHeight * (wBitsPerPixel / 8);

    BITMAPINFOHEADER bmpInfoHeader = {0}; // 頭信息

    bmpInfoHeader.biSize        = sizeof(BITMAPINFOHEADER);  // Size of the header 頭信息大小
    bmpInfoHeader.biBitCount    = wBitsPerPixel;             // Bit count 
    bmpInfoHeader.biCompression = BI_RGB;                    // Standard RGB, no compression 標準RGB,不壓縮
    bmpInfoHeader.biWidth       = lWidth;                    // Width in pixels
    bmpInfoHeader.biHeight      = -lHeight;                  // Height in pixels, negative indicates it's stored right-side-up
    bmpInfoHeader.biPlanes      = 1;                         // Default
    bmpInfoHeader.biSizeImage   = dwByteCount;               // Image size in bytes

    BITMAPFILEHEADER bfh = {0};

    bfh.bfType    = 0x4D42;                                           // 'M''B', indicates bitmap
    bfh.bfOffBits = bmpInfoHeader.biSize + sizeof(BITMAPFILEHEADER);  // Offset to the start of pixel data
    bfh.bfSize    = bfh.bfOffBits + bmpInfoHeader.biSizeImage;        // Size of image + headers

    // Create the file on disk to write to 創建要寫入數據的文件
    HANDLE hFile = CreateFileW(lpszFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    // Return if error opening file
    if (NULL == hFile) 
    {
        return E_ACCESSDENIED;
    }

    DWORD dwBytesWritten = 0;
    
    // Write the bitmap file header 寫位圖文件頭
    if (!WriteFile(hFile, &bfh, sizeof(bfh), &dwBytesWritten, NULL))
    {
        CloseHandle(hFile);
        return E_FAIL;
    }
    
    // Write the bitmap info header 寫位圖信息頭
    if (!WriteFile(hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwBytesWritten, NULL))
    {
        CloseHandle(hFile);
        return E_FAIL;
    }
    
    // Write the RGB Data 寫入RGB數據
    if (!WriteFile(hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwBytesWritten, NULL))
    {
        CloseHandle(hFile);
        return E_FAIL;
    }    

    // Close the file
    CloseHandle(hFile);
    return S_OK;
}

發現沒什么要特別說明的,對照前面的BMP格式說明很好理解,就這樣吧(逃

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容