CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  图形处理/算法

急!各位请进

楼主bjghm(明镜)2003-06-03 16:51:40 在 VC/MFC / 图形处理/算法 提问

怎样将一个数据流以avi形式保存 问题点数:0、回复次数:3Top

1 楼deverxp(abcdefg)回复于 2003-06-03 17:15:57 得分 0

WRITEAVI.C  
  /**************************************************************************  
    *  
    *     THIS   CODE   AND   INFORMATION   IS   PROVIDED   "AS   IS"   WITHOUT   WARRANTY   OF   ANY  
    *     KIND,   EITHER   EXPRESSED   OR   IMPLIED,   INCLUDING   BUT   NOT   LIMITED   TO   THE  
    *     IMPLIED   WARRANTIES   OF   MERCHANTABILITY   AND/OR   FITNESS   FOR   A   PARTICULAR  
    *     PURPOSE.  
    *  
    *     Copyright   (C)   1992   -   1996   Microsoft   Corporation.     All   Rights   Reserved.  
    *  
    **************************************************************************/  
  /****************************************************************************  
    *  
    *     WRITEAVI.C  
    *  
    *     Creates   the   file   OUTPUT.AVI,   an   AVI   file   consisting   of   a   rotating   clock  
    *     face.     This   program   demonstrates   using   the   functions   in   AVIFILE.DLL  
    *     to   make   writing   AVI   files   simple.  
    *  
    *     This   is   a   stripped-down   example;   a   real   application   would   have   a   user  
    *     interface   and   check   for   errors.  
    *  
    ***************************************************************************/  
  #include   "stdafx.h"  
  #include   <windowsx.h>  
  #include   <memory.h>  
  #include   <mmsystem.h>  
  #include   <vfw.h>  
   
  #include   "writeavi.h"  
   
  static   HANDLE     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_COLORS);  
   
  //   Fix   this   if   GetDIBits   messed   it   up....  
  lpbi->biClrUsed   =   (bits   <=   8)   ?   1<<bits   :   0;  
   
  DeleteDC(hdc)   ;  
  GlobalUnlock(hdib);  
   
  return   hdib   ;  
  }  
   
  CAVIFile::CAVIFile(LPCTSTR   lpszFileName,   int   xdim,   int   ydim)  
    : FName(lpszFileName),  
  xDim(xdim),   yDim(ydim),   bOK(true),   nFrames(0)  
  {  
  pfile   =   NULL;  
  ps   =   NULL;  
  psCompressed   =   NULL;  
  psText   =   NULL;  
  aopts[0]   =   &opts;  
  WORD   wVer   =   HIWORD(VideoForWindowsVersion());  
  if   (wVer   <   0x010A)  
  {  
  //   oops,   we   are   too   old,   blow   out   of   here  
  bOK   =   false;  
  }  
  else  
  {  
  AVIFileInit();  
  }  
  }  
   
  CAVIFile::~CAVIFile()  
  {  
  if   (ps)  
  AVIStreamClose(ps);  
   
  if   (psCompressed)  
  AVIStreamClose(psCompressed);  
   
  if   (psText)  
  AVIStreamClose(psText);  
   
  if   (pfile)  
  AVIFileClose(pfile);  
   
  WORD   wVer   =   HIWORD(VideoForWindowsVersion());  
  if   (wVer   >=   0x010A)  
  {  
  AVIFileExit();  
  }  
  }  
   
  bool   CAVIFile::AddFrame(CBitmap&   bmp)  
  {  
  HRESULT   hr;  
  char   szMessage[BUFSIZE];  
   
  if   (!bOK)  
  return   false;  
  LPBITMAPINFOHEADER   alpbi   =   (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp,   8));  
  if   (alpbi   ==   NULL)  
  return   false;  
  if   (xDim>=0   &&   xDim   !=   alpbi->biWidth)  
  {  
  GlobalFreePtr(alpbi);  
  return   false;  
  }  
  if   (yDim>=0   &&   yDim   !=   alpbi->biHeight)  
  {  
  GlobalFreePtr(alpbi);  
  return   false;  
  }  
  xDim   =   alpbi->biWidth;  
  yDim   =   alpbi->biHeight;  
  if   (nFrames   ==   0)  
  {  
  hr   =   AVIFileOpen(&pfile,         //   returned   file   pointer  
                FName, //   file   name  
        OF_WRITE   |   OF_CREATE,         //   mode   to   open   file   with  
        NULL); //   use   handler   determined  
  //   from   file   extension....  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
  _fmemset(&strhdr,   0,   sizeof(strhdr));  
  strhdr.fccType                                 =   streamtypeVIDEO;//   stream   type  
  strhdr.fccHandler                           =   0;  
  strhdr.dwScale                                 =   1;  
  strhdr.dwRate                                   =   15;         //   15   fps  
  strhdr.dwSuggestedBufferSize     =   alpbi->biSizeImage;  
  SetRect(&strhdr.rcFrame,   0,   0,         //   rectangle   for   stream  
  (int)   alpbi->biWidth,  
  (int)   alpbi->biHeight);  
   
  //   And   create   the   stream;  
  hr   =   AVIFileCreateStream(pfile,         //   file   pointer  
                    &ps,         //   returned   stream   pointer  
    &strhdr);         //   stream   header  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  _fmemset(&opts,   0,   sizeof(opts));  
   
  if   (!AVISaveOptions(NULL,   0,   1,   &ps,   (LPAVICOMPRESSOPTIONS   FAR   *)   &aopts))  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  hr   =   AVIMakeCompressedStream(&psCompressed,   ps,   &opts,   NULL);  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  hr   =   AVIStreamSetFormat(psCompressed,   0,  
        alpbi,         //   stream   format  
                alpbi->biSize   +       //   format   size  
                alpbi->biClrUsed   *   sizeof(RGBQUAD));  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  //   Fill   in   the   stream   header   for   the   text   stream....  
   
  //   The   text   stream   is   in   60ths   of   a   second....  
  /*  
  _fmemset(&strhdr,   0,   sizeof(strhdr));  
  strhdr.fccType                                 =   streamtypeTEXT;  
  strhdr.fccHandler                           =   mmioFOURCC('D',   'R',   'A',   'W');  
  strhdr.dwScale                                 =   1;  
  strhdr.dwRate                                   =   60;  
  strhdr.dwSuggestedBufferSize     =   sizeof(szText);  
  SetRect(&strhdr.rcFrame,   0,   (int)   alpbi->biHeight,  
  (int)   alpbi->biWidth,   (int)   alpbi->biHeight   +   TEXT_HEIGHT);  
   
  //   ....and   create   the   stream.  
  hr   =   AVIFileCreateStream(pfile,   &psText,   &strhdr);  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  dwTextFormat   =   sizeof(dwTextFormat);  
  hr   =   AVIStreamSetFormat(psText,   0,   &dwTextFormat,   sizeof(dwTextFormat));  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
  */  
  }  
   
  //   Jetzt   eigentliches   Schreiben  
  hr   =   AVIStreamWrite(psCompressed, //   stream   pointer  
  nFrames   *   10, //   time   of   this   frame  
  1, //   number   to   write  
  (LPBYTE)   alpbi   + //   pointer   to   data  
  alpbi->biSize   +  
  alpbi->biClrUsed   *   sizeof(RGBQUAD),  
  alpbi->biSizeImage, //   size   of   this   frame  
  AVIIF_KEYFRAME,   //   flags....  
  NULL,  
  NULL);  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
   
  //   Make   some   text   to   put   in   the   file   ...  
  //LoadString(hInstance,   IDS_TEXTFORMAT,   szMessage,   BUFSIZE   );  
  /*  
  strcpy(szMessage,   "This   is   frame   #%d");  
   
  int   iLen   =   wsprintf(szText,   szMessage,   (int)(nFrames   +   1));  
   
  //   ...   and   write   it   as   well.  
  hr   =   AVIStreamWrite(psText,  
  nFrames   *   40,  
  1,  
  szText,  
  iLen   +   1,  
  AVIIF_KEYFRAME,  
  NULL,  
  NULL);  
  if   (hr   !=   AVIERR_OK)  
  {  
  GlobalFreePtr(alpbi);  
  bOK   =   false;  
  return   false;  
  }  
  */  
  GlobalFreePtr(alpbi);  
   
  nFrames++;  
  return   true;  
  }  
   
  Top

