1

我正在使用 Windows Media Foundation 来处理我的网络摄像头。我已经能够成功地从网络摄像头检索数据样本并确定格式为 RGB24。现在我想将单个帧保存为位图。下面是我用来从网络摄像头读取样本的一小段代码。

IMFSample *pSample = NULL;

hr = pReader->ReadSample(
   MF_SOURCE_READER_ANY_STREAM,    // Stream index.
   0,                              // Flags.
   &streamIndex,                   // Receives the actual stream index. 
   &flags,                         // Receives status flags.
   &llTimeStamp,                   // Receives the time stamp.
   &pSample                        // Receives the sample or NULL.
 );

因此,一旦我用 IMFSample 填充了 pSample,我该如何将其保存为位图?

4

1 回答 1

1

下面是我用来从 IMFSample 保存位图的代码片段。我采取了很多捷径,我很确定我只能以这种方式逃脱,因为我的网络摄像头默认返回 RGB24 流和 640 x 480 像素缓冲区,这意味着没有条纹在 pData 中担心。

   hr = pReader->ReadSample(
    MF_SOURCE_READER_ANY_STREAM,    // Stream index.
    0,                              // Flags.
    &streamIndex,                   // Receives the actual stream index. 
    &flags,                         // Receives status flags.
    &llTimeStamp,                   // Receives the time stamp.
    &pSample                        // Receives the sample or NULL.
    );

wprintf(L"Stream %d (%I64d)\n", streamIndex, llTimeStamp);

HANDLE file;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER fileInfo;
DWORD write = 0;

file = CreateFile(L"sample.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  //Sets up the new bmp to be written to

fileHeader.bfType = 19778;                                                                    //Sets our type to BM or bmp
fileHeader.bfSize = sizeof(fileHeader.bfOffBits) + sizeof(RGBTRIPLE);                                                //Sets the size equal to the size of the header struct
fileHeader.bfReserved1 = 0;                                                                    //sets the reserves to 0
fileHeader.bfReserved2 = 0;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);                    //Sets offbits equal to the size of file and info header

fileInfo.biSize = sizeof(BITMAPINFOHEADER);
fileInfo.biWidth = 640;
fileInfo.biHeight = 480;
fileInfo.biPlanes = 1;
fileInfo.biBitCount = 24;
fileInfo.biCompression = BI_RGB;
fileInfo.biSizeImage = 640 * 480 * (24/8);
fileInfo.biXPelsPerMeter = 2400;
fileInfo.biYPelsPerMeter = 2400;
fileInfo.biClrImportant = 0;
fileInfo.biClrUsed = 0;

WriteFile(file,&fileHeader,sizeof(fileHeader),&write,NULL);
WriteFile(file,&fileInfo,sizeof(fileInfo),&write,NULL);

IMFMediaBuffer *mediaBuffer = NULL;
    BYTE *pData = NULL;

pSample->ConvertToContiguousBuffer(&mediaBuffer);

hr = mediaBuffer->Lock(&pData, NULL, NULL);

WriteFile(file, pData, fileInfo.biSizeImage, &write, NULL);

CloseHandle(file);

mediaBuffer->Unlock();

我在这里做了一些讨论。

于 2012-04-19T11:10:24.043 回答