Use the DirectShow drive camera

zhaozj2021-02-08  207

There are several ways to drive the small camera. By using DirectShow, it is better to drive the camera flexibility. It has a relatively complex but more efficient method. This article only introduces the simple method, I hope to communicate with you!

Use DirectShow to use the camera, which generally requires the camera's drive to be WDM format, of course, some older drive format DirectShow can also support. DirectSHOW is connected to the driver of the upper application and the lower layer through the Filter Graph Manager. DirectSHOW supports capture of the camera through a Capture Filter, and a capture filter has multiple sockets (PREVIEW) sockets can be used to display an image.

DirectShow controls the entire process of video capture through several COM interfaces, where IGraphBuilder is used to establish a filter, and IcapturegraphBuilder2 is used to establish contacts with the next driver, iVideoWindow, ImediaControl, IMEDIAEVENTEX respectively on the entire process video window, playback process Control with event response,

Below is routine:

CCOMQIPTR M_PVW;

CCOMQIPTR M_PMC;

CCOMQIPTR m_pme;

CCIMPTR M_PGRAPH;

CComptr m_pcapture;

DWORD M_DWGRAPHREGISTER;

Bool Binit (HWND HWND)

{

HRESULT HR;

// Get the interface

HR = CoCreateInstance (CLSID_FILTERGRAPH, NULL, CLSCTX_INPROC,

IID_IGRAPHBUILDER, (void **) & m_pgraph);

IF (Failed (HR))

Return False;

HR = COCREATEINSTANCE (CLSID_CAPTUREGRAPHBUILDER2, NULL, CLSCTX_INPROC,

IID_ICAPTUREGRAPHBUILDER2, (void **) & m_pcapture;

IF (Failed (HR))

Return False;

m_pmc = m_pgraph;

m_pvw = m_pgraph;

M_PME = m_pgraph;

// get a message

m_pme-> setNotifyWindow (m_hwnd = hwnd), WM_GraphNotify, 0);

// Connect the filter and capture

m_pcapture-> setfiltergraph (M_PGRAPH);

// Device join

// Enumerate the equipment

CComptr PCDE = NULL;

CComptr PEM = NULL;

HR = CocreateInstance (CLSID_SYSTEMDEVICEENUM, NULL, CLSCTX_INPROC,

IID_ICREATEDEVENUM, (VOID **) & PCDE);

IF (Failed (HR))

Return False;

PCDE-> CreateClassenumerator (CLSID_VideoInputDeviceCategory "

, & PEM, 0);

IF (PEM == Null)

Return False;

CComptr PM = NULL;

Ulong cfetched;

CComptr PBF = NULL;

IF (PEM-> Next (1, & PM, & CFETCHED) == S_OK)

{

PM-> BindToObject (0, 0, IID_IBASEFILTER, (Void **) & PBF);

PM.Release ();

}

Else

{

Return False;

}

// Add the device to the graph

HR = m_pgraph-> addfilter (PBF, L "Video Capture);

IF (Failed (HR))

Return False;

/ / Connect a source socket

HR = m_pcapture-> renderstream (& pin_category_preview, & mediatype_video

, PBF, NULL, NULL;

IF (Failed (HR))

Return False;

Pbf.release ();

// Set the video window

// Set a sub-window for the video window as the main window

HR = m_pvw-> put_owner (oahwnd) hWnd;

IF (Failed (HR))

Return False;

// Set the window style

M_PVW-> PUT_WINDOWSTYLE (WS_CHILD | WS_CLIPCHILDREN);

IF (Failed (HR))

Return False;

// Set the window size

CRECT RECLIENT;

GetClientRect (hwnd, recclient);

M_Pvw-> setWindowPosition (0, 0, 320, 240);

/ / Set visual

HR = m_pvw-> put_visible (oatrue);

IF (Failed (HR))

Return False;

// Add object to the list of running objects

CComptr prot;

GetrunningObjectTable (0, & prot);

Wchar C [128];

WSPRINTFW (C, L "Filtergraph% 08X PID% 08X / 0", (dword_ptr) m_pgraph.p,

GetCurrentProcessId ());

HR = CREATEITEMMONIKER (L "!", C, & PM);

IF (Failed (HR))

Return False;

HR = prot-> register (Rotflags_RegistrationKeepsalive, M_PGRAPH,

PM, & m_dwgraphregister;

PM.Release ();

Return False;

}

转载请注明原文地址:https://www.9cbs.com/read-2920.html

New Post(0)