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

时间:2022-07-15 19:26:31

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

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

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

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

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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