程序代码如下:
void CFaceView::OnFileOpen()
{
// TODO: Add your command handler code here
CFaceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); CString strFilter = "AVI Files (*.avi)|*.avi|All Files (*.*)|*.*||";
CFileDialog FileDlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
(LPCSTR)strFilter, this);
if (FileDlg.DoModal() == IDOK)
{
CString strFileName = FileDlg.GetPathName();
char szFileName[_MAX_PATH];
GetShortPathName((LPCSTR)strFileName, szFileName, _MAX_PATH); ExtractAVIFrames(strFileName);
Invalidate(TRUE);
}
}BOOL CFaceView::ExtractAVIFrames(CString szFileName)
{
AVIFileInit(); PAVIFILE avi;
int res=AVIFileOpen(&avi, szFileName, OF_READ, NULL); if (res!=AVIERR_OK)
{
//an error occures if (avi!=NULL)
AVIFileRelease(avi);
return FALSE;
} AVIFILEINFO avi_info;
AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO)); PAVISTREAM pStream;
//get avi stream
res=AVIFileGetStream(avi, &pStream, streamtypeVIDEO /*video stream*/,
0 /*first stream*/); if (res!=AVIERR_OK)
{
if (pStream!=NULL)
AVIStreamRelease(pStream); AVIFileExit();
return FALSE;
} //do some task with the stream int iNumFrames;
int iFirstFrame; iFirstFrame=AVIStreamStart(pStream);
if (iFirstFrame==-1)
{
//Error getteing the frame inside the stream
if (pStream!=NULL)
AVIStreamRelease(pStream); AVIFileExit();
return FALSE;
} iNumFrames=AVIStreamLength(pStream);
if (iNumFrames==-1)
{
//Error getteing the number of frames inside the stream
if (pStream!=NULL)
AVIStreamRelease(pStream);
AVIFileExit();
return FALSE;
} //getting bitmap from frame BITMAPINFOHEADER bih;
ZeroMemory(&bih, sizeof(BITMAPINFOHEADER)); bih.biBitCount=24; //24 bit per pixel bih.biClrImportant=0;
bih.biClrUsed = 0;
bih.biCompression = BI_RGB;
bih.biPlanes = 1;
bih.biSize = 40;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
//calculate total size of RGBQUAD scanlines (DWORD aligned) bih.biSizeImage = (((bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight ;
PGETFRAME pFrame;
pFrame=AVIStreamGetFrameOpen(pStream,
NULL/*(BITMAPINFOHEADER*) AVIGETFRAMEF_BESTDISPLAYFMT*/ /*&bih*/); //Get the first frame int index=0;
for (int i=iFirstFrame; i<iNumFrames; i++)
{
index= i-iFirstFrame; BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index); CreateFromPackedDIBPointer(pDIB, index);
}
AVIStreamGetFrameClose(pFrame); //close the stream after finishing the task if (pStream!=NULL)
AVIStreamRelease(pStream);
AVIFileExit(); return TRUE;
}有的avi文件可以读,但是有的不能读,不能读的问题出在AVIStreamGetFrameOpen这个函数上,这个函数返回为空,不知道是什么原因,请大家指教。
void CFaceView::OnFileOpen()
{
// TODO: Add your command handler code here
CFaceDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); CString strFilter = "AVI Files (*.avi)|*.avi|All Files (*.*)|*.*||";
CFileDialog FileDlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
(LPCSTR)strFilter, this);
if (FileDlg.DoModal() == IDOK)
{
CString strFileName = FileDlg.GetPathName();
char szFileName[_MAX_PATH];
GetShortPathName((LPCSTR)strFileName, szFileName, _MAX_PATH); ExtractAVIFrames(strFileName);
Invalidate(TRUE);
}
}BOOL CFaceView::ExtractAVIFrames(CString szFileName)
{
AVIFileInit(); PAVIFILE avi;
int res=AVIFileOpen(&avi, szFileName, OF_READ, NULL); if (res!=AVIERR_OK)
{
//an error occures if (avi!=NULL)
AVIFileRelease(avi);
return FALSE;
} AVIFILEINFO avi_info;
AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO)); PAVISTREAM pStream;
//get avi stream
res=AVIFileGetStream(avi, &pStream, streamtypeVIDEO /*video stream*/,
0 /*first stream*/); if (res!=AVIERR_OK)
{
if (pStream!=NULL)
AVIStreamRelease(pStream); AVIFileExit();
return FALSE;
} //do some task with the stream int iNumFrames;
int iFirstFrame; iFirstFrame=AVIStreamStart(pStream);
if (iFirstFrame==-1)
{
//Error getteing the frame inside the stream
if (pStream!=NULL)
AVIStreamRelease(pStream); AVIFileExit();
return FALSE;
} iNumFrames=AVIStreamLength(pStream);
if (iNumFrames==-1)
{
//Error getteing the number of frames inside the stream
if (pStream!=NULL)
AVIStreamRelease(pStream);
AVIFileExit();
return FALSE;
} //getting bitmap from frame BITMAPINFOHEADER bih;
ZeroMemory(&bih, sizeof(BITMAPINFOHEADER)); bih.biBitCount=24; //24 bit per pixel bih.biClrImportant=0;
bih.biClrUsed = 0;
bih.biCompression = BI_RGB;
bih.biPlanes = 1;
bih.biSize = 40;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
//calculate total size of RGBQUAD scanlines (DWORD aligned) bih.biSizeImage = (((bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight ;
PGETFRAME pFrame;
pFrame=AVIStreamGetFrameOpen(pStream,
NULL/*(BITMAPINFOHEADER*) AVIGETFRAMEF_BESTDISPLAYFMT*/ /*&bih*/); //Get the first frame int index=0;
for (int i=iFirstFrame; i<iNumFrames; i++)
{
index= i-iFirstFrame; BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index); CreateFromPackedDIBPointer(pDIB, index);
}
AVIStreamGetFrameClose(pFrame); //close the stream after finishing the task if (pStream!=NULL)
AVIStreamRelease(pStream);
AVIFileExit(); return TRUE;
}有的avi文件可以读,但是有的不能读,不能读的问题出在AVIStreamGetFrameOpen这个函数上,这个函数返回为空,不知道是什么原因,请大家指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货