C#使用Aforge对uvc协议摄像头亮度属性的更改

使用c#自带的DirectShow类对基于uvc协议的摄像头进行亮度参数的更改时,需要打开其自带的一个页面,如果想要实现在代码中更改需要使用DirectShow的扩展类。首先,需要在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 >= 0);

                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 >= 0);

                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 >= 0);

                Marshal.ReleaseComObject(tempSourceObject);
            }

            return ret;
        }

然后,需要新建类VideoProcAmpProperty(和VideoCaptureDevice.cs在同一目录)

 using System;

    /// <summary>
    /// The enumeration specifies a setting on a camera.
    /// </summary>
    public enum VideoProcAmpProperty
    {
        /// <summary>
        /// Brightness control.
        /// </summary>
        Brightness = 0,

        /// <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
    }

最后需要在Internals文件夹下新建IAMVideoProcAmp类

 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
            );
    }

然后进行重新生成解决方案并在想要使用扩展类的项目中更改引用即可使用DirectShow的扩展类。
DirectShow的源代码可从c#的包管理器中点击DirectShow的源路径获取。
嫌麻烦的可以去我上传的资源进行下载。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值