First of all, my system is win7. It is said that win7 comes with DirectX, so you do not need to download DirectX again.
Add in header file# Pragma comment (Lib, "strmiids") <br/> # pragma comment (Lib, "quartz") <br/> # include <dshow. h> /// all required DirectShow applications <br/>
First, find the video device list
Void ccameradlg: getallcapdevices () <br/>{< br/> uint uindex = 0; <br/> // release the video device list <br/> for (INT I = 0; I <numelms (m_rgpmvideomenu); I ++) {<br/> imonrelease (m_rgpmvideomenu [I]); <br/>}</P> <p> // enumerate all video capturing devices <br/> icreatedevenum * pcreatedevenum = 0; <br/> hresult hR = cocreateinstance (clsid_systemdeviceenum, null, clsctx_inproc_server, <br/> iid_icreatedevenum, (void **) & pcreatedevenum ); // create an enumeration for all system devices <br/> If (HR! = Noerror) <br/>{< br/> error (text ("error creatint device enumerator"); <br/> return; <br/>}< br/> ienummoniker * PEM = 0; <br/> hR = pcreatedevenum-> createclassenumerator (clsid_videoinputdevicecategory, & PEM, 0 ); // filter out the video input device <br/> If (HR! = Noerror) <br/>{< br/> error (text ("Sorry, you have no video capture hardware. /R/n/R/N "); <br/>}< br/> PEM-> Reset (); <br/> ulong cfetched; <br/> imoniker * PM; </P> <p> while (hR = PEM-> next (1, & PM, & cfetched) = s_ OK) // loop until the last device <br/>{< br/> ipropertybag * pbag = 0; <br/> hR = PM-> bindtostorage (0, 0, iid_ipropertybag, (void **) & pbag); // bind the object to the corresponding property object <br/> If (succeeded (HR )) <br/>{< br/> variant var; <br/> var. vt = vt_bstr; <br/> hR = pbag-> Read (L "friendlyname", & var, null); <br/> If (hR = noerror) <br/>{< br/> cstring strdevicename = var. bstrval; <br/> // cstring strmenuitem; <br/> // strmenuitem. format ("% d) % s", (uindex + 1), strdevicename); <br/> sysfreestring (var. bstrval); <br/> m_rgpmvideomenu [uindex] = PM; <br/> PM-> addref (); <br/>}< br/> pbag-> release (); <br/>}< br/> PM-> release (); <br/> uindex ++; <br/>}< br/> PEM-> release (); <br/>}< br/> void ccameradlg: imonrelease (imoniker * & pm) <br/>{< br/> If (PM) <br/>{< br/> PM-> release (); <br/> PM = 0; <br/>}< br/>
The error function is:
Void ccameradlg: Error (tchar * ptext) <br/>{< br/>: MessageBox (null, ptext, text ("error! "), Mb_ OK | mb_taskmodal | mb_setforeground); <br/>}
Connect and display the video:
Hresult ccameradlg: initandstartpreview () <br/> {<br/> If (numelms (m_rgpmvideomenu)> 0) <br/> {<br/> hresult hR = m_rgpmvideomenu [0]-> bindtoobject (0, 0, iid_ibasefilter, (void **) & m_pvcap ); // bind the selected device to the Source Filter <br/> If (! M_pvcap) <br/>{< br/> error (text ("there is no USB/web camera for video capture! Please check! "); <Br/> return e_fail; <br/>}< br/> hR = m_pcapturegraph.cocreateinstance (clsid_filtergraph); <br/> If (! M_pcapturegraph) <br/>{< br/> error (text ("cocould not create filter graph"); <br/> return e_fail; <br/>}< br/> hR = m_pcapturegraph-> addfilter (m_pvcap, l "videopreview"); <br/> If (failed (HR )) <br/>{< br/> error (text ("cocould not put capture device in graph"); <br/> return e_fail; <br/>}< br/> hR = m_pbuilder.cocreateinstance (clsid_capturegraphbuilder2); <br/> If (! M_pbuilder) <br/>{< br/> error (text ("cocould not create capture graph builder2"); <br/> return hr; <br/>}</P> <p> hR = m_pbuilder-> setfiltergraph (m_pcapturegraph); // <br/> If (HR! = Noerror) <br/>{< br/> error (text ("cannot give graph to builder"); <br/> return e_fail; <br/>}< br/> // connect the filters of the captured video image <br/> hR = m_pbuilder-> renderstream (& pin_category_preview, & mediatype_interleaved, m_pvcap, null, null); <br/> If (failed (HR) <br/>{< br/> hR = m_pbuilder-> renderstream (& pin_category_preview, & mediatype_video, m_pvcap, null, null); <br/> If (failed (HR) <br/>{< br/> hR = m_pbuilder-> renderstream (& Pi N_category_capture, & mediatype_video, m_pvcap, null, null ); <br/>}< br/> // find the video window and stuff it in our window <br/> ccomqiptr <ivideowindow, & iid_ivideowindow> pwindow = m_pcapturegraph; // videowindow interface <br/> If (! Pwindow) <br/>{< br/> error (text ("cocould not get video window interface"); <br/> return e_fail; <br/>}< br/> // set up the preview window to be in our dialog instead of floating popup <br/> hwnd hwndpreview = NULL; <br/> getdlgitem (idc_preview_avi, & hwndpreview); // obtain the hwnd of the preview window control <br/> rect RC; <br/>: getwindowrect (hwndpreview, & rc); <br/> hR = pwindow-> put_owner (oahwnd) hwndpreview); <br/> hR = pwindow-> put_left (0 ); <br/> hR = pwindow-> put_top (0); <br/> hR = pwindow-> put_width (RC. right-rc.left); <br/> hR = pwindow-> put_height (RC. bottom-rc.top); <br/> hR = pwindow-> put_windowstyle (ws_child | ws_clipsiblings); <br/> hR = pwindow-> put_visible (oatrue ); </P> <p> // run the filter graph. The imediacontrol interface provides the flow of data in the filer graph. <br/> ccomqiptr <imediacontrol, & iid_imediacontrol> pcontrol = m_pcapturegraph; <br/> hR = pcontrol-> Run (); </P> <p> If (failed (HR )) <br/>{< br/> error (text ("cocould not run graph"); <br/> return hr; <br/>}</P> <p>}
It is best to add it to the ccameradlg: oninitdialog () function.
Coinitializeex (null, coinit_apartmentthreaded); // initialize the com environment <br/> zeromemory (m_rgpmvideomenu, sizeof (m_rgpmvideomenu); <br/> getallcapdevices (); <br/> initandstartpreview ();
In this way, you can easily display the data.