编程实现AVI转BMP的问题!

wengzhong 2006-06-03 01:59:58
已经实现了
使用VFW.H里的
AVIFileOpen
AVIFileInfo
...
这些函数把非压缩AVI专成一张一张的BMP.

但现在的问题时,抓出来的BMP图片失真比较严重,最右边的图象跑到了最左边
,并且上边有很多的条纹.这个是什么原因呢?

程序如下:

int main()
{
wchar_t aviFilename[256],bmpFilePath[256];
wcin >> aviFilename >> bmpFilePath;

wcout << aviFilename << endl;
AVIFileInit();
PAVIFILE pAvi;
int result = AVIFileOpen( &pAvi , aviFilename , OF_READ , NULL );

if ( result != AVIERR_OK )
{
wcout << " can't open file" << endl;
if ( pAvi != NULL )
AVIFileRelease( pAvi );
return 0;
}
AVIFILEINFO avi_info;
AVIFileInfo( pAvi , &avi_info ,sizeof( AVIFILEINFO )) ;
PAVISTREAM pStream;
result = AVIFileGetStream( pAvi , &pStream , streamtypeVIDEO , 0L );

if ( result != AVIERR_OK )
{
wcout << "can't get stream " << endl;
if ( pStream != NULL )
{
AVIStreamRelease( pStream );
}
AVIFileExit();
return 0;
}
int iNumFrames;
int iFirstFrame;
iFirstFrame = AVIStreamStart( pStream );
if ( iFirstFrame == -1 )
{
wcout << " the first frame is -1" << endl;
if ( pStream != NULL )
{
AVIStreamRelease( pStream );
}
AVIFileExit();
return 0;
}

iNumFrames = AVIStreamLength( pStream );
if ( iFirstFrame == -1 )
{
wcout << " the frame length is -1 " << endl;
if ( pStream != NULL )
{
AVIStreamRelease( pStream );
}
AVIFileExit();
return 0;
}

BITMAPINFOHEADER bmpheader;
ZeroMemory( &bmpheader , sizeof( BITMAPINFOHEADER ) );

bmpheader.biBitCount = 24;
bmpheader.biClrImportant = 0;
bmpheader.biClrUsed = 0;
bmpheader.biCompression = BI_RGB;
bmpheader.biPlanes = 1;
bmpheader.biSize = 40;
bmpheader.biXPelsPerMeter = 0;
bmpheader.biYPelsPerMeter = 0;
bmpheader.biSizeImage = ((( bmpheader.biWidth * 3 ) + 3 ) &0xFFFC ) * bmpheader.biHeight ;

PGETFRAME pFrame;
pFrame = AVIStreamGetFrameOpen( pStream , NULL );

AVISTREAMINFO streaminfo;
AVIStreamInfo( pStream , &streaminfo , sizeof( AVISTREAMINFO ));

BITMAPINFOHEADER bmpheader1;
long lsize = sizeof( BITMAPINFOHEADER );
int index = 0 ;
for ( int i = iFirstFrame ; i < iNumFrames ; ++i )
{
index = i - iFirstFrame;
if ( index > 100 )
{
break;
}
BYTE *pDIB = (BYTE*) AVIStreamGetFrame( pFrame , index );
AVIStreamReadFormat( pStream , index , &bmpheader1 , &lsize );
BITMAPFILEHEADER stFileHdr;

BYTE* bits = new BYTE[bmpheader1.biSizeImage];
AVIStreamRead(pStream,index,1,bits,bmpheader1.biSizeImage,NULL,NULL);

bmpheader1.biClrUsed = 0;
stFileHdr.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
stFileHdr.bfSize = sizeof( BITMAPFILEHEADER );
stFileHdr.bfType = 0x4d42;

ostringstream filepath;
filepath << bmpFilePath << "_" << i << ".bmp" ;
ofstream outfile( filepath.str().c_str() );
outfile.write( (const char *)&stFileHdr , sizeof( BITMAPFILEHEADER ) );
outfile.write( (const char *)&bmpheader1 , sizeof( BITMAPINFOHEADER ) );
outfile.write( (const char *)bits , bmpheader1.biSizeImage );
outfile.close();
delete bits;
}
AVIStreamGetFrameClose( pFrame );
if ( pStream != NULL )
AVIStreamRelease( pStream );
AVIFileExit();

return 0;

}
...全文
345 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
duangexin521 2006-10-17
  • 打赏
  • 举报
回复
我现在想实现有声音的avi文件播放,视频流播放已经实现,但是音频流不知道怎么来实现?怎么来使得其同步呢??我是用vfw来做的,有谁知道的请指点一下啊~~谢谢拉
duan.gexin@byd.com.cn
justin108 2006-06-05
  • 打赏
  • 举报
回复
我有一段.net实现的。
wengzhong 2006-06-04
  • 打赏
  • 举报
回复
vcPlayer(我是一只小小鸟!)
for ( int l = 0 ; l < bmpheader1.biHeight ; ++l )
{
outfile.write( (const char *) &bits[l*bmpheader1.biWidth*3],bmpheader1.biWidth*3);
int k = bmpheader1.biWidth * 3 % 4;
if ( k != 0 )
{
while ( k-- != 0 )
outfile.put( '0x00' );
}
}


还是不行,好象和这个没有关系,我怀疑是从流中取的桢就有问题
(不知道可以怎么测试一下,数据太多了,对比不过来)
另外从流中取得的数据,我感觉,就已经填充了0,和按照逆序存储了.
vcPlayer 2006-06-03
  • 打赏
  • 举报
回复
1、请正确设置文件头;
2、一般是依行序逆序存储,也就是先存储最后一行,最后存储第一行;
3、最最最重要的是:每一行的长度是以4字节对齐的,如果不足请添“0”补足!!!!!!!!
没看你的代码,但从描述来看,问题是出在第三点。
syy64 2006-06-03
  • 打赏
  • 举报
回复
错位了吧。

19,469

社区成员

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

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