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(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格式说明很好理解,就这样吧(逃

你可能感兴趣的:(Kinect学习(3)——图像存储)