修改后的代码:github
一、调用windows自身摄像头属性设置窗口
使用VideoCaptureDevice对象的DisplayPropertyPage(IntPtr parentWindow)方法即可,以下是从Aforge源码里找到的调用api方式:
/// <summary> /// Invokes a new property frame, that is, a property sheet dialog box. /// </summary> /// /// <param name="hwndOwner">Parent window of property sheet dialog box.</param> /// <param name="x">Horizontal position for dialog box.</param> /// <param name="y">Vertical position for dialog box.</param> /// <param name="caption">Dialog box caption.</param> /// <param name="cObjects">Number of object pointers in <b>ppUnk</b>.</param> /// <param name="ppUnk">Pointer to the objects for property sheet.</param> /// <param name="cPages">Number of property pages in <b>lpPageClsID</b>.</param> /// <param name="lpPageClsID">Array of CLSIDs for each property page.</param> /// <param name="lcid">Locale identifier for property sheet locale.</param> /// <param name="dwReserved">Reserved.</param> /// <param name="lpvReserved">Reserved.</param> /// /// <returns>Returns <b>S_OK</b> on success.</returns> /// [DllImport( "oleaut32.dll" )] public static extern int OleCreatePropertyFrame( IntPtr hwndOwner, int x, int y, [MarshalAs( UnmanagedType.LPWStr )] string caption, int cObjects, [MarshalAs( UnmanagedType.Interface, ArraySubType = UnmanagedType.IUnknown )] ref object ppUnk, int cPages, IntPtr lpPageClsID, int lcid, int dwReserved, IntPtr lpvReserved );
二、通过代码自定义设置摄像头属性
aforge发布版只封装了对摄像头控制属性(缩放、焦点、曝光等)的设置方法,要想设置亮度、对比度这些属性,需要在源码上添加功能。
扩展代码原地址:https://code.google.com/archive/p/aforge/issues/357
有三个文件,都是Video.DirectShow项目下的:IAMVideoProcAmp.cs,VideoCaptureDevice.cs,VideoProcAmpProperty.cs
IAMVideoProcAmp.cs声明了几个调用com对象的方法,放在Internals文件夹下
// AForge Direct Show Library // AForge.NET framework // http://www.aforgenet.com/framework/ // // Copyright © AForge.NET, 2009-2013 // contacts@aforgenet.com // namespace AForge.Video.DirectShow.Internals { using System; using System.Runtime.InteropServices; /// <summary> /// The IAMVideoProcAmp interface controls camera settings such as brightness, contrast, hue, /// or saturation. To obtain this interface, query the filter that controls the camera. /// </summary> [ComImport, Guid("C6E13360-30AC-11D0-A18C-00A0C9118956"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IAMVideoProcAmp { /// <summary> /// Gets the range and default value of a specified camera property. /// </summary> /// /// <param name="Property">Specifies the property to query.</param> /// <param name="pMin">Receives the minimum value of the property.</param> /// <param name="pMax">Receives the maximum value of the property.</param> /// <param name="pSteppingDelta">Receives the step size for the property.</param> /// <param name="pDefault">Receives the default value of the property. </param> /// <param name="pCapsFlags">Receives a member of the VideoProcAmpFlags enumeration, indicating whether the property is controlled automatically or manually.</param> /// /// <returns>Return's <b>HRESULT</b> error code.</returns> /// [PreserveSig] int GetRange( [In] VideoProcAmpProperty Property, [Out] out int pMin, [Out] out int pMax, [Out] out int pSteppingDelta, [Out] out int pDefault, [Out] out VideoProcAmpFlags pCapsFlags ); /// <summary> /// Sets a specified property on the camera. /// </summary> /// /// <param name="Property">Specifies the property to set.</param> /// <param name="lValue">Specifies the new value of the property.</param> /// <param name="Flags">Specifies the desired control setting, as a member of the VideoProcAmpFlags enumeration.</param> /// /// <returns>Return's <b>HRESULT</b> error code.</returns> /// [PreserveSig] int Set( [In] VideoProcAmpProperty Property, [In] int lValue, [In] VideoProcAmpFlags Flags ); /// <summary> /// Gets the current setting of a camera property. /// </summary> /// /// <param name="Property">Specifies the property to retrieve.</param> /// <param name="lValue">Receives the value of the property.</param> /// <param name="Flags">Receives a member of the VideoProcAmpFlags enumeration. /// The returned value indicates whether the setting is controlled manually or automatically.</param> /// /// <returns>Return's <b>HRESULT</b> error code.</returns> /// [PreserveSig] int Get( [In] VideoProcAmpProperty Property, [Out] out int lValue, [Out] out VideoProcAmpFlags Flags ); } }
VideoCaptureDevice.cs添加了几个方法用来获取和设置参数,替换掉源文件即可,也可以在原文件加上这几个方法的代码
1 /// <summary> 2 /// Sets a specified property on the camera. 3 /// </summary> 4 /// 5 /// <param name="property">Specifies the property to set.</param> 6 /// <param name="value">Specifies the new value of the property.</param> 7 /// <param name="controlFlags">Specifies the desired control setting.</param> 8 /// 9 /// <returns>Returns true on success or false otherwise.</returns> 10 /// 11 /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> 12 /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> 13 /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> 14 /// 15 public bool SetVideoProperty(VideoProcAmpProperty property, int value, VideoProcAmpFlags controlFlags) 16 { 17 bool ret = true; 18 19 // check if source was set 20 if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))) 21 { 22 throw new ArgumentException("Video source is not specified."); 23 } 24 25 lock (sync) 26 { 27 object tempSourceObject = null; 28 29 // create source device's object 30 try 31 { 32 tempSourceObject = FilterInfo.CreateFilter(deviceMoniker); 33 } 34 catch 35 { 36 throw new ApplicationException("Failed creating device object for moniker."); 37 } 38 39 if (!(tempSourceObject is IAMVideoProcAmp)) 40 { 41 throw new NotSupportedException("The video source does not support camera control."); 42 } 43 44 IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject; 45 int hr = pCamControl.Set(property, value, controlFlags); 46 47 ret = (hr >= 0); 48 49 Marshal.ReleaseComObject(tempSourceObject); 50 } 51 52 return ret; 53 } 54 55 /// <summary> 56 /// Gets the current setting of a camera property. 57 /// </summary> 58 /// 59 /// <param name="property">Specifies the property to retrieve.</param> 60 /// <param name="value">Receives the value of the property.</param> 61 /// <param name="controlFlags">Receives the value indicating whether the setting is controlled manually or automatically</param> 62 /// 63 /// <returns>Returns true on success or false otherwise.</returns> 64 /// 65 /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> 66 /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> 67 /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> 68 /// 69 public bool GetVideoProperty(VideoProcAmpProperty property, out int value, out VideoProcAmpFlags controlFlags) 70 { 71 bool ret = true; 72 73 // check if source was set 74 if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))) 75 { 76 throw new ArgumentException("Video source is not specified."); 77 } 78 79 lock (sync) 80 { 81 object tempSourceObject = null; 82 83 // create source device's object 84 try 85 { 86 tempSourceObject = FilterInfo.CreateFilter(deviceMoniker); 87 } 88 catch 89 { 90 throw new ApplicationException("Failed creating device object for moniker."); 91 } 92 93 if (!(tempSourceObject is IAMVideoProcAmp)) 94 { 95 throw new NotSupportedException("The video source does not support camera control."); 96 } 97 98 IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject; 99 int hr = pCamControl.Get(property, out value, out controlFlags); 100 101 ret = (hr >= 0); 102 103 Marshal.ReleaseComObject(tempSourceObject); 104 } 105 106 return ret; 107 } 108 109 /// <summary> 110 /// Gets the range and default value of a specified camera property. 111 /// </summary> 112 /// 113 /// <param name="property">Specifies the property to query.</param> 114 /// <param name="minValue">Receives the minimum value of the property.</param> 115 /// <param name="maxValue">Receives the maximum value of the property.</param> 116 /// <param name="stepSize">Receives the step size for the property.</param> 117 /// <param name="defaultValue">Receives the default value of the property.</param> 118 /// <param name="controlFlags">Receives a member of the <see cref="CameraControlFlags"/> enumeration, indicating whether the property is controlled automatically or manually.</param> 119 /// 120 /// <returns>Returns true on success or false otherwise.</returns> 121 /// 122 /// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception> 123 /// <exception cref="ApplicationException">Failed creating device object for moniker.</exception> 124 /// <exception cref="NotSupportedException">The video source does not support camera control.</exception> 125 /// 126 public bool GetVideoPropertyRange(VideoProcAmpProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out VideoProcAmpFlags controlFlags) 127 { 128 bool ret = true; 129 130 // check if source was set 131 if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))) 132 { 133 throw new ArgumentException("Video source is not specified."); 134 } 135 136 lock (sync) 137 { 138 object tempSourceObject = null; 139 140 // create source device's object 141 try 142 { 143 tempSourceObject = FilterInfo.CreateFilter(deviceMoniker); 144 } 145 catch 146 { 147 throw new ApplicationException("Failed creating device object for moniker."); 148 } 149 150 if (!(tempSourceObject is IAMVideoProcAmp)) 151 { 152 throw new NotSupportedException("The video source does not support camera control."); 153 } 154 155 IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject; 156 int hr = pCamControl.GetRange(property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags); 157 158 ret = (hr >= 0); 159 160 Marshal.ReleaseComObject(tempSourceObject); 161 } 162 163 return ret; 164 }
VideoProcAmpProperty.cs枚举对象,放在VideoCaptureDevice.cs同目录下
1 // AForge Direct Show Library 2 // AForge.NET framework 3 // http://www.aforgenet.com/framework/ 4 // 5 // Copyright © AForge.NET, 2009-2013 6 // contacts@aforgenet.com 7 // 8 9 namespace AForge.Video.DirectShow 10 { 11 using System; 12 13 /// <summary> 14 /// The enumeration specifies a setting on a camera. 15 /// </summary> 16 public enum VideoProcAmpProperty 17 { 18 /// <summary> 19 /// Brightness control. 20 /// </summary> 21 Brightness = 0, 22 23 /// <summary> 24 /// Contrast control. 25 /// </summary> 26 Contrast, 27 28 /// <summary> 29 /// Hue control. 30 /// </summary> 31 Hue, 32 33 /// <summary> 34 /// Saturation control. 35 /// </summary> 36 Saturation, 37 38 /// <summary> 39 /// Sharpness control. 40 /// </summary> 41 Sharpness, 42 43 /// <summary> 44 /// Gamma control. 45 /// </summary> 46 Gamma, 47 48 /// <summary> 49 /// ColorEnable control. 50 /// </summary> 51 ColorEnable, 52 53 /// <summary> 54 /// WhiteBalance control. 55 /// </summary> 56 WhiteBalance, 57 58 /// <summary> 59 /// BacklightCompensation control. 60 /// </summary> 61 BacklightCompensation, 62 63 /// <summary> 64 /// Gain control. 65 /// </summary> 66 Gain 67 } 68 69 /// <summary> 70 /// The enumeration defines whether a camera setting is controlled manually or automatically. 71 /// </summary> 72 [Flags] 73 public enum VideoProcAmpFlags 74 { 75 /// <summary> 76 /// No control flag. 77 /// </summary> 78 None = 0x0, 79 80 /// <summary> 81 /// Auto control Flag. 82 /// </summary> 83 Auto = 0x0001, 84 85 /// <summary> 86 /// Manual control Flag. 87 /// </summary> 88 Manual = 0x0002 89 } 90 }
生成dll添加引用就可以了