日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2023/12/10 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 setif ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))){throw new ArgumentException("Video source is not specified.");}lock (sync){object tempSourceObject = null;// create source device's objecttry{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 setif ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))){throw new ArgumentException("Video source is not specified.");}lock (sync){object tempSourceObject = null;// create source device's objecttry{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 setif ((deviceMoniker == null) || (string.IsNullOrEmpty(deviceMoniker))){throw new ArgumentException("Video source is not specified.");}lock (sync){object tempSourceObject = null;// create source device's objecttry{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的源路徑獲取。
嫌麻煩的可以去我上傳的資源進行下載。

總結

以上是生活随笔為你收集整理的C#使用Aforge对uvc协议摄像头亮度属性的更改的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。