2 楼deverxp(abcdefg)回复于 2003-06-03 17:19:13 得分 0

writeAVI.h  
  ==============  
   
   
  #include   <vfw.h>  
   
  #define   TEXT_HEIGHT 20  
  #define   AVIIF_KEYFRAME 0x00000010L   //   this   frame   is   a   key   frame.  
  #define   BUFSIZE   260  
   
  class   CAVIFile   :   public   CObject  
  {  
  public:  
  CAVIFile(LPCTSTR   lpszFileName,   int   xdim   =   -1,   int   ydim   =   -1);  
  virtual   ~CAVIFile();  
   
  virtual   bool   AddFrame(CBitmap&   bmp);  
  CString   GetFName()   const                           {return   FName;};  
  virtual   bool   IsOK()   const                         {return   bOK;};  
   
  private:  
  CString   FName;  
  int   xDim;  
  int   yDim;  
   
  AVISTREAMINFO   strhdr;  
  PAVIFILE   pfile;  
  PAVISTREAM   ps;  
  PAVISTREAM   psCompressed;  
  PAVISTREAM   psText;  
  AVICOMPRESSOPTIONS   opts;  
  AVICOMPRESSOPTIONS   FAR   *   aopts[1];  
  DWORD   dwTextFormat;  
  char   szText[BUFSIZE];  
  int   nFrames;  
  bool   bOK;  
  };  
   
   
  请注意揭帖,要不然下次可能就没人理你了,我想分想疯了。Top

3 楼bjghm(明镜)回复于 2003-06-03 17:50:41 得分 0

谢谢你,你是自己写的一个avi类吗?我现在很急,没时间看,能帮我注视一下吗?一定给分!!!!Top

相关问题

  • 各位,请进!
  • 各位请进!
  • 各位请进!
  • 急!各位vfp高手,请进!
  • 急,请各位进来看看
  • 急~!!!各位杀毒高手请进
  • 请教各位!!急!
  • 请教各位,急!!
  • 急急急!!请各位帮助!!FASTREPORT报表高手请进!
  • 请各位大虾进来看看,急急急!!!

关键词

  • hr
  • null
  • alpbi
  • lpbi
  • strhdr
  • wcolsize
  • writeavi
  • xdim
  • ydim
  • bitmap

得分解答快速导航

  • 帖主:bjghm

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo