DirectShow图片的抓取

本文介绍两种使用DirectShow进行图片抓取的方法。第一种利用IBasicVideo接口的GetCurrentImage方法直接捕获当前显示的图像帧并保存为位图文件。第二种方法则较为复杂,涉及到SampleGrabberFilter的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DirectShow图片的抓取
2010-08-11 14:31
转载自  MegaChan
最终编辑  Guo_1017
第1种方法是使用IBasicVideo::GetCurrentImage:
bool SnapshotBitmap(IBaseicVideo *pBasicVideo,const char *outFile)
{
    if(pBasicVideo)
 {
  long bitmapSize=0;
  //首先获得图像大小
  if(SUCCEEDED(pBasicVideo->GetCurrentImage(&bitmapSize,0)))
  {
   bool pass=false;
   //分配图像帧内存
   unsigned char *buffer=new unsigned char[bitmapSize];
   //获取图像帧数据
   if(SUCCEEDED(pBasicVideo->GetCurrentImage(&bitmapSize,(long*)buffer)))
   {
    BITMAPFILEHEADER hdr;
    LPBITMAPINFOHEADER lpbi;
    lpbi=(LPBITMAPINFOHEADER)buffer;
    int nColors=1<<lpbi->biBitCount;
    if(nColors>256)
    nColors=0;
    hdr.bfType=((WORD)('M'<<8)|'B');
    hdr.bfSize=bitmapSize+sizeof(hdr);
    hdr.bfReserved1=0;
    hdr.bfReserved2=0;
    hdr.bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+lpbi->biSize+nColors*sizeof(RGBQUAD));
    CFile bitmapFile(outFile,CFile::modeReadWrite|CFile::modeCreate|CFile::typeBinary);
    //写入位图文件头
    bitmapFile.Write(&hdr,sizeof(BITMAPFILEHEADER));
    //写入图像帧数据
    bitmapFile.Write(buffer,bitmapSize);
    bitmapFile.Close();
    pass=true;
   }
   delete []buffer;
   return pass;
  }
 }
 return false;
}
第2种方法比较复杂,使用Sample Grabber Filter.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值