修改后的代码:github
一、调用windows自身摄像头属性设置窗口
使用VideoCaptureDevice对象的DisplayPropertyPage(IntPtr parentWindow)方法即可,以下是从Aforge源码里找到的调用api方式:
///
/// Invokes a new property frame, that is, a property sheet dialog box.
///
///
/// Parent window of property sheet dialog box.
/// Horizontal position for dialog box.
/// Vertical position for dialog box.
/// Dialog box caption.
/// Number of object pointers in ppUnk.
/// Pointer to the objects for property sheet.
/// Number of property pages in lpPageClsID.
/// Array of CLSIDs for each property page.
/// Locale identifier for property sheet locale.
/// Reserved.
/// Reserved.
///
/// Returns S_OK on success.
///
[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;
///
/// The IAMVideoProcAmp interface controls camera settings such as brightness, contrast, hue,
/// or saturation. To obtain this interface, query the filter that controls the camera.
///
[ComImport,
Guid("C6E13360-30AC-11D0-A18C-00A0C9118956"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IAMVideoProcAmp
{
///
/// Gets the range and default value of a specified camera property.
///
///
/// Specifies the property to query.
/// Receives the minimum value of the property.
/// Receives the maximum value of the property.
/// Receives the step size for the property.
/// Receives the default value of the property.
/// Receives a member of the VideoProcAmpFlags enumeration, indicating whether the property is controlled automatically or manually.
///
/// Return's HRESULT error code.
///
[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
);
///
/// Sets a specified property on the camera.
///
///
/// Specifies the property to set.
/// Specifies the new value of the property.
/// Specifies the desired control setting, as a member of the VideoProcAmpFlags enumeration.
///
/// Return's HRESULT error code.
///
[PreserveSig]
int Set(
[In] VideoProcAmpProperty Property,
[In] int lValue,
[In] VideoProcAmpFlags Flags
);
///
/// Gets the current setting of a camera property.
///
///
/// Specifies the property to retrieve.
/// Receives the value of the property.
/// Receives a member of the VideoProcAmpFlags enumeration.
/// The returned value indicates whether the setting is controlled manually or automatically.
///
/// Return's HRESULT error code.
///
[PreserveSig]
int Get(
[In] VideoProcAmpProperty Property,
[Out] out int lValue,
[Out] out VideoProcAmpFlags Flags
);
}
}
VideoCaptureDevice.cs添加了几个方法用来获取和设置参数,替换掉源文件即可,也可以在原文件加上这几个方法的代码
1 ///
2 ///Sets a specified property on the camera.3 ///
4 ///
5 /// Specifies the property to set.
6 /// Specifies the new value of the property.
7 /// Specifies the desired control setting.
8 ///
9 /// Returns true on success or false otherwise.
10 ///
11 /// Video source is not specified - device moniker is not set.
12 /// Failed creating device object for moniker.
13 /// The video source does not support camera control.
14 ///15 public bool SetVideoProperty(VideoProcAmpProperty property, intvalue, 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 isIAMVideoProcAmp))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 returnret;53 }54
55 ///
56 ///Gets the current setting of a camera property.57 ///
58 ///
59 /// Specifies the property to retrieve.
60 /// Receives the value of the property.
61 /// Receives the value indicating whether the setting is controlled manually or automatically
62 ///
63 /// Returns true on success or false otherwise.
64 ///
65 /// Video source is not specified - device moniker is not set.
66 /// Failed creating device object for moniker.
67 /// The video source does not support camera control.
68 ///69 public bool GetVideoProperty(VideoProcAmpProperty property, out int value, outVideoProcAmpFlags 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 isIAMVideoProcAmp))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, outcontrolFlags);100
101 ret = (hr >= 0);102
103 Marshal.ReleaseComObject(tempSourceObject);104 }105
106 returnret;107 }108
109 ///
110 ///Gets the range and default value of a specified camera property.111 ///
112 ///
113 /// Specifies the property to query.
114 /// Receives the minimum value of the property.
115 /// Receives the maximum value of the property.
116 /// Receives the step size for the property.
117 /// Receives the default value of the property.
118 /// Receives a member of theenumeration, indicating whether the property is controlled automatically or manually.
119 ///
120 /// Returns true on success or false otherwise.
121 ///
122 /// Video source is not specified - device moniker is not set.
123 /// Failed creating device object for moniker.
124 /// The video source does not support camera control.
125 ///126 public bool GetVideoPropertyRange(VideoProcAmpProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, outVideoProcAmpFlags 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 isIAMVideoProcAmp))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, outcontrolFlags);157
158 ret = (hr >= 0);159
160 Marshal.ReleaseComObject(tempSourceObject);161 }162
163 returnret;164 }
VideoProcAmpProperty.cs枚举对象,放在VideoCaptureDevice.cs同目录下
1 //AForge Direct Show Library2 //AForge.NET framework3 // http://www.aforgenet.com/framework/
4 //
5 //Copyright © AForge.NET, 2009-20136 //contacts@aforgenet.com7 //8
9 namespaceAForge.Video.DirectShow10 {11 usingSystem;12
13 ///
14 ///The enumeration specifies a setting on a camera.15 ///
16 public enumVideoProcAmpProperty17 {18 ///
19 ///Brightness control.20 ///
21 Brightness = 0,22
23 ///
24 ///Contrast control.25 ///
26 Contrast,27
28 ///
29 ///Hue control.30 ///
31 Hue,32
33 ///
34 ///Saturation control.35 ///
36 Saturation,37
38 ///
39 ///Sharpness control.40 ///
41 Sharpness,42
43 ///
44 ///Gamma control.45 ///
46 Gamma,47
48 ///
49 ///ColorEnable control.50 ///
51 ColorEnable,52
53 ///
54 ///WhiteBalance control.55 ///
56 WhiteBalance,57
58 ///
59 ///BacklightCompensation control.60 ///
61 BacklightCompensation,62
63 ///
64 ///Gain control.65 ///
66 Gain67 }68
69 ///
70 ///The enumeration defines whether a camera setting is controlled manually or automatically.71 ///
72 [Flags]73 public enumVideoProcAmpFlags74 {75 ///
76 ///No control flag.77 ///
78 None = 0x0,79
80 ///
81 ///Auto control Flag.82 ///
83 Auto = 0x0001,84
85 ///
86 ///Manual control Flag.87 ///
88 Manual = 0x0002
89 }90 }
生成dll添加引用就可以了



