C# Aforge设置摄像头视频属性和控制属性
修改后的代码: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添加了几个方法用来获取和设置参数,替换掉源文件即可,也可以在原文件加上这几个方法的代码
/// <summary>
/// Sets a specified property on the camera.
/// </summary>
///
/// <param name="property">Specifies the property to set.</param>
/// <param name="value">Specifies the new value of the property.</param>
/// <param name="controlFlags">Specifies the desired control setting.</param>
///
/// <returns>Returns true on success or false otherwise.</returns>
///
/// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception>
/// <exception cref="ApplicationException">Failed creating device object for moniker.</exception>
/// <exception cref="NotSupportedException">The video source does not support camera control.</exception>
///
public bool SetVideoProperty(VideoProcAmpProperty property, int value, VideoProcAmpFlags controlFlags)
{
bool ret = true; // check if source was set
if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker)))
{
throw new ArgumentException("Video source is not specified.");
} lock (sync)
{
object tempSourceObject = null; // create source device's object
try
{
tempSourceObject = FilterInfo.CreateFilter(deviceMoniker);
}
catch
{
throw new ApplicationException("Failed creating device object for moniker.");
} if (!(tempSourceObject is IAMVideoProcAmp))
{
throw new NotSupportedException("The video source does not support camera control.");
} IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject;
int hr = pCamControl.Set(property, value, controlFlags); ret = (hr >= ); Marshal.ReleaseComObject(tempSourceObject);
} return ret;
} /// <summary>
/// Gets the current setting of a camera property.
/// </summary>
///
/// <param name="property">Specifies the property to retrieve.</param>
/// <param name="value">Receives the value of the property.</param>
/// <param name="controlFlags">Receives the value indicating whether the setting is controlled manually or automatically</param>
///
/// <returns>Returns true on success or false otherwise.</returns>
///
/// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception>
/// <exception cref="ApplicationException">Failed creating device object for moniker.</exception>
/// <exception cref="NotSupportedException">The video source does not support camera control.</exception>
///
public bool GetVideoProperty(VideoProcAmpProperty property, out int value, out VideoProcAmpFlags controlFlags)
{
bool ret = true; // check if source was set
if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker)))
{
throw new ArgumentException("Video source is not specified.");
} lock (sync)
{
object tempSourceObject = null; // create source device's object
try
{
tempSourceObject = FilterInfo.CreateFilter(deviceMoniker);
}
catch
{
throw new ApplicationException("Failed creating device object for moniker.");
} if (!(tempSourceObject is IAMVideoProcAmp))
{
throw new NotSupportedException("The video source does not support camera control.");
} IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject;
int hr = pCamControl.Get(property, out value, out controlFlags); ret = (hr >= ); Marshal.ReleaseComObject(tempSourceObject);
} return ret;
} /// <summary>
/// Gets the range and default value of a specified camera property.
/// </summary>
///
/// <param name="property">Specifies the property to query.</param>
/// <param name="minValue">Receives the minimum value of the property.</param>
/// <param name="maxValue">Receives the maximum value of the property.</param>
/// <param name="stepSize">Receives the step size for the property.</param>
/// <param name="defaultValue">Receives the default value of the property.</param>
/// <param name="controlFlags">Receives a member of the <see cref="CameraControlFlags"/> enumeration, indicating whether the property is controlled automatically or manually.</param>
///
/// <returns>Returns true on success or false otherwise.</returns>
///
/// <exception cref="ArgumentException">Video source is not specified - device moniker is not set.</exception>
/// <exception cref="ApplicationException">Failed creating device object for moniker.</exception>
/// <exception cref="NotSupportedException">The video source does not support camera control.</exception>
///
public bool GetVideoPropertyRange(VideoProcAmpProperty property, out int minValue, out int maxValue, out int stepSize, out int defaultValue, out VideoProcAmpFlags controlFlags)
{
bool ret = true; // check if source was set
if ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker)))
{
throw new ArgumentException("Video source is not specified.");
} lock (sync)
{
object tempSourceObject = null; // create source device's object
try
{
tempSourceObject = FilterInfo.CreateFilter(deviceMoniker);
}
catch
{
throw new ApplicationException("Failed creating device object for moniker.");
} if (!(tempSourceObject is IAMVideoProcAmp))
{
throw new NotSupportedException("The video source does not support camera control.");
} IAMVideoProcAmp pCamControl = (IAMVideoProcAmp)tempSourceObject;
int hr = pCamControl.GetRange(property, out minValue, out maxValue, out stepSize, out defaultValue, out controlFlags); ret = (hr >= ); Marshal.ReleaseComObject(tempSourceObject);
} return ret;
}
VideoProcAmpProperty.cs枚举对象,放在VideoCaptureDevice.cs同目录下
// AForge Direct Show Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2009-2013
// contacts@aforgenet.com
// namespace AForge.Video.DirectShow
{
using System; /// <summary>
/// The enumeration specifies a setting on a camera.
/// </summary>
public enum VideoProcAmpProperty
{
/// <summary>
/// Brightness control.
/// </summary>
Brightness = , /// <summary>
/// Contrast control.
/// </summary>
Contrast, /// <summary>
/// Hue control.
/// </summary>
Hue, /// <summary>
/// Saturation control.
/// </summary>
Saturation, /// <summary>
/// Sharpness control.
/// </summary>
Sharpness, /// <summary>
/// Gamma control.
/// </summary>
Gamma, /// <summary>
/// ColorEnable control.
/// </summary>
ColorEnable, /// <summary>
/// WhiteBalance control.
/// </summary>
WhiteBalance, /// <summary>
/// BacklightCompensation control.
/// </summary>
BacklightCompensation, /// <summary>
/// Gain control.
/// </summary>
Gain
} /// <summary>
/// The enumeration defines whether a camera setting is controlled manually or automatically.
/// </summary>
[Flags]
public enum VideoProcAmpFlags
{
/// <summary>
/// No control flag.
/// </summary>
None = 0x0, /// <summary>
/// Auto control Flag.
/// </summary>
Auto = 0x0001, /// <summary>
/// Manual control Flag.
/// </summary>
Manual = 0x0002
}
}
生成dll添加引用就可以了
C# Aforge设置摄像头视频属性和控制属性的更多相关文章
- HTML连载15-文本属性&颜色控制属性
一.文本装饰的属性 1.格式:text-decoration:underline; 2.取值: (1)underline代表下划线 (2)line-through代表删除线 (3)overline代表 ...
- 播放一个视频并用滚动条控制进度-OpenCV应用学习笔记二
今天我们来做个有趣的程序实现:利用OpenCV读取本地文件夹的视频文件,并且在窗口中创建拖动控制条来显示并且控制视频文件的读取进度. 此程序调试花费了笔者近一天时间,其实大体程序都已经很快写出,结果执 ...
- Emgucv(一)Aforge切换摄像头并调用摄像头属性
一.新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件.一个ComboBox控件,命名为PictureBox1.cbCapture,还有两个Button控件,Tex ...
- AForge.NET 设置摄像头分辨率
AForge.NET 老版本在预览摄像头时可通过设置DesiredFrameSize 属性,设置摄像头支持的分辨率,新版本提示已过期: 解决办法: 获取VideoCapabilities属性集合,选中 ...
- 使用 AForge.NET 做视频采集
AForge.NET 是基于C#设计的,在计算机视觉和人工智能方向拥有很强大功能的框架.btw... it's an open source framework. 附上官网地址: http://www ...
- 如何用FFmpeg API采集摄像头视频和麦克风音频,并实现录制文件的功能
之前一直用Directshow技术采集摄像头数据,但是觉得涉及的细节比较多,要开发者比较了解Directshow的框架知识,学习起来有一点点难度.最近发现很多人问怎么用FFmpeg采集摄像头图像,事实 ...
- WPF中在摄像头视频上叠加控件的解决方案
一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...
- 视频直播 object 标签属性详解
最近在做视频直播这一块的,html5的video不能实现此功能,在网上查找了资料,觉得很有用. 一.介绍: 我们要在网页中正常显示flash内容,那么页面中必须要有指定flash路径的标签.也就是OB ...
- HTML连载16-颜色控制属性2&标签选择器
一.颜色控制属性(上接连载15) (4)十六进制 在前端开发中通过十六进制来表示颜色,其实本质就是RGB,十六进制中是通过每两位表示一个颜色. 例如:#FFEE00,其中FF代表的是R,EE代表的G, ...
随机推荐
- Android通过ksoap2这个框架调用webservice大讲堂
昨天有人问我Android怎么连接mysql数据库,和对数据库的操作呀,我想把,给他说说json通信,可是他并不知道怎么弄,哎算了吧,直接叫他用ksoap吧,给他说了大半天,好多零碎的知识,看来还是有 ...
- 软件定义网络基础---SDN的核心思想
一:SDN包含的核心思想:解耦,抽象,可编程 二:解耦 (一)SDN网络解耦思想 解耦是指将控制平面和数据平面进行分离,主要为了解决传统网络中控制平面和数据平面在物理上紧耦合导致的问题 控制平面和数据 ...
- PP篇9 更改计划订单
首先看了下这个函数的版本 还挺新的 S4版本应该是有更新过,里面用了新语法 导入: PLANNEDORDER 计划订单号HEADERDATA 表头数据(来自外部系统) HEA ...
- LeetCode_349. Intersection of Two Arrays
349. Intersection of Two Arrays Easy Given two arrays, write a function to compute their intersectio ...
- rem设置网页字体大小
「rem」是指根元素(root element,html)的字体大小,好开心的是,从遥远的 IE6 到版本帝 Chrome 他们都约好了,根元素默认的 font-size 都是 16px.这样一个新的 ...
- vuex中mutations数据响应
vuex中的mutation需遵守Vue的响应规则: 既然Vuex的store中的状态是响应式的,那么在我们变更状态时,监视状态的Vuex最好在state中初始化好所有的所需属性. 如果需要在对象上添 ...
- Python之可变参数,*参数,**参数,以及传入*参数,进行解包
1.定义了一个需要两个参数的函数 def print_str(first, second): print first print second if __name__ == "__main_ ...
- Qt5.编译错误.error: C2338: The slot requires more arguments than the signal provides.
1.Qt563x86vs2015,遇到如下 编译错误: error: C2338: The slot requires more arguments than the signal provides. ...
- 高级UI-TableLayout
TableLayout选项卡,用于需要使用选项卡的场景,一般是用于切换Fragment,现在的主流做法一般是TableLayout+ViewPager+Fragment,综合实现选项卡的操作 由于Ta ...
- js 防抖 节流
函数防抖:将几次操作合并为一此操作进行.原理是维护一个计时器,规定在delay时间后触发函数,但是在delay时间内再次触发的话,就会取消之前的计时器而重新设置.这样一来,只有最后一次操作能被触发.( ...