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, ...
随机推荐
- Leetcode: Minimum Domino Rotations For Equal Row
In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domin ...
- openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...
- 品优购商城项目(二)AngularJS、自动代码生成器、select2下拉多选框
品优购商城想项目第二阶段 AngularJS.自动代码生成器.select2下拉多选框 完成了课程第三天.第四天的的任务. 1.学习了AngularJs前端的mvc分层思想,js部分分成control ...
- ifc osg施工现场模拟
基于ifc数据模型的施工现场模拟
- C++数据存储方式
1.栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区,里面的变量通常是局部变量.函数参数等. 2.堆,就是那些由new分配的内存块,他们的释放编译器不去管,由我们的应用程序去 ...
- Java Thread Local – How to use and code sample(转)
转载自:https://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/ Thread Local ...
- Top 10 Machine Learning Algorithms For Beginners
Linear Regression Logistic regression KNN Classification Support Vector Machine (SVM) Decision Trees ...
- 《MySQL必知必会》学习笔记——前言
前言 MySQL已经成为世界上最受欢迎的数据库管理系统之一.无论是用在小型开发项目上,还是用来构建那些声名显赫的网站,MySQL都证明了自己是个稳定.可靠.快速.可信的系统,足以胜任任何数据存储业务的 ...
- [lodop]css样式after、before添加content内容之前和之后
css样式可以在内容之前和之后加内容.格式是:css类名:before{content:在之前加的内容}css类名:after{content:在之后加的内容}这种写法在LODOP里直接测试是不行的, ...
- AI - AutoKeras - 简介
前言 在数据集上训练神经网络时,主要有两个目标: 定义符合数据集特性的神经网络架构. 在许多试验中对一组超参数进行调优,从而使得模型具有较高的准确率并且能够泛化至训练集和测试集之外的数据. 针对不同的 ...