版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、前言DirectShow是微软公司提供的一套在Win dows平台上进行流媒体处理的开发包,与DirectX开发包一起发布。DirectShow为多媒体流的捕捉和回放提供了强有力的支持。用DirectShow开发应用程序,我们可以很方便地从支持WDM驱动模型的采集卡上捕获数据,并且进行相应的后期处理乃至存储到文件中。DirectShow 是基于 COM的,为了编写 DirectShow应用程序,需要了解COM客户程序编写的基础知识。DirectShow提供了大量的接口,但在编程中发现还是不够方便,如果能构建一个视频捕捉类把常用的一些动作封装起来,那么就更方便了。编程思路为了更加容易建立视频捕捉
2、应用程序,DirectShow 提供了一个叫做Capture GraphBuilder 的对象,Capture Graph Builder 提供 lcaptureGraphBuilder2 接口,该接口 可以建立和控制 Capture Graph 。建立视频捕捉程序,必须首先获取并初始化lcaptureGraphBuilder2 接口,然后选择一个适当的视频捕捉设备。选择好设备后,为该设备创建Capture filter,然后调用AddFilter 把 Capture filter 添加到 Filter Graph 。如果仅仅希望用摄像头来进行实时监控的话,只需要在上面的基础上调用ICaptu
3、reGraphBuilder2:Re nderStream就可以了:ICaptureGraphBuilder2 *pBuild; / Capture Graph Builder/省略初始化部分代码IBaseFilter *pCap; / Video capture filter./省略初始化和添加到 Filter Graph部分代码pBuild-> RenderStream(&PIN_CATEGORY_PREVIEW, & MEDIATYPE_Video, pCap, NULL,NULL);DirectShow提供了一个捕捉静态图像的方法:使用Sample Grabber
4、 filter 。依次按照以下三个步骤就可以了:第一步,定义一个类实现 Sample Grabber的回调接口 IsampleGrabberCB :/在后面提供的类中具体完成CSampleGrabberCB mCB;第二步、调用 RenderStream依次把Still pin 、Sample Grabber和系统默认Ren derer Filter连接起来。第三步、配置Sample Grabber以捕获数据。视频捕捉类CCaptureVideo的具体实现/ CCaptureVideo视频捕捉类头文件/#if !defined(AFX_CAPTUREVIDEO_H_F5345AA4_A39F_
5、4B07_B843_3D87C4287AA0_INC LUDED_)#defineAFX_CAPTUREVIDEO_H_F5345AA4_A39F_4B07_B843_3D87C4287AA0_INCLUDED_/ CaptureVideo.h : header file/#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000#include <atlbase.h>#include <windows.h>#include <dshow.h>#ifndef SAFE_RELEASE#defin
6、e SAFE_RELEASE( x ) if ( NULL != x ) x->Release( ); x = NULL; #endifclass CSampleGrabberCB;class CCaptureVideo : public CWndfriend class CSampleGrabberCB;public:void GrabOneFrame(BOOL bGrab);HRESULT Init(int iDevicelD,HWND hWnd);int EnumDevices(HWND hList);CCaptureVideo();virtual CCaptureVideo();
7、private:HWND m_hWnd;IGraphBuilder *m_pGB;ICaptureGraphBuilder2* m_pCapture;IBaseFilter* m_pBF;IMediaControl* m_pMC;IVideoWindow* m_pVW;CComPtr<ISampleGrabber> m_pGrabber;protected:void FreeMediaType(AM_MEDIA_TYPE & mt);bool BindFilter(int deviceId, IBaseFilter *pFilter);void ResizeVideoWin
8、dow();HRESULT SetupVideoWindow();HRESULT InitCaptureGraphBuilder();#endif/ !defined(AFX_CAPTUREVIDEO_H_F5345AA4_A39F_4B07_B843_3D87C4287AA0_INCLUDED_)/ CCaptureVideo 视频捕捉类实现文件 CaptureVideo.cpp |/ CaptureVideo.cpp: implementation of the CCaptureVideo class./#include "stdafx.h"#include "
9、;CaptureVideo.h" |#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE=_FILE_;#define new DEBUG_NEW#endifBOOL bOneShot=FALSE;全局变量class CSampleGrabberCB : public ISampleGrabberCBpublic:long lWidth;long lHeight;TCHAR m_szFileNameMAX_PATH;位图文件名称CSampleGrabberCB( )strcpy(m_szFileName, "c:donaldo
10、.bmp");STDMETHODIMP_(ULONG) AddRef() return 2; STDMETHODIMP_(ULONG) Release。 return 1; STDMETHODIMP Querylnterface(REFIID riid, void * ppv)if( riid = IID_ISampleGrabberCB | riid = IID_IUnknown )*ppv = (void *) static_cast<ISampleGrabberCB*> ( this );return NOERROR;return E_NOINTERFACE;STD
11、METHODIMP SampleCB( double SampleTime, IMediaSample * pSample ) return 0;STDMETHODIMP BufferCB( double dblSampleTime, BYTE * pBuffer, long lBufferSize )if( !bOneShot )return 0;if (!pBuffer)return E_POINTER;SaveBitmap(pBuffer , lBufferSize);bOneShot = FALSE;return 0;/创建位图文件BOOL SaveBitmap(BYTE * pBuf
12、fer , long lBufferSize )HANDLE hf = CreateFile(m_szFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,CREATE_ALWAYS, NULL, NULL );if( hf = INVALID_HANDLE_VALUE )return 0;/写文件头BITMAPFILEHEADER bfh;memset( &bfh, 0, sizeof( bfh );bfh.bfType =' MB ;bfh.bfSize = sizeof( bfh ) + lBufferSize + sizeof(
13、BITMAPINFOHEADER );bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( BITMAPFILEHEADER );DWORD dwWritten = 0;WriteFile( hf, &bfh, sizeof( bfh ), & dwWritten, NULL );/写位图格式BITMAPINFOHEADER bih;memset( &bih, 0, sizeof( bih );bih.biSize = sizeof( bih );bih.biWidth = lWidth;bih.biHeight =
14、lHeight;bih.biPlanes = 1;bih.biBitCount = 24;WriteFile( hf, & bih, sizeof( bih ), & dwWritten, NULL );/写位图数据WriteFile( hf, pBuffer , lBufferSize, & dwWritten, NULL );CloseHandle( hf );return 0;;CSampleGrabberCB mCB;/ Construction/Destruction/CCaptureVideo:CCaptureVideo()/COM Library Inti
15、alizationif(FAILED(Colnitialize(NULL)/*, COINIT_APARTMENTTHREADED)*/AfxMessageBox("Colnitialize Failed!rn");return;m_hWnd = NULL;m_pVW = NULL;m_pMC = NULL;m_pGB = NULL;m_pCapture = NULL;CCaptureVideo:CCaptureVideo()/ Stop media playback if(m_pMC)m_pMC->Stop();if(m_pVW)m_pVW->put_Visi
16、ble(OAFALSE);m_pVW->put_Owner(NULL);SAFE_RELEASE(m_pCapture);SAFE_RELEASE(m_pMC);SAFE_RELEASE(m_pGB);SAFE_RELEASE(m_pBF);CoUninitialize();int CCaptureVideo:EnumDevices(HWND hList)if (!hList)return -1;int id = 0;/枚举视频扑捉设备ICreateDevEnum *pCreateDevEnum;NULL,HRESULT hr = CoCreatelnstance(CLSID_Syste
17、mDeviceEnum, CLSCTX_INPROC_SERVER,IIDCreateDevEnum, (void*)&pCreateDevEnum);if (hr != NOERROR)return -1;CComPtr<IEnumMoniker> pEm;hrpCreateDevEnum->CreateClassEnumerator(CLSID_VideolnputDeviceCategory,&pEm, 0);if (hr != NOERROR)return -1;pEm->Reset();ULONG cFetched;IMoniker *pM;w
18、hile(hr = pEm->Next(1, &pM, & cFetched), hr=S_OK)IPropertyBag *pBag;hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void *)&pBag);if(SUCCEEDED(hr)VARIANT var;var.vt = VT_BSTR;hr = pBag->Read(L"FriendlyName", &var, NULL);if (hr = NOERROR)TCHAR str2048;id+;WideCharTo
19、MultiByte(CP_ACP ,0,var .bstrVal, -1, str , 2048, NULL, NULL);:SendMessage(hList, CB_ADDSTRING, 0,(LPARAM)str); SysFreeString(var .bstrVal);pBag->Release();pM->Release();return id;HRESULT CCaptureVideo:Init(int iDeviceID, HWND hWnd)HRESULT hr;hr = InitCaptureGraphBuilder();if (FAILED(hr)AfxMes
20、sageBox("Failed to get video interfaces!");return hr;/ Bind Device Filter . We know the device because the id was passed in|if(!BindFilter(iDevicelD, &m_pBF)return S_FALSE;hr = m_pGB->AddFilter(m_pBF, L"Capture Filter");/ hr = m_pCapture->RenderStream(&PIN_CATEGORY_
21、PREVIEW, & MEDIATYPE_Video,/ m_pBF , NULL, NULL);/ create a sample grabberhr = m_pGrabber .CoCreatelnstance( CLSID_SampleGrabber );if( !m_pGrabber )AfxMessageBox("Fail to create SampleGrabber, maybe qedit.dll is not registered?");return hr;CComQIPtrv IBaseFilter , &IID_IBaseFilter
22、> pGrabBase( m_pGrabber );/设置视频格式AM_MEDIA_TYPE mt;ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE);mt.majortype = MEDIATYPE_Video;mt.subtype = MEDIASUBTYPE_RGB24;hr = m_pGrabber->SetMediaType (&m t);if( FAILED( hr ) )AfxMessageBox("Fail to set media type!");return hr;hr = m_pGB->AddF
23、ilter( pGrabBase, L"Grabber");if( FAILED( hr ) )AfxMessageBox("Fail to put sample grabber in graph");return hr;/ try to render preview/capture pinhr=m_pCapture->RenderStream(&PIN_CATEGORY_PREVIEW& MEDIATYPE_Video,m_pBF ,pGrabBase,NULL);if( FAILED( hr )hr =m_pCapture-&g
24、t;RenderStream(&PIN_CATEGORY_CAPTURE,& MEDIATYPE_Video,m_pBF ,pGrabBase,NULL);if( FAILED( hr ) )AfxMessageBox("Can' t build the graph");return hr;hr = m_pGrabber->GetConnectedMediaType( &mt );if ( FAILED( hr) )AfxMessageBox("Failt to read the connected media type&qu
25、ot;);return hr;VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;mCB.lWidth = vih->bmiHeader.biWidth;mCB.lH eight = vih->bmiHeader.biHeight;FreeMediaType(mt);hr = m_pGrabber->SetBufferSamples( FALSE );hr = m_pGrabber->SetOneShot( FALSE );hr = m_pGrabber->SetCallback( & mCB, 1
26、 );/设置视频捕捉窗口m_hWnd = hWnd ;SetupVideoWindow();hr = m_pMC->Run();开始视频捕捉if(FAILED(hr)AfxMessageBox("Couldn' t run the graph!");return hr;return S_OK;bool CCaptureVideo:BindFilter(int deviceId, IBaseFilter *pFilter)if (deviceId < 0)|return false;/ enumerate all video capture devices
27、CComPtr<ICreateDevEnum> pCreateDevEnum;NULL,HRESULT hr = CoCreatelnstance(CLSID_SystemDeviceEnum, CLSCTX_INPROC_SERVER,IID_ICreateDevEnum, (void* )&pCreateDevEnum);if (hr != NOERROR)return false;CComPtr<IEnumMoniker> pEm;hrpCreateDevEnum->CreateClassEnumerator(CLSID_VideolnputDevi
28、ceCategory,&pEm, 0);if (hr != NOERROR)return false;pEm->Reset();ULONG cFetched;IMoniker *pM;int index = 0;while(hr = pEm->Next(1, &pM, &cFetched), hr=S_OK, index <= deviceId)IPropertyBag *pBag;hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void *)&pBag);if(SUCCEEDED(hr)V
29、ARIANT var;var .vt = VT_BSTR;hr = pBag->Read(L"FriendlyName", &var, NULL);if (hr = NOERROR)if (index = deviceId)pM->BindToObject(0, 0, IID_IBaseFilter,(void*)pFilter);SysFreeString(var .bstrVal);pBag->Release();pM->Release();HRESULT CCaptureVideo:lnitCaptureGraphBuilder()HR
30、ESULT hr;/ 创建 IGraphBuilder 接口hr=CoCreatelnstance(CLSID_FilterGraph,NULL, CLSCTX_INPROC_SERVER,IID_IGraphBuilder , (void *)&m_pGB);/ 创建 ICaptureGraphBuilder2 接口hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,IID_ICaptureGraphBuilder2, (void *) &m_pCapture);if (FAILED(
31、hr)return hr;m_pCapture->SetFiltergraph(m_pGB);hr = m_pGB->Querylnterface(IID_IMediaControl, (void *)&m _pMC);if (FAILED(hr)return hr;hr = m_pGB->Querylnterface(IID_IVideoWindow, (LPVOID *) & m_pVW);if (FAILED(hr)return hr;return hr;HRESULT CCaptureVideo:SetupVideoWindow()HRESULT hr
32、;hr = m_pVW->put_Owner(OAHWND)m_hWnd);if (FAILED(hr)return hr;hr = m_pVW->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);if (FAILED(hr)return hr;ResizeVideoWindow();hr = m_pVW->put_Visible(OATRUE);return hr;void CCaptureVideo:ResizeVideoWindow()if (m_pVW)/让图像充满整个窗口CRect rc;:GetClientRect(m_hWn
33、d, &rc);m_pVW->SetWindowPosition(0, 0, rc.right, rc.bottom);void CCaptureVideo:GrabOneFrame(BOOL bGrab)bOneShot = bGrab;void CCaptureVideo:FreeMediaType(AM_MEDIA_TYPE & mt) if (mt.cbFormat != 0) CoTaskMemFree(PVOID)mt.pbFormat);/ Strictly unnecessary but tidiermt.cbFormat = 0;mt.pbFormat = NULL;if (mt.pUnk != NULL) mt.pUnk->Release();mt.pUnk = NULL;如何使用视频捕捉类 CCaptureVideo构建CCaptureV
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 污泥委托处置合同范例
- 单位物业工程合同范例
- 服装制版师习题
- 2025年聊城道路运输从业资格考试系统
- 售后返租合同范例
- 工程招商合同范例
- 商业演出合同范例
- 本地户外租赁合同范例
- 电控箱加工合同范例
- 2025年七台河货运从业资格证考试题库答案
- 铁路建设项目施工企业信用评价办法(铁总建设〔2018〕124号)
- 模具报价表精简模板
- 形式发票模板 PI模板 英文版
- 初一的最美的风景高分的作文600字
- 高考英语单项选择题题库题
- 检验检测机构资质认定现场评审日程表及签到表
- 完整版高低压开关柜投标文件技术标
- 兰州市行政区划代码表
- 管鲍之交-历史剧剧本(共4页)
- [交流][jtag]跟我学jtag协议破解——第一弹初识jtagtap状态机
- 尼康FM2说明书25页
评论
0/150
提交评论