Using Directshowlib to enumerate camera devices and resolutions in WPF

Source: Internet
Author: User

Provide multi-touch device application development, artboard development, Camera/Kiosk Device Application development under window platform based on net technology and QT technology

The essence or the use of DX interface, direct code good understanding

1. Define the Device interface

 Public Interface idevice{    string devicename{get;  Set;}  // Device name    string devicepath{get;  Set;}  // Device Path    System.Runtime.InteropServices.ComTypes.IMoniker moniker{get;  Set;}}

2. Kiosk Device Interface

 Public classcameradevice:idevice{//-Interface IDevice-     Public stringdevicename{Get;Set;}  Public stringdevicepath{Get;Set; }  PublicSystem.Runtime.InteropServices.ComTypes.IMoniker moniker{Get;Set;} //End IDevice//resolution structure Body     Public structresolution{ Public Doublewidth{Set;Set;}  Public Doubleheight{Get;Set;} }     PublicCameradevice () { This. DevicePath =string.        Empty;  This. DeviceName =string.    Empty; }}

3. Service class, achieve the use of Directshowlib to obtain the resolution of the device, the essence or the use of DX interface

 Public classDeviceservice {//get video input devices installed in the system ~     PublicSystem.collections.generic.ilist<idevice>GetDevices () {dsdevice[] Devicesofcat=Dsdevice.getdevicesofcat (Filtercategory.videoinputdevice); System.Collections.Generic.IList<IDeviceInfo> list =NewSystem.collections.generic.list<ideviceinfo>(); Dsdevice[] Array=Devicesofcat;  for(inti =0; i < Array.Length; i++) {Dsdevice Dsdevice=Array[i]; List. ADD ({Newcameradevice{devicename=Dsdevice. Name, DevicePath=Dsdevice. DevicePath, moniker=Dsdevice.        Mon}}); }        returnlist; }     PublicSystem.collections.generic.ilist<cameradevice.resolution> Getresolutions (stringDevicePath) {        //Get DeviceSystem.collections.generic.ilist<idevice> devices= This.        GetDevices (); foreach(IDevice currentinchdevices) {            //gets the associated resolution of the currently specified device path            if(Current. DevicePath = =DevicePath) {                return  This.            Getresolutions (current); }        }        return NewSystem.collections.generic.list<cameradevice.resolution>(); } PrivateSystem.collections.generic.ilist<cameradevice.resolution>getresolutions (IDevice deivce) {System.Collections.Generic.List<CameraDevice.Resolution> list =NewSystem.collections.generic.list<cameradevice.resolution>(); Ibasefilter Vsource=NULL; IFILTERGRAPH2 Filtergraph=NewFiltergraph () asIFILTERGRAPH2; if(Filtergraph = =NULL)     {          returnlist; } filtergraph.addsourcefilterformoniker (Deivce. Moniker,NULL, Deivce. DeviceName, outVsource); Ipin Pin= Dsfindpin.bycategory (Vsource, Pincategory.capture,0); Videoinfoheader Videoinfoheader=NewVideoinfoheader ();     Ienummediatypes enummediatypes; Pin. Enummediatypes ( outenummediatypes); Ammediatype[] Array=Newammediatype[1]; System.IntPtr Zero=System.IntPtr.Zero; Enummediatypes.next (1, array, zero); intnum =0; intnum2 =0;  while(Array. Any<ammediatype> () && array[0] !=NULL) {System.Runtime.InteropServices.Marshal.PtrToStructure (array[0].formatptr, Videoinfoheader); if(VideoInfoHeader.BmiHeader.Size! =0&& VideoInfoHeader.BmiHeader.BitCount! =0)         {            if((int) VideoInfoHeader.BmiHeader.BitCount >num2) {list.                Clear (); Num=0; Num2= (int) VideoInfoHeader.BmiHeader.BitCount; } list. ADD (Newcameradevice.resolution {Width= (Double) VideoInfoHeader.BmiHeader.Width, Height= (Double) videoInfoHeader.BmiHeader.Height}); if(VideoInfoHeader.BmiHeader.Width > Num | | videoInfoHeader.BmiHeader.Height >num) {num=System.Math.Max (VideoInfoHeader.BmiHeader.Width, videoInfoHeader.BmiHeader.Height); }} enummediatypes.next (1, array, zero); }      return( fromDinchList. Distinct<cameradevice.resolution>()                     byD.widthSelectD). Tolist<cameradevice.resolution>(); }}
View Code

4. Test: Create a new WPF project, a button. Click event

Private void_btngetdevices_click_1 (Objectsender, RoutedEventArgs e) {Deviceservice Videoservice=NewDeviceservice (); IList<IDevice> Deviceinfos =Videoservice.            GetDevices (); if(Deviceinfos! =NULL&& Deviceinfos. Count >0) {IDevice d= deviceinfos[0]; List<CameraDevice.Resolution> ls = videoservice. Getresolutions (d). Tolist<cameradevice.resolution>(); stringstrmsg =string.                Empty; strmsg+="Device Count:"+ Deviceinfos. Count.tostring () +"\ n"; strmsg+="Device Name:"+ D.devicename +"\ n"; foreach(Cameradevice.resolution curinchls) {strmsg+="RW:"+ cur. Width +" ; RH:"+ cur. Height +"\ n";            } MessageBox.Show (strmsg); }}
View Code

Using Directshowlib to enumerate camera devices and resolutions in WPF

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.