请大家给看看程序:avi视频读取和重建
我应用VFW.H,vfw32.lib读取 和重建AVI视频。首先读取视频的第50祯,然后重建一个200
祯的AVI视频,视频的每一祯,都是原来视频的第 50 祯。程序在AVIStreamWrite()处出错
。重建的视频,没有内容,不知道什么地方出错了,请大家指教。
void CVideowatermarkingDlg::Onmain()
{
UpdateData();
if(m_input.IsEmpty())
{
AfxMessageBox("please input avi file name",MB_ICONEXCLAMATION|MB_OK);
return;
}
/////////////////////////////////////////
AVIFileInit();
hResult=AVIFileOpen(&pfile,m_input,OF_READWRITE,0L);
if(hResult!=0)
{
AVIFileExit();
AfxMessageBox("can not open this avi,there maybe have a mistake");
}
hResult=AVIFileInfo(pfile,&thisPfi,sizeof(AVIFILEINFO));
if(hResult!=0)
{
AfxMessageBox("can not appear to be an avi file");
}
hResult=AVIFileGetStream(pfile,&pVideoStream,streamtypeVIDEO,0);
if(hResult!=0)
{
AfxMessageBox("coult not retrieve video stream");
}
pget=AVIStreamGetFrameOpen(pVideoStream,NULL);
lpbi=(LPBITMAPINFOHEADER)AVIStreamGetFrame(pget,50);
HBITMAP hBmp;
hBmp=CreateDIBitmap(NULL,lpbi,CBM_INIT,lpData,PBITMAPINFO(lpbi),DIB_RGB_COLOR
S);
//////////
HANDLE hDib=NULL;
/////////////////
hDib=makedib(hBmp,24);
if(hDib==NULL)
{
AfxMessageBox("can not hDib");
}
lpbi=(LPBITMAPINFOHEADER)GlobalLock(hDib);
//////////
pfile=NULL;
PAVISTREAM ps=NULL;
AVISTREAMINFO asi;
hResult=AVIFileOpen(&pfile,"C:\\testwatermarked.avi",OF_WRITE|OF_CREATE,NULL)
;
if(hResult==AVIERR_OK)
{
asi.fccType=streamtypeVIDEO;
asi.fccHandler=0;
asi.dwScale=1;
asi.dwRate=25;
asi.dwSuggestedBufferSize=lpbi->biSizeImage;
}
hResult=AVIFileCreateStream(pfile,&ps,&asi);
if(hResult!=0)
{
AfxMessageBox("can not AVIFileCreateStream");
}
hResult=AVIStreamSetFormat(ps,0,lpbi,lpbi->biSize);
if(hResult!=0)
{
AfxMessageBox("can not AVIStreamSetFormat");
}
for(i=0;i<20;i++)
{
hResult=AVIStreamWrite(ps,i,1,(LPBYTE)lpbi+lpbi->biSize,lpbi->biSizeImage,AV
IIF_KEYFRAME,NULL,NULL);
}
if(hResult!=0)
{
AfxMessageBox("can not Stream Write");
}
/////////////////
UpdateData(FALSE);
AVIFileRelease(pfile);
AVIFileExit();
}
HANDLE CVideowatermarkingDlg::makedib(HBITMAP hbitmap, UINT bits)
{
HANDLE hdib ;
HDC hdc ;
BITMAP bitmap ;
UINT wLineLen ;
DWORD dwSize ;
DWORD wColSize ;
LPBITMAPINFOHEADER lpbi ;
LPBYTE lpBits ;
GetObject(hbitmap,sizeof(BITMAP),&bitmap) ;
//
// DWORD align the width of the DIB
// Figure out the size of the colour table
// Calculate the size of the DIB
//
wLineLen = (bitmap.bmWidth*bits+31)/32 * 4;
wColSize = sizeof(RGBQUAD)*((bits <= 8) ? 1<<bits : 0);
dwSize = sizeof(BITMAPINFOHEADER) + wColSize +
(DWORD)(UINT)wLineLen*(DWORD)(UINT)bitmap.bmHeight;
//
// Allocate room for a DIB and set the LPBI fields
//
hdib = GlobalAlloc(GHND,dwSize);
if (!hdib)
return hdib ;
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hdib) ;
lpbi->biSize = sizeof(BITMAPINFOHEADER) ;
lpbi->biWidth = bitmap.bmWidth ;
lpbi->biHeight = bitmap.bmHeight ;
lpbi->biPlanes = 1 ;
lpbi->biBitCount = (WORD) bits ;
lpbi->biCompression = BI_RGB ;
lpbi->biSizeImage = dwSize - sizeof(BITMAPINFOHEADER) - wColSize ;
lpbi->biXPelsPerMeter = 0 ;
lpbi->biYPelsPerMeter = 0 ;
lpbi->biClrUsed = (bits <= 8) ? 1<<bits : 0;
lpbi->biClrImportant = 0 ;
//
// Get the bits from the bitmap and stuff them after the LPBI
//
lpBits = (LPBYTE)(lpbi+1)+wColSize ;
hdc = CreateCompatibleDC(NULL) ;
GetDIBits(hdc,hbitmap,0,bitmap.bmHeight,lpBits,(LPBITMAPINFO)lpbi, DIB_RGB_CO
LORS);
// Fix this if GetDIBits messed it up....
lpbi->biClrUsed = (bits <= 8) ? 1<<bits : 0;
DeleteDC(hdc) ;
GlobalUnlock(hdib);
return hdib ;
}