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

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

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

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

DirectShow VS2013 控制台下捕捉摄像头并且显示_第1张图片


接下来添加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);
}


你可能感兴趣的:(控制台,opencv,图像处理,摄像头,directshow)