码迷,mamicode.com
首页 > Windows程序 > 详细

WPF中使用DirectShowLib枚举摄像头设备和分辨率

时间:2015-10-21 13:59:51      阅读:1679      评论:0      收藏:0      [点我收藏+]

标签:

 


提供window平台下基于Net技术和Qt技术的多点触摸设备应用开发,画板开发,摄像头/展台设备应用开发 


 

本质还是对DX接口的运用,直接代码好理解

1. 定义设备接口

public interface IDevice{
    string DeviceName{get;set;}  //设备名称
    string DevicePath{get;set;}  //设备路径
    System.Runtime.InteropServices.ComTypes.IMoniker Moniker{get;set;}
}

2.展台设备接口

public class CameraDevice : IDevice{
    // - interface IDevice - 
    public string DeviceName{get;set;}
    public string DevicePath{ get; set; }
    public System.Runtime.InteropServices.ComTypes.IMoniker Moniker{get;set;}
    // end IDevice

    //分辨率结构体
    public struct Resolution{
        public double Width{set;set;}
        public double Height{get;set;}
    }

    public CameraDevice(){ 
        this.DevicePath = string.Empty;
        this.DeviceName = string.Empty;
    }
}

3. 服务类,实现利用DirectShowLib获取设备的分辨率,本质还是DX接口的运用 

技术分享
public class DeviceService 
{
    // 获取系统内安装的视频输入设备~ 
    public System.Collections.Generic.IList<IDevice> GetDevices()
    {
        DsDevice[] devicesOfCat = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
        System.Collections.Generic.IList<IDeviceInfo> list = new System.Collections.Generic.List<IDeviceInfo>();
        DsDevice[] array = devicesOfCat;

        for( int i  = 0 ; i < array.length ; i++ ){
            DsDevice dsdevice = array[i];
            list.Add({new CameraDevice{
                DeviceName = dsdevice.Name,
                DevicePath = dsdevice.DevicePath,
                Moniker  = dsdevice.Mon 
            }});
        }

        return list;
    }

    public System.Collections.Generic.IList<CameraDevice.Resolution> GetResolutions(string devicePath)
    {
        //获取设备
        System.Collections.Generic.IList<IDevice> devices= this.GetDevices();
        foreach( IDevice current in devices)
        {
            //获取当前指定设备路径的关联分辨率
            if( current.DevicePath == devicePath ){
                return this.GetResolutions(current);
            }
        }

        return new System.Collections.Generic.List<CameraDevice.Resolution>();
    }

 private System.Collections.Generic.IList<CameraDevice.Resolution> GetResolutions(IDevice deivce)
 {
     System.Collections.Generic.List<CameraDevice.Resolution> list = new System.Collections.Generic.List<CameraDevice.Resolution>();
     IBaseFilter vSource = null;
    IFilterGraph2 filterGraph = new FilterGraph() as IFilterGraph2;
    if (filterGraph == null)
     {
          return list;
     }
     filterGraph.AddSourceFilterForMoniker(deivce.Moniker, null, deivce.DeviceName, out vSource);
     IPin pin = DsFindPin.ByCategory(vSource, PinCategory.Capture, 0);
     VideoInfoHeader videoInfoHeader = new VideoInfoHeader();
     IEnumMediaTypes enumMediaTypes;
     pin.EnumMediaTypes(out enumMediaTypes);
     AMMediaType[] array = new AMMediaType[1];
     System.IntPtr zero = System.IntPtr.Zero;
     enumMediaTypes.Next(1, array, zero);
     int num = 0;
     int num2 = 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(new CameraDevice.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 (from d in list.Distinct<CameraDevice.Resolution>()
                    orderby d.Width
                    select d).ToList<CameraDevice.Resolution>();                       
  }
}
View Code

4. 测试:新建WPF工程,一个按钮。 click事件

技术分享
private void _btnGetDevices_Click_1(object sender, RoutedEventArgs e)
 {
            DeviceService videoservice = new DeviceService();
            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>();
                string strmsg = string.Empty;
                strmsg += "device count:" + deviceinfos.Count.ToString() + "\n";
                strmsg += "device name:" + d.DeviceName + "\n";
                foreach (CameraDevice.Resolution cur in ls)
                {
                    strmsg += "RW:" + cur.Width + " ; RH: " + cur.Height + "\n";
                }

                MessageBox.Show(strmsg);
            }
}
View Code

 


 

WPF中使用DirectShowLib枚举摄像头设备和分辨率

标签:

原文地址:http://www.cnblogs.com/visonme/p/4897540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!