C# Aforge设置摄像头视频属性和控制属性
程序员文章站
2023-12-03 15:31:46
一、调用windows自身摄像头属性设置窗口 使用VideoCaptureDevice对象的DisplayPropertyPage(IntPtr parentWindow)方法即可,以下是从Aforge源码里找到的调用api方式: 二、通过代码自定义设置摄像头属性 aforge发布版只封装了对摄像头 ......
一、调用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发布版只封装了对摄像头控制属性(缩放、焦点、曝光等)的设置方法,要想设置亮度、对比度这些属性,需要在源码上添加功能。
扩展代码原地址:
有三个文件,都是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添加引用就可以了