1. 从摄像头采集到视频数据保存为未压缩的avi硬盘文件,路线:capCreateCaptureWindow --> capDriverConnect --> capPreviewRate --> capFileSetCaptureFile。用上面任何一个函数名在网上搜索,可以找到很多视频采集的示例;
2. avi2bmp和bmp2avi
BOOL CMainFrame::AviToBmp(CString szAVIName, CString strBmpDir) { // TODO: 在此添加控件通知处理程序代码 AVIFileInit(); //initializes the AVIFile library PAVIFILE avi; int res=AVIFileOpen(&avi, szAVIName, OF_READ, NULL); int n = GetLastError(); if (res!=AVIERR_OK) { //an error occures if (avi!=NULL) AVIFileRelease(avi); return FALSE; } AVIFILEINFO avi_info; //structure contains global information for an entire AVI file. AVIFileInfo(avi, &avi_info, sizeof(AVIFILEINFO)); PAVISTREAM pStream; 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)); //fills a block of memory with zeros bih.biBitCount=24; //24 bit per pixel bih.biClrImportant=0; bih.biClrUsed = 0; bih.biCompression = BI_RGB; bih.biPlanes = 1; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biWidth=avi_info.dwWidth; bih.biHeight=avi_info.dwHeight; //calculate total size of RGBQUAD scanlines (DWORD aligned) bih.biSizeImage =(((bih.biWidth * 3) + 3) & 0xFFFC) * bih.biHeight; PGETFRAME pFrame; pFrame=AVIStreamGetFrameOpen(pStream, NULL); AVISTREAMINFO streaminfo; AVIStreamInfo(pStream,&streaminfo,sizeof(AVISTREAMINFO)); //Get the first frame //BITMAPINFOHEADER bih2; long lsize = sizeof(bih); int index=0; for (int i=iFirstFrame; i<10; i++) { index= i-iFirstFrame; BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index); //返回该帧的地址 //AVIStreamReadFormat(pStream,index,&bih,&lsize); BITMAPFILEHEADER stFileHdr; BYTE* Bits=new BYTE[bih.biSizeImage]; ZeroMemory(Bits,bih.biSizeImage); memcpy(Bits,pDIB-1,bih.biSizeImage); /* for(int j=0;j<int(bih.biSizeImage);j++) { *(Bits+j)=*(pDIB+j+2); } if(!AVIStreamRead(pStream,index,bih.biWidth*bih.biHeight,&Bits,bih.biSizeImage,NULL,NULL)) { MessageBox("Data read fail",NULL,MB_OK); }*/ //RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), bih2.biSizeImage); bih.biClrUsed =0; stFileHdr.bfOffBits=sizeof(BITMAPFILEHEADER);//+sizeof(BITMAPINFOHEADER); stFileHdr.bfSize=bih.biSizeImage+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); stFileHdr.bfType=0x4d42; stFileHdr.bfReserved1=0; stFileHdr.bfReserved2=0; CString FileName; FileName.Format("Frame-%05d.bmp", index); CString strtemp = strBmpDir; strtemp += "\\"; strtemp += FileName; FILE* fp=_tfopen(strtemp ,_T("wb")); fwrite(&stFileHdr,1,sizeof(BITMAPFILEHEADER),fp); fwrite(&bih,1,sizeof(BITMAPINFOHEADER),fp); int ff = fwrite(Bits,1,bih.biSizeImage,fp); int e = GetLastError(); fclose(fp); ///// delete Bits; //CreateFromPackedDIBPointer(pDIB, index); } AVIStreamGetFrameClose(pFrame); //close the stream after finishing the task if (pStream!=NULL) AVIStreamRelease(pStream); AVIFileExit(); return TRUE; } BOOL CMainFrame::BmpToAvi(CString szAVIName, CString strBmpDir) { CFileFind finder; strBmpDir += _T("\\*.*"); AVIFileInit(); AVISTREAMINFO strhdr; PAVIFILE pfile; PAVISTREAM ps; int nFrames =0; HRESULT hr; BOOL bFind = finder.FindFile(strBmpDir); while(bFind) { bFind = finder.FindNextFile(); if(!finder.IsDots() && !finder.IsDirectory()) { CString str = finder.GetFilePath(); FILE *fp = fopen(str,"rb"); BITMAPFILEHEADER bmpFileHdr; BITMAPINFOHEADER bmpInfoHdr; fseek( fp,0,SEEK_SET); fread(&bmpFileHdr,sizeof(BITMAPFILEHEADER),1, fp); fread(&bmpInfoHdr,sizeof(BITMAPINFOHEADER),1, fp); BYTE *tmp_buf = NULL; if(nFrames ==0 ) { AVIFileOpen(&pfile,szAVIName,OF_WRITE | OF_CREATE,NULL); memset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = 1; strhdr.dwRate = 15; // 15 fps strhdr.dwSuggestedBufferSize = bmpInfoHdr.biSizeImage ; SetRect(&strhdr.rcFrame, 0, 0, bmpInfoHdr.biWidth, bmpInfoHdr.biHeight); // And create the stream; hr = AVIFileCreateStream(pfile,&ps,&strhdr); // hr = AVIStreamSetFormat(ps,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr)); } tmp_buf = new BYTE[bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3]; fread(tmp_buf, 1, bmpInfoHdr.biWidth * bmpInfoHdr.biHeight * 3, fp); hr = AVIStreamSetFormat(ps,nFrames,&bmpInfoHdr,sizeof(bmpInfoHdr)); hr = AVIStreamWrite(ps, // stream pointer nFrames , // time of this frame 1, // number to write (LPBYTE) tmp_buf, bmpInfoHdr.biSizeImage , // size of this frame AVIIF_KEYFRAME, // flags.... NULL, NULL); nFrames ++; fclose(fp); } } AVIStreamClose(ps); if(pfile != NULL) AVIFileRelease(pfile); AVIFileExit(); return TRUE; }