DirectShow VS2013 控制台下捕捉摄像头并且显示

需要lib库文件 strmiids.lib,下载地址:http://download.csdn.net/detail/dopamy_busymonkey/8872687

放在解决方案中项目的根目录中直接使用(也可以放在VS的安装目录中的库文件夹中,但是为了方便之后查找,还是放在项目文件夹中)。

新建控制台项目,添加依赖项 strmiids.lib,在项目的解决方案资源管理器中,项目右键,属性中添加:



接下来添加cpp文件,源码如下:

#include "windows.h"  
#include "TCHAR.h"  
#include <dshow.h>

LRESULT CALLBACK WindowProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
	);


int _tmain(int argc, _TCHAR* argv[])
{
	IGraphBuilder *pGraph = NULL;
	ICaptureGraphBuilder2 *pBuilder = NULL;
	ICreateDevEnum *pSysDevEnum;
	IEnumMoniker *pEnumCat;
	IBaseFilter *pBaseFilter;
	IMoniker *pMoniker;
	IVideoWindow *pWindow;
	IMediaControl *pControl = NULL;
	IMediaEvent   *pEvent = NULL;
	HRESULT hr = CoInitialize(NULL);
	long pWidth;
	long pHeight;

	HINSTANCE hInstance;
	hInstance = GetModuleHandle(NULL);
	WNDCLASS Draw;
	Draw.cbClsExtra = 0;
	Draw.cbWndExtra = 0;
	Draw.hCursor = LoadCursor(hInstance, IDC_ARROW);;
	Draw.hIcon = LoadIcon(hInstance, IDI_APPLICATION);;
	Draw.lpszMenuName = NULL;
	Draw.style = CS_HREDRAW | CS_VREDRAW;
	Draw.hbrBackground = (HBRUSH)COLOR_WINDOW;
	Draw.lpfnWndProc = WindowProc;
	Draw.lpszClassName = _T("DDraw");
	Draw.hInstance = hInstance;


	RegisterClass(&Draw);

	HWND hwnd = CreateWindow(
		_T("DDraw"),           //上面注册的类名,要完全一致    
		L"绘制",  //窗口标题文字    
		WS_OVERLAPPEDWINDOW, //窗口外观样式    
		38,             //窗口相对于父级的X坐标    
		20,             //窗口相对于父级的Y坐标    
		480,                //窗口的宽度    
		250,                //窗口的高度    
		NULL,               //没有父窗口,为NULL    
		NULL,               //没有菜单,为NULL    
		hInstance,          //当前应用程序的实例句柄    
		NULL);              //没有附加数据,为NULL    

	// 显示窗口    
	ShowWindow(hwnd, SW_SHOW);

	// 更新窗口    
	UpdateWindow(hwnd);

	if (FAILED(hr))
	{
		printf("ERROR - Could not initialize COM library");
		return -1;
	}

	hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,
		CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuilder);

	if (SUCCEEDED(hr))
	{
		hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph);
		if (SUCCEEDED(hr))
		{
			hr = pBuilder->SetFiltergraph(pGraph);
		}
	}


	hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
		IID_ICreateDevEnum, reinterpret_cast<void **>(&pSysDevEnum));
	hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);


	if (pEnumCat->Next(1, &pMoniker, NULL) == S_OK)
	{
		hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pBaseFilter);
		if (SUCCEEDED(hr))
		{
			hr = pGraph->AddFilter(pBaseFilter, L"Capture Filter");
			hr = pBuilder->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pBaseFilter, NULL, NULL);
			hr = pGraph->QueryInterface(IID_IVideoWindow, (void**)&pWindow);
			hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
			pControl->Run();
			pWindow->put_Owner((OAHWND)hwnd);
			pWindow->put_WindowStyle(WS_CHILD);
			pWindow->get_Width(&pWidth);
			pWindow->get_Height(&pHeight);
			pWindow->SetWindowPosition(0, 0, pWidth, pHeight);
		}
	}


	// 消息循环    
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	pMoniker->Release();
	pControl->Release();
	pGraph->Release();
	CoUninitialize();
}

// 消息处理函数的实现  
LRESULT CALLBACK WindowProc(
	_In_  HWND hwnd,
	_In_  UINT uMsg,
	_In_  WPARAM wParam,
	_In_  LPARAM lParam
	)
{
	switch (uMsg)
	{
	case WM_DESTROY:
	{
					   PostQuitMessage(0);
					   return 0;
	}
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值