三、AVICap编程示例
下面以一个简单的应用程序为例说明AVICap的使用,该程序对输入的视频流进
行实时的显示和捕获,演示需要一个视频捕获卡和摄像头。界面中的菜单项如图1
所示。其中,菜单项Display可以以Preview 或Overlay模式显示图像;菜单项
Setting可通过弹出AVICap提供的对话框Video Source、Video Format和Video
Display来对捕获进行设置,图4 中的图像就是按照图2、图3的对话框所示进行设
置、以Preview模式显示的结果;菜单项Capture可将视频流或单帧图像捕获到指定
的文件中去。
图1 菜单项
图2 Video Format对话框
图3 Video Source对话框
图4 图2和图3设置下显示的一帧图
由于篇幅有限,下面仅介绍与视频捕获相关的编程。
1、定义全局变量:
HWND ghWndCap ; //捕获窗的句柄
CAPDRIVERCAPS gCapDriverCaps ; //视频驱动器的能力
CAPSTATUS gCapStatus ; //捕获窗的状态
2、处理WM_CREATE消息:
//创建捕获窗,其中hWnd为主窗口句柄
ghWndCap = capCreateCaptureWindow((LPSTR)"Capture Window",
WS_CHILD | WS_VISIBLE, 0, 0, 300,240, (HWND) hWnd, (int) 0);
//登记三个回调函数,它们应被提前申明
capSetCallbackOnError(ghWndCap, (FARPROC)ErrorCallbackProc);
capSetCallbackOnStatus(ghWndCap, (FARPROC)StatusCallbackProc);
capSetCallbackOnFrame(ghWndCap, (FARPROC)FrameCallbackProc);
capDriverConnect(ghWndCap,0); // 将捕获窗同驱动器连接
//获得驱动器的能力,相关的信息放在结构变量gCapDriverCaps中
capDriverGetCaps(ghWndCap,&gCapDriverCaps,sizeof(CAPDRIVERCAPS))
;
3、处理WM_CLOSE消息:
//取消所登记的三个回调函数
capSetCallbackOnStatus(ghWndCap, NULL);
capSetCallbackOnError(ghWndCap, NULL);
capSetCallbackOnFrame(ghWndCap, NULL);
capCaptureAbort(ghWndCap);//停止捕获
capDriverDisconnect(ghWndCap); //将捕获窗同驱动器断开
4、处理菜单项Preview:
capPreviewRate(ghWndCap, 66); // 设置Preview模式的显示速率
capPreview(ghWndCap, TRUE); //启动Preview模式
5、处理菜单项Overlay:
if(gCapDriverCaps.fHasOverlay) //检查驱动器是否有叠加能力
capOverlay(ghWndCap,TRUE); //启动Overlay模式
6、处理菜单项Exit:
SendMessage(hWnd,WM_CLOSE,wParam,lParam);
7、分别处理Setting下的三个菜单项,它们可分别控制视频源、视频格式及显
示:
if (gCapDriverCaps.fHasDlgVideoSource)
capDlgVideoSource(ghWndCap); //Video source 对话框
if (gapDriverCaps.fHasDlgVideoFormat)
capDlgVideoFormat(ghWndCap); // Video format 对话框
if (CapDriverCaps.fHasDlgVideoDisplay)
capDlgVideoDisplay(ghWndCap); // Video display 对话框
8、处理Video Stream菜单项,它捕获视频流到一个.AVI文件:
char szCaptureFile[] = "MYCAP.AVI";
capFileSetCaptureFile( ghWndCap, szCaptureFile); //指定捕获文件名
capFileAlloc( ghWndCap, (1024L * 1024L * 5)); //为捕获文件分配存
储空间
capCaptureSequence(ghWndCap); //开始捕获视频序列
9、处理Single Frame菜单项:
capGrabFrame(ghWndCap); //捕获单帧图像
10、定义三个回调函数:
LRESULT CALLBACK StatusCallbackProc(HWND hWnd, int nID, LPSTR
lpStatusText)
{
if (!ghWndCap) return FALSE;
//获得捕获窗的状态
capGetStatus(ghWndCap, &gCapStatus, sizeof (CAPSTATUS));
//更新捕获窗的大小
SetWindowPos(ghWndCap, NULL, 0, 0, gCapStatus.uiImageWidth,
gCapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
if (nID == 0) { // 清除旧的状态信息
SetWindowText(ghWndCap, (LPSTR) gachAppName);
return (LRESULT) TRUE;
}
// 显示状态 ID 和状态文本
wsprintf(gachBuffer, "Status# %d: %s", nID, lpStatusText);
SetWindowText(ghWndCap, (LPSTR)gachBuffer);
return (LRESULT) TRUE;
}
LRESULT CALLBACK ErrorCallbackProc(HWND hWnd, int nErrID,LPSTR
lpErrorText)
{
if (!ghWndCap)
return FALSE;
if (nErrID == 0)
return TRUE;// 清除旧的错误
wsprintf(gachBuffer, "Error# %d", nErrID); //显示错误标识和文本
MessageBox(hWnd, lpErrorText, gachBuffer,MB_OK |
MB_ICONEXCLAMATION);
return (LRESULT) TRUE;
}
LRESULT CALLBACK FrameCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)
{
if (!ghWndCap)
return FALSE;
//假设fp为一打开的.dat文件指针
fwrite(fp,lpVHdr->lpData,lpVHdr->dwBufferLength,1);
return (LRESULT) TRUE ;
}
值得注意的是:应在.cpp文件中加入#include 一句,在Link设置中加入
vfw32.lib。
上述的回调函数FrameCallbackProc是将视频数据直接从缓冲写入文件,也可
利用memcpy函数将视频数据直接拷贝到另一缓存。同理,可定义
VideoStreamCallbackProc。capSetCallbackOnVideoStream的使用比
capSetCallbackOnFrame稍微复杂一些。在捕获过程中,当一个新的视频缓冲可得
时,系统就调用它所登记的回调函数。在缺省情况下,捕获窗在捕获过程中不允许
其它应用程序继续运行。为了取消这个限制,可以设置CAPTUREPARMS的成员
fYield为TRUE或建立一个Yield回调函数。为了解决潜在的重入(reentry)问题,
可在YieldCallbackProc中用PeekMessage过滤掉一些消息,例如鼠标消息。
四、结束语
Visual C++提供的AVICap窗口类为捕获数字视频流及其相关操作提供了很大的
方便,灵活编写其中的回调函数可满足实时视频传输的需要,例如应用程序可直接
从缓冲中取得数字视频并对其进行压缩编码后实时地传到远端的计算机。笔者所从
事的电话网上的可视电话系统就是采用AVICap进行视频捕获的,这种方法同样可用
于其它多媒体会议系统中,如ISDN、局域网上的会议系统等。(
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
http://blog.sina.com.cn/s/blog_51396f890102fabr.html
在图像处理时经常会用到从摄像头读取图像。OPENCV有提供的方法来实现,非常简单,不用多说。而使用VC++则没有那么容易,下面介绍使用CImage和DirectShow读取摄像头图像,并显示的对话框中。
我用的开发工具是VS2010。
源代码下载,使用VS2010编译通过。 http://download.csdn.net/detail/sdlypyzq/4087013
一、创建一个MFC对话框程序,工程起名为CameraVCTest。
二、删除无用的控件和按钮。添加一个图片控件,ID为IDC_PICTURE,并为其添加CStatic 类型变量m_picture。添加四个按钮,名称分别为预览,拍照,保存,关闭。ID分别为IDC_VIEW,IDC_TAKEPHOTO,IDC_SAVE,IDC_CLOSE。分别为其添加变量m_view,m_takePhoto,m_save,m_close。双击四个按钮,生成四个响应函数。
三、将DirectShow文件夹和CameraDS类的头文件和源文件拷贝到项目源文件夹下。并在项目属性VC++ Directories添加include Directories,“.\DirectShow”。在Solution Exlporer添加上CameraDS类的头文件和源文件。
四、在CCameraVCTestDlg 类的头文件中,添加
#include "CameraDS.h" #include using namespace std;
添加公共变量和方法
vector cameralist; CImage *m_pImage; void ShowPicture(CImage *img,CStatic *pic); voidSaveCard(CImage *img); int m_cam_count; bool isBreak; CString m_savePath; CString savePath; intm_saveIndex; int m_fileCount;
在源文件的初始化函数里添加:
m_cam_count = CCameraDS::CameraCount(); cameralist.resize(m_cam_count); if(! cameralist[m_cam_count-1].OpenCamera(m_cam_count-1, false, 640,480)) { return FALSE; } this->isBreak = false; this->isSave = false; this->m_view.EnableWindow(TRUE); this->m_takePhoto.EnableWindow(FALSE); this->m_save.EnableWindow(FALSE);
添加如下函数
void DoEvents1() { MSG msg; if (::PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } void CCameraVCTestDlg::OnBnClickedView() {// TODO: Add your control notification handler code here this->isBreak = false; this->m_view.EnableWindow(FALSE); this->m_takePhoto.EnableWindow(TRUE); this->m_save.EnableWindow(FALSE); if(m_cam_count==0) { return ; }//CImage imgShow;//imgShow.Create(this->MAX_WIDTH,this->MAX_HEIGHT,24); while(1) { DoEvents1(); if(isBreak) {break; } this->m_pImage = cameralist[m_cam_count-1].QueryFrame2(); this->ShowPicture(m_pImage,&this->m_picture); //if(m_fileCount == 100) //{ // this->isBreak = true; //this->m_bnInit.EnableWindow(TRUE); // this->m_bnTake.EnableWindow(FALSE); // this->m_bnSave.EnableWindow(TRUE); // break; //} if(isSave) { if(this->m_fileCount<<span style="color: #800080;">100) { //this->m_ctrlProg.SetPos(m_fileCount); CString path; path.Format(L"%s\\%d.jpg",savePath,m_fileCount+m_saveIndex); this->m_pImage->Save(path); m_fileCount++; } else { isSave = false; this->m_view.EnableWindow(FALSE); this->m_takePhoto.EnableWindow(TRUE); //this->m_save.EnableWindow(TRUE); } } Sleep(150); } }
添加视频帧显示函数
void CCameraVCTestDlg::ShowPicture(CImage *img,CStatic *pic) { if(img==NULL) { return ; } intwidth = img->GetWidth(); int height = img->GetHeight(); CRect picRect; this->m_picture.GetClientRect(&picRect); CRect rt(picRect); CDC* dc = this->m_picture.GetDC(); CBrush*pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)); //dc->FillRect(rt, pBrush);if(picRect.Height()*width > height*picRect.Width()) { CPoint p1(0,(picRect.Height()-(picRect.Width()*height/width))/2); CPoint p2(picRect.Width(),(picRect.Height() - p1.y)); rt.SetRect(p1,p2); } else { CPoint p1((picRect.Width()-(picRect.Height()*width/height))/2,0); CPoint p2(picRect.Width()-p1.x,picRect.Height()); rt.SetRect(p1,p2); } // } // this->ShowMouseCursor(CursorTag); ::SetStretchBltMode(dc->m_hDC,HALFTONE); img->Draw(dc->m_hDC, rt); }
添加关闭程序
void CCameraVCTestDlg::OnBnClickedClose() { this->isBreak = true; this->OnClose(); }
好了,现在对话框上,点击预览按钮,就可以打开摄像头了,若还需要什么功能自己添加吧,如有问题,可以一起交流。欢迎不吝赐教。
Camera.h
1 #ifndef POINTER_64
2
3
4 #if !defined(_MAC) && (defined(_M_MRX000) || defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_VER >= 1100) && !(defined(MIDL_PASS) || defined(RC_INVOKED))
5 #define POINTER_64 __ptr64
6 typedef unsigned __int64 POINTER_64_INT;
7 #if defined(_WIN64)
8 #define POINTER_32 __ptr32
9 #else
10 #define POINTER_32
11 #endif
12 #else
13 #if defined(_MAC) && defined(_MAC_INT_64)
14 #define POINTER_64 __ptr64
15 typedef unsigned __int64 POINTER_64_INT;
16 #else
17 #if (_MSC_VER >= 1300) && !(defined(MIDL_PASS) || defined(RC_INVOKED))
18 #define POINTER_64 __ptr64
19 #else
20 #define POINTER_64
21 #endif
22 typedef unsigned long POINTER_64_INT;
23 #endif
24 #define POINTER_32
25 #endif
26
27 #endif
28
29
30
31
32
33 #ifndef CCAMERA_H
34 #define CCAMERA_H
35
36 #define WIN32_LEAN_AND_MEAN
37
38 #include <</span>atlbase.h>
39 #include "qedit.h"
40 #include "dshow.h"
41 #include <</span>windows.h>
42 //#include "cv.h"
43
44 //#include
45
46 #define MYFREEMEDIATYPE(mt) {if ((mt).cbFormat != 0) \
47 {CoTaskMemFree((PVOID)(mt).pbFormat); \
48 (mt).cbFormat =0; \
49 (mt).pbFormat = NULL; \
50 } \
51 if ((mt).pUnk != NULL) \
52 { \
53 (mt).pUnk->Release(); \
54 (mt).pUnk = NULL; \
55 }}
56
57
58 class CCameraDS
59 {
60 private:
61 // IplImage * m_pFrame;
62 CImage m_image;
63 bool m_bConnected;
64 int m_nWidth;
65 int m_nHeight;
66 bool m_bLock;
67 bool m_bChanged;
68 long m_nBufferSize;
69
70 CComPtr<</span>IGraphBuilder> m_pGraph;
71 CComPtr<</span>IBaseFilter> m_pDeviceFilter;
72 CComPtr<</span>IMediaControl> m_pMediaControl; 73 CComPtr<</span>IBaseFilter> m_pSampleGrabberFilter; 74 CComPtr<</span>ISampleGrabber> m_pSampleGrabber; 75 CComPtr<</span>IPin> m_pGrabberInput; 76 CComPtr<</span>IPin> m_pGrabberOutput; 77 CComPtr<</span>IPin> m_pCameraOutput; 78 CComPtr<</span>IMediaEvent> m_pMediaEvent; 79 CComPtr<</span>IBaseFilter> m_pNullFilter; 80 CComPtr<</span>IPin> m_pNullInputPin; 81 82 private: 83 bool BindFilter(int nCamIDX, IBaseFilter **pFilter); 84 void SetCrossBar(); 85 86 public: 87 CCameraDS(); 88 virtual~CCameraDS(); 89 90 //打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,... 91 //bDisplayProperties指示是否自动弹出摄像头属性页 92 //nWidth和nHeight设置的摄像头的宽和高,如果摄像头不支持所设定的宽度和高度,则返回false 93 bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties=true, int nWidth =320, intnHeight =240); 94 95 //关闭摄像头,析构函数会自动调用这个函数 96 void CloseCamera(); 97 98 //返回摄像头的数目 99 //可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。 100 staticint CameraCount(); 101 102 //根据摄像头的编号返回摄像头的名字 103 //nCamID: 摄像头编号 104 //sName: 用于存放摄像头名字的数组 105 //nBufferSize: sName的大小 106 //可以不用创建CCameraDS实例,采用CCameraDS::CameraName();得到结果。 107 staticint CCameraDS::CameraName(int nCamID, char* sName, int nBufferSize); 108 109 //返回图像宽度 110 int GetWidth(){return m_nWidth;} 111 112 //返回图像高度 113 int GetHeight(){return m_nHeight;} 114 115 //抓取一帧,返回的IplImage不可手动释放! 116 //返回图像数据的为RGB模式的Top-down(第一个字节为左上角像素),即IplImage::origin=0(IPL_ORIGIN_TL) 117 // IplImage * QueryFrame(); 118 CImage* QueryFrame2(); 119 }; 120 121 #endif
camera.cpp
#include "stdafx.h"
#include "CameraDS.h"
#pragma comment(lib,"Strmiids.lib")
//
// Construction/Destruction
//
CCameraDS::CCameraDS()
{
m_bConnected =false;
m_nWidth =0;
m_nHeight =0;
m_bLock =false;
m_bChanged =false;
// m_pFrame = NULL;
m_nBufferSize =0;
m_pNullFilter = NULL;
m_pMediaEvent = NULL;
m_pSampleGrabberFilter = NULL;
m_pGraph = NULL;
CoInitialize(NULL);
}
CCameraDS::~CCameraDS()
{
CloseCamera();
CoUninitialize();
}
void CCameraDS::CloseCamera()
{
if(m_bConnected)
m_pMediaControl->Stop();
m_pGraph = NULL;
m_pDeviceFilter = NULL;
m_pMediaControl = NULL;
m_pSampleGrabberFilter = NULL;
m_pSampleGrabber = NULL;
m_pGrabberInput = NULL;
m_pGrabberOutput = NULL;
m_pCameraOutput = NULL;
m_pMediaEvent = NULL;
m_pNullFilter = NULL;
m_pNullInputPin = NULL;
//if (m_pFrame)
// cvReleaseImage(&m_pFrame);
if(!this->m_image.IsNull())
{
this->m_image.Destroy();
}
m_bConnected =false;
m_nWidth =0;
m_nHeight =0;
m_bLock =false;
m_bChanged =false;
m_nBufferSize =0;
}
bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties, int nWidth, int nHeight)
{
HRESULT hr = S_OK;
CoInitialize(NULL);
// Create the Filter Graph Manager.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IGraphBuilder, (void**)&m_pGraph);
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (LPVOID *)&m_pSampleGrabberFilter);
hr = m_pGraph->QueryInterface(IID_IMediaControl, (void**) &m_pMediaControl);
hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void**) &m_pMediaEvent);
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (LPVOID*) &m_pNullFilter);
hr = m_pGraph->AddFilter(m_pNullFilter, L"NullRenderer");
hr = m_pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (void**)&m_pSampleGrabber);
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
mt.formattype = FORMAT_VideoInfo;
hr = m_pSampleGrabber->SetMediaType(&mt);
MYFREEMEDIATYPE(mt);
m_pGraph->AddFilter(m_pSampleGrabberFilter, L"Grabber");
// Bind Device Filter. We know the device because the id was passed in
BindFilter(nCamID, &m_pDeviceFilter);
m_pGraph->AddFilter(m_pDeviceFilter, NULL);
CComPtr<</span>IEnumPins> pEnum;
m_pDeviceFilter->EnumPins(&pEnum);
hr = pEnum->Reset();
hr = pEnum->Next(1, &m_pCameraOutput, NULL);
pEnum = NULL;
m_pSampleGrabberFilter->EnumPins(&pEnum);
pEnum->Reset();
hr = pEnum->Next(1, &m_pGrabberInput, NULL);
pEnum = NULL;
m_pSampleGrabberFilter->EnumPins(&pEnum);
pEnum->Reset();
pEnum->Skip(1);
hr = pEnum->Next(1, &m_pGrabberOutput, NULL);
pEnum = NULL;
m_pNullFilter->EnumPins(&pEnum);
pEnum->Reset();
hr = pEnum->Next(1, &m_pNullInputPin, NULL);
//SetCrossBar();
if (bDisplayProperties)
{
CComPtr<</span>ISpecifyPropertyPages> pPages;
HRESULT hr = m_pCameraOutput->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pPages);
if (SUCCEEDED(hr))
{
PIN_INFO PinInfo;
m_pCameraOutput->QueryPinInfo(&PinInfo);
CAUUID caGUID;
pPages->GetPages(&caGUID);
OleCreatePropertyFrame(NULL, 0, 0,
L"Property Sheet", 1,
(IUnknown **)&(m_pCameraOutput.p),
caGUID.cElems,
caGUID.pElems,
0, 0, NULL);
CoTaskMemFree(caGUID.pElems);
PinInfo.pFilter->Release();
}
pPages = NULL;
}
else
{
//
// 加入由 lWidth和lHeight设置的摄像头的宽和高 的功能,默认320*240
// by flymanbox @2009-01-24
//
int _Width = nWidth, _Height = nHeight;
IAMStreamConfig* iconfig;
iconfig = NULL;
hr = m_pCameraOutput->QueryInterface(IID_IAMStreamConfig, (void**)&iconfig);
AM_MEDIA_TYPE* pmt;
if(iconfig->GetFormat(&pmt) !=S_OK)
{
//printf("GetFormat Failed ! \n");
returnfalse;
}
VIDEOINFOHEADER* phead;
if ( pmt->formattype == FORMAT_VideoInfo)
{
phead=( VIDEOINFOHEADER*)pmt->pbFormat;
phead->bmiHeader.biWidth = _Width;
phead->bmiHeader.biHeight = _Height;
if(( hr=iconfig->SetFormat(pmt)) != S_OK )
{
returnfalse;
}
}
iconfig->Release();
iconfig=NULL;
MYFREEMEDIATYPE(*pmt);
}
hr = m_pGraph->Connect(m_pCameraOutput, m_pGrabberInput);
hr = m_pGraph->Connect(m_pGrabberOutput, m_pNullInputPin);
if (FAILED(hr))
{
switch(hr)
{
case VFW_S_NOPREVIEWPIN :
break;
case E_FAIL :
break;
case E_INVALIDARG :
break;
case E_POINTER :
break;
}
}
m_pSampleGrabber->SetBufferSamples(TRUE);
m_pSampleGrabber->SetOneShot(TRUE);
hr = m_pSampleGrabber->GetConnectedMediaType(&mt);
if(FAILED(hr))
returnfalse;
VIDEOINFOHEADER *videoHeader;
videoHeader = reinterpret_cast<</span>VIDEOINFOHEADER*>(mt.pbFormat);
m_nWidth = videoHeader->bmiHeader.biWidth;
m_nHeight = videoHeader->bmiHeader.biHeight;
m_bConnected =true;
pEnum = NULL;
returntrue;
}
bool CCameraDS::BindFilter(int nCamID, IBaseFilter **pFilter)
{
if (nCamID <</span>0)
returnfalse;
// enumerate all video capture devices
CComPtr<</span>ICreateDevEnum> pCreateDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void**)&pCreateDevEnum);
if (hr != NOERROR)
{
returnfalse;
}
CComPtr<</span>IEnumMoniker> pEm;
hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&pEm, 0);
if (hr != NOERROR)
{
returnfalse;
}
pEm->Reset();
ULONG cFetched;
IMoniker *pM;
int index =0;
while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK, index <= nCamID)
{
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)
{
if (index == nCamID)
{
pM->BindToObject(0, 0, IID_IBaseFilter, (void**)pFilter);
}
SysFreeString(var.bstrVal);
}
pBag->Release();
}
pM->Release();
index++;
}
pCreateDevEnum = NULL;
returntrue;
}
//将输入crossbar变成PhysConn_Video_Composite
void CCameraDS::SetCrossBar()
{
int i;
IAMCrossbar *pXBar1 = NULL;
ICaptureGraphBuilder2 *pBuilder = NULL;
HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,
CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2,
(void**)&pBuilder);
if (SUCCEEDED(hr))
{
hr = pBuilder->SetFiltergraph(m_pGraph);
}
hr = pBuilder->FindInterface(&LOOK_UPSTREAM_ONLY, NULL,
m_pDeviceFilter,IID_IAMCrossbar, (void**)&pXBar1);
if (SUCCEEDED(hr))
{
long OutputPinCount;
long InputPinCount;
long PinIndexRelated;
long PhysicalType;
long inPort =0;
long outPort =0;
pXBar1->get_PinCounts(&OutputPinCount,&InputPinCount);
for( i =0;i<</span>InputPinCount;i++)
{
pXBar1->get_CrossbarPinInfo(TRUE,i,&PinIndexRelated,&PhysicalType);
if(PhysConn_Video_Composite==PhysicalType)
{
inPort = i;
break;
}
}
for( i =0;i<</span>OutputPinCount;i++) { pXBar1->get_CrossbarPinInfo(FALSE,i,&PinIndexRelated,&PhysicalType); if(PhysConn_Video_VideoDecoder==PhysicalType) { outPort = i; break; } } if(S_OK==pXBar1->CanRoute(outPort,inPort)) { pXBar1->Route(outPort,inPort); } pXBar1->Release(); } pBuilder->Release(); } //IplImage* CCameraDS::QueryFrame() //{ // // long evCode; // long size = 0; // // m_pMediaControl->Run(); // m_pMediaEvent->WaitForCompletion(INFINITE, &evCode); // // m_pSampleGrabber->GetCurrentBuffer(&size, NULL); // ////if the buffer size changed // if (size != m_nBufferSize) // { // if (m_pFrame) // cvReleaseImage(&m_pFrame); // // m_nBufferSize = size; // m_pFrame = cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 3); // } // if (m_pFrame == NULL) return NULL; // m_pSampleGrabber->GetCurrentBuffer(&m_nBufferSize, (long*)m_pFrame->imageData); // cvFlip(m_pFrame); // // return m_pFrame; //} CImage* CCameraDS::QueryFrame2() { long evCode; long size =0; m_pMediaControl->Run(); m_pMediaEvent->WaitForCompletion(INFINITE, &evCode); m_pSampleGrabber->GetCurrentBuffer(&size, NULL); //if the buffer size changed if (size != m_nBufferSize) { //if (m_pImage != NULL )//|| !m_pImage->IsNull()) //{ // m_pImage->Destroy(); //} // cvReleaseImage(&m_pFrame); m_nBufferSize = size; //m_pFrame = cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 3); m_image.Create(m_nWidth,m_nHeight,24); } //if (m_pFrame == NULL) return NULL; if(m_image.IsNull()) { return0; } byte*q; byte*p =newbyte[m_nWidth*m_nHeight*3]; //m_pSampleGrabber->GetCurrentBuffer(&m_nBufferSize, (long*)m_pFrame->imageData); m_pSampleGrabber->GetCurrentBuffer(&m_nBufferSize, (long*)p); // cvFlip(m_pFrame); // for(int y=0, z=m_nHeight-1; y<</span>m_nHeight,z>=0; y++,z--) { q = (byte*)m_image.GetPixelAddress(0,z); memcpy(q,&p[m_nWidth*3*y],m_nWidth*3); } delete []p; return&m_image; } int CCameraDS::CameraCount() { int count =0; CoInitialize(NULL); // enumerate all video capture devices CComPtr<</span>ICreateDevEnum> pCreateDevEnum; HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum); CComPtr<</span>IEnumMoniker> pEm; hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0); if (hr != NOERROR) { return count; } pEm->Reset(); ULONG cFetched; IMoniker *pM; while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK) { count++; } pCreateDevEnum = NULL; pEm = NULL; return count; } int CCameraDS::CameraName(int nCamID, char* sName, int nBufferSize) { int count =0; CoInitialize(NULL); // enumerate all video capture devices CComPtr<</span>ICreateDevEnum> pCreateDevEnum; HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pCreateDevEnum); CComPtr<</span>IEnumMoniker> pEm; hr = pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEm, 0); if (hr != NOERROR) return0; pEm->Reset(); ULONG cFetched; IMoniker *pM; while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK) { if (count == nCamID) { IPropertyBag *pBag=0; 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) { //获取设备名称 WideCharToMultiByte(CP_ACP,0,var.bstrVal,-1,sName, nBufferSize ,"",NULL); SysFreeString(var.bstrVal); } pBag->Release(); } pM->Release(); break; } count++; } pCreateDevEnum = NULL; pEm = NULL; return1; }
|