Use DirectShow to drive the camera

Source: Internet
Author: User
There are several ways to drive a small camera. using DirectShow to drive the camera is more flexible, and there are simple and complex but more effective methods. This article only introduces simple methods, hope to communicate with you! When using DirectShow to use a camera, it is generally required that the camera driver be In the WDM format. Of course, some older driver formats, DirectShow, are also supported. DirectShow uses the filter graph manager to communicate with upper-layer applications and lower-layer drivers. DirectShow supports camera capturing through a kind of stuff called the capture filter. A capture filter has multiple pins, in which the Preview (PreView) the plug-in can be used to display the hidden image.

DirectShow uses several com interfaces to control the video capture process. igraphbuilder is used to create a filter. icapturegraphbuilder2 is used to establish a connection with the underlying driver, ivideowindow, imediacontrol, imediaeventex controls the video windows, playing processes, and event responses of the entire process, respectively,
The following is a routine:

Ccomqiptr <ivideowindow> m_pvw;
Ccomqiptr <imediacontrol> m_pmc;
Ccomqiptr <imediaeventex> m_pme;
Ccomptr <igraphbuilder> m_pgraph;
Ccomptr <icapturegraphbuilder2> m_pcapture;
DWORD m_dwgraphregister;

Bool binit (hwnd)
{
Hresult hr;
// Obtain 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 the message
M_pme-> setpolicywindow (oahwnd) (m_hwnd = hwnd), wm_graph1_y, 0 );
// Connect filtering and capturing
M_pcapture-> setfiltergraph (m_pgraph );
// Device connection
// Enumerate Devices
Ccomptr <icreatedevenum> pcde = NULL;
Ccomptr <ienummoniker> 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 <imoniker> PM = NULL;
Ulong cfetched;
Ccomptr <ibasefilter> 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 graph
HR = m_pgraph-> addfilter (PBF, l "video capture ");
If (failed (HR ))
Return false;
// Connect a source plug-in
HR = m_pcapture-> renderstream (& pin_category_preview, & mediatype_video
, PBF, null, null );
If (failed (HR ))
Return false;

PBF. Release ();
// Set the video window
// Sets a subwindow of the main video 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 rectclient;
Getclientrect (hwnd, rectclient );
M_pvw-> setwindowposition (0, 0,320,240 );
// Set the visualization
HR = m_pvw-> put_visible (oatrue );
If (failed (HR ))
Return false;
// Add the object to the running Object List
Ccomptr <irunningobjecttable> 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;
}

Author: lu_zi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.