将采集卡采到的每帧数据保存为AVI视频的问题

tgwz973011 2011-08-15 08:17:15
每一帧图像大小是1024*1024,数据位是8位(灰度图像)。现在我将每一帧的数据写入到AVI文件中,但是在播放时候却不不是灰度图像,而是彩色图像。是怎么回事?谢谢,跪求。。。我将源码贴下
PAVIFILE m_pAviFile;
if(FAILED(AVIFileOpen(&m_pAviFile, m_szFileName, OF_CREATE|OF_WRITE, NULL)))
{
SetErrorMessage("Unable to Create the Movie File");
return E_FAIL;
}
AVISTREAMINFO m_AviStreamInfo
ZeroMemory(&m_AviStreamInfo,sizeof(AVISTREAMINFO));
m_AviStreamInfo.fccType = streamtypeVIDEO;
m_AviStreamInfo.fccHandler = m_dwFCCHandler;
m_AviStreamInfo.dwScale = 1;
m_AviStreamInfo.dwRate = m_dwFrameRate; // Frames Per Second;
m_AviStreamInfo.dwQuality = -1; // Default Quality
m_AviStreamInfo.dwSuggestedBufferSize = 1024*1024;
SetRect(&m_AviStreamInfo.rcFrame, 0, 0, 1024, 1024);
_tcscpy(m_AviStreamInfo.szName, _T("Video Stream"));

PAVISTREAM m_pAviStream;
if(FAILED(AVIFileCreateStream(m_pAviFile,&m_pAviStream,&m_AviStreamInfo)))
{
SetErrorMessage("Unable to Create Video Stream in the Movie File");
return E_FAIL;
}
AVICOMPRESSOPTIONS m_AviCompressOptions;
ZeroMemory(&m_AviCompressOptions,sizeof(AVICOMPRESSOPTIONS));
m_AviCompressOptions.fccType=streamtypeVIDEO;
m_AviCompressOptions.fccHandler=mmioFOURCC('M','S','V','C');
m_AviCompressOptions.dwFlags=AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;
m_AviCompressOptions.dwKeyFrameEvery=1;

PAVISTREAM m_pAviCompressedStream;
if(FAILED(AVIMakeCompressedStream(&m_pAviCompressedStream,m_pAviStream,&m_AviCompressOptions,NULL)))
{
// One reason this error might occur is if you are using a Codec that is not
// available on your system. Check the mmioFOURCC() code you are using and make
// sure you have that codec installed properly on your machine.
SetErrorMessage("Unable to Create Compressed Stream: Check your CODEC options");
return E_FAIL;
}

BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biWidth = 1024;
bmpInfo.bmiHeader.biHeight = 1024;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = 8;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*bmpInfo.bmiHeader.biBitCount/8;

if(FAILED(AVIStreamSetFormat(m_pAviCompressedStream,0,(LPVOID)&bmpInfo, bmpInfo.bmiHeader.biSize))) {
// One reason this error might occur is if your bitmap does not meet the Codec requirements.
// For example,
// your bitmap is 32bpp while the Codec supports only 16 or 24 bpp; Or
// your bitmap is 274 * 258 size, while the Codec supports only sizes that are powers of 2; etc...
// Possible solution to avoid this is: make your bitmap suit the codec requirements,
// or Choose a codec that is suitable for your bitmap.
SetErrorMessage("Unable to Set Video Stream Format");
return E_FAIL;
}

return S_OK; // Everything went Fine
}

DWORD dwSize=nWidth*nHeight*nBitsPerPixel/8;

if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,pBits(指向数据位的指针),dwSize,0,NULL,NULL)))
{
SetErrorMessage(_T("Unable to Write Video Stream to the output Movie File"));

ReleaseMemory();

return E_FAIL;
}

把每帧图像保存成AVI视频没有问题,但是播放的时候有失真,不是黑白视频。例如,同一个保存的视频第一次播放是蓝色,第二次或许就成了红色。是不是我保存的时候没有将颜色表保存进去?但是我如果要在蓝色字体前面加上如下代码
for (i=0;i<256;i++)
{
bmpInfo.bmiHeader.bmiColors[i].rgbBlue = i ;
bmpInfo.bmiHeader.bmiColors[i].rgbGreen = i ;
bmpInfo.bmiHeader.bmiColors[i].rgbRed = i ;
bmpInfo.bmiHeader.bmiColors[i].rgbReserved = 0 ;
}的话。那么程序运行到蓝色那里就自动退出了。软件就关闭了。是什么原因呢?请各位帮忙改一下啊,谢谢啦。
...全文
274 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyhhb 2011-08-16
  • 打赏
  • 举报
回复
我也觉得是位图的信息头问题,你检查一下?
lifesider 2011-08-15
  • 打赏
  • 举报
回复
bmpInfo.bmiHeader.biBitCount = 8;
这里应该改成24吧
bmpInfo.bmiHeader.biBitCount = 24;

根据你上面格式写成BI_RGB,因而每个像素是24位

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