修改后的代码: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

https://files.cnblogs.com/files/maoruishan/AForge.Video.DirectShow%E6%89%A9%E5%B1%95%E4%BB%A3%E7%A0%81.zip

有三个文件,都是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设置摄像头视频属性和控制属性的更多相关文章

  1. HTML连载15-文本属性&颜色控制属性

    一.文本装饰的属性 1.格式:text-decoration:underline; 2.取值: (1)underline代表下划线 (2)line-through代表删除线 (3)overline代表 ...

  2. 播放一个视频并用滚动条控制进度-OpenCV应用学习笔记二

    今天我们来做个有趣的程序实现:利用OpenCV读取本地文件夹的视频文件,并且在窗口中创建拖动控制条来显示并且控制视频文件的读取进度. 此程序调试花费了笔者近一天时间,其实大体程序都已经很快写出,结果执 ...

  3. Emgucv(一)Aforge切换摄像头并调用摄像头属性

    一.新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件.一个ComboBox控件,命名为PictureBox1.cbCapture,还有两个Button控件,Tex ...

  4. AForge.NET 设置摄像头分辨率

    AForge.NET 老版本在预览摄像头时可通过设置DesiredFrameSize 属性,设置摄像头支持的分辨率,新版本提示已过期: 解决办法: 获取VideoCapabilities属性集合,选中 ...

  5. 使用 AForge.NET 做视频采集

    AForge.NET 是基于C#设计的,在计算机视觉和人工智能方向拥有很强大功能的框架.btw... it's an open source framework. 附上官网地址: http://www ...

  6. 如何用FFmpeg API采集摄像头视频和麦克风音频,并实现录制文件的功能

    之前一直用Directshow技术采集摄像头数据,但是觉得涉及的细节比较多,要开发者比较了解Directshow的框架知识,学习起来有一点点难度.最近发现很多人问怎么用FFmpeg采集摄像头图像,事实 ...

  7. WPF中在摄像头视频上叠加控件的解决方案

    一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...

  8. 视频直播 object 标签属性详解

    最近在做视频直播这一块的,html5的video不能实现此功能,在网上查找了资料,觉得很有用. 一.介绍: 我们要在网页中正常显示flash内容,那么页面中必须要有指定flash路径的标签.也就是OB ...

  9. HTML连载16-颜色控制属性2&标签选择器

    一.颜色控制属性(上接连载15) (4)十六进制 在前端开发中通过十六进制来表示颜色,其实本质就是RGB,十六进制中是通过每两位表示一个颜色. 例如:#FFEE00,其中FF代表的是R,EE代表的G, ...

随机推荐

  1. SpringBoot整合Quartz和H2的例子

    话不多说,直接上代码: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  2. INotifyPropertyChanged PropertyChangedEventArgs获取变更属性

    INotifyPropertyChanged: 该接口包含一个事件, 针对属性发生变更时, 执行该事件发生. // // 摘要: // 通知客户端属性值已更改. public interface IN ...

  3. CentOS 7 配置 nginx php-fpm 详细教程

    CentOS 7 配置 Nginx 的步骤如下: 首先更新 yum,没有安装 yum 的自行安装 yum update 1. 安装 Nginx yum install nginx 开启 Nginx 并 ...

  4. Python3基础 print 格式化输出 %% 输出%

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  5. JS 数字相加出现多个小数的问题

    今天在页面上用到了js进行小数相加119.01+0.01,结果大家都知道应该是:119.02的,然而结果是119..0200000…. ,莫名其妙的,还以为是我写的程序有问题,后来查了下才知道这是ja ...

  6. pip 安装指定版本

    pip在安装包的时候可以不需要从网上下载,以windows的scipy为例 pip install scipy==0.15.1 以上表示安装0.15.1版本的scipy,这里用”==”接版本,如果权限 ...

  7. ES6深入浅出-11 ES6新增的API(上)-2.Array新增API

    Array.form 把不是数组的东西变成数组.最常见的就是把伪数组变成数组 那么什么是伪数组 这就是伪数组,因为它不是继承自Array的原型的对象.它只是一个看起来很像数组的数组 只看下面的代码.a ...

  8. sklearn使用高斯核SVM显示支持向量

    import graphviz import mglearn from mpl_toolkits.mplot3d import Axes3D from sklearn.datasets import ...

  9. POJ 3903 Stock Exchange 最长上升子序列入门题

    题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...

  10. 创建 LVM

    1.将物理磁盘设备条带化为物理卷 # pvcreate /dev/sdb 查看物理卷: # pvs# pvdisplay 2.创建卷组,并添加 PV 到卷组 # vgcreate vg1 /dev/s ...