调整网络摄像头亮度(曝光/增益)C#
我正在尝试调整我的网络摄像头的亮度。我需要用不同的亮度设置拍摄3张不同的照片。我不想让它成为手动的,所以如果我想在程序中包括设置 下面是我正在使用的代码。使用methode GetFrame()从网络摄像头获取下一张图片。我知道有DirectShow(iamvideoprocamp),我读了其他问题,但我仍然不知道如何整合它。有人能给我一个提示或c#的例子吗。谢谢调整网络摄像头亮度(曝光/增益)C#,c#,webcam,directshow.net,brightness,vfw,C#,Webcam,Directshow.net,Brightness,Vfw,我正在尝试调整我的网络摄像头的亮度。我需要用不同的亮度设置拍摄3张不同的照片。我不想让它成为手动的,所以如果我想在程序中包括设置 下面是我正在使用的代码。使用methode GetFrame()从网络摄像头获取下一张图片。我知道有DirectShow(iamvideoprocamp),我读了其他问题,但我仍然不知道如何整合它。有人能给我一个提示或c#的例子吗。谢谢 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用Syste
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
使用系统图;
使用系统线程;
使用DirectShowLib;
库格尔
{
类框架
{
//属性变量
专用int m_宽度=640;
私人内部m_高度=480;
私有int mCapHwnd;
//全局变量,以加快视频捕获速度
私有IDataObject tempObj;
专用系统.Drawing.Image tempImg;
#区域API声明
[DllImport(“user32”,EntryPoint=“SendMessage”)]
公共静态外部int SendMessage(int hWnd、uint Msg、int wParam、int lParam);
[DllImport(“avicap32.dll”,EntryPoint=“capCreateCaptureWindowA”)]
公共静态外部int capCreateCaptureWindowA(字符串lpszWindowName、int dwStyle、int X、int Y、int nWidth、int nHeight、int hwndParent、int nID);
[DllImport(“user32”,EntryPoint=“OpenClipboard”)]
公共静态外部intopenclipboard(inthwnd);
[DllImport(“user32”,EntryPoint=“emptycipboard”)]
公共静态外部int EmptyClipboard();
[DllImport(“user32”,EntryPoint=“CloseClipboard”)]
公共静态外部int CloseClipboard();
#端区
#区域API常数
public const int WM_USER=1024;
公共建筑内部WM\U CAP\U CONNECT=1034;
公共建筑内部WM_CAP_DISCONNECT=1035;
公共const int WM_CAP_GET_FRAME=1084;
公共建筑内部WM_CAP_副本=1054;
公共const int WM_CAP_START=WM_USER;
公共const int WM_CAP_DLG_VIDEOFORMAT=WM_CAP_START+41;
公共const int WM_CAP_DLG_VIDEOSOURCE=WM_CAP_START+42;
公共建筑内部WM_CAP_DLG_VIDEODISPLAY=WM_CAP_START+43;
public const int WM_CAP_GET_VIDEOFORMAT=WM_CAP_START+44;
公共const int WM_CAP_SET_VIDEOFORMAT=WM_CAP_START+45;
公共const int WM_CAP_DLG_VIDEOCOMPRESSION=WM_CAP_START+46;
公共建筑内部WM\U CAP\U SET\U PREVIEW=WM\U CAP\U START+50;
#端区
#区域启动和停止捕获功能
///
///开始视频捕获
///
公开作废开始()
{
尝试
{
//为了安全起见,呼叫停止,以防我们已经在运行
这个。停止();
//设置捕获窗口
mCapHwnd=capCreateCaptureWindowA(“网络地图”,0,0,0,m_宽度,m_高度,0,0);
//连接到捕获设备
Application.DoEvents();
SendMessage(mCapHwnd,WM_CAP_CONNECT,0,0);
SendMessage(mCapHwnd,WM\u CAP\u SET\u PREVIEW,0,0);
}
捕获(例外情况除外)
{
//MessageBox.Show(“启动视频捕获时出错。请检查网络摄像机是否正确连接并已打开。\r\n\n”+例外消息);
这个。停止();
}
}
///
///停止视频捕获
///
公共停车场()
{
尝试
{
//断开与视频源的连接
Application.DoEvents();
发送消息(mCapHwnd,WM_CAP_DISCONNECT,0,0);
}
捕获(例外情况除外)
{//不要在此处引发错误。
}
}
#端区
公共图书馆
{
尝试
{
SendMessage(mCapHwnd,WM_CAP_DLG_VIDEOSOURCE,0,0);
}
捕获(例外情况除外)
{
//MessageBox.Show(“捕获视频图像时出错。视频捕获将立即终止。\r\n\n”+例外消息);
}
}
公共映像GetFrame()
{
尝试
{
//获取下一帧;
SendMessage(mCapHwnd,WM_CAP_GET_FRAME,0,0);
//将框架复制到剪贴板
SendMessage(mCapHwnd,WM_CAP_COPY,0,0);
//从剪贴板中获取
tempObj=Clipboard.GetDataObject();
tempImg=(System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
返回时间;
}
捕获(例外情况除外)
{
//MessageBox.Show(“捕获视频图像时出错。视频捕获将立即终止。\r\n\n”+例外消息);
这个。停止();
}
返回null;
}
您几乎无法集成DirectShow和您提供的代码段,因为您的GetFrame
使用的是完全不同的API:
这是两个不同的API,它们可能共享同一个视频捕获设备。因此,很可能您在DirectShow中找到了相同的设备,然后您开始构建捕获图,您获得了界面,过滤器将希望在不启动视频捕获的情况下接受您的亮度变化,然后它将希望使它们成为persi支架,因此可通过VfW API用于同一设备。考虑到所有这些假设和复杂性,您可以通过DirectShow进行调整,而通过VfW进行捕获。但是,完全转到DirectShow看起来更合理。设置缩放,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using DirectShowLib;
namespace Kugel
{
class Frames
{
// property variables
private int m_Width = 640;
private int m_Height = 480;
private int mCapHwnd;
// global variables to make the video capture go faster
private IDataObject tempObj;
private System.Drawing.Image tempImg;
#region API Declarations
[DllImport("user32", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
[DllImport("user32", EntryPoint = "OpenClipboard")]
public static extern int OpenClipboard(int hWnd);
[DllImport("user32", EntryPoint = "EmptyClipboard")]
public static extern int EmptyClipboard();
[DllImport("user32", EntryPoint = "CloseClipboard")]
public static extern int CloseClipboard();
#endregion
#region API Constants
public const int WM_USER = 1024;
public const int WM_CAP_CONNECT = 1034;
public const int WM_CAP_DISCONNECT = 1035;
public const int WM_CAP_GET_FRAME = 1084;
public const int WM_CAP_COPY = 1054;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;
public const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;
public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;
public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;
public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
#endregion
#region Start and Stop Capture Functions
/// <summary>
/// Starts the video capture
/// </summary>
public void Start()
{
try
{
// for safety, call stop, just in case we are already running
this.Stop();
// setup a capture window
mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, 0, 0);
// connect to the capture device
Application.DoEvents();
SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);
}
catch (Exception ex)
{
// MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + excep.Message);
this.Stop();
}
}
/// <summary>
/// Stops the video capture
/// </summary>
public void Stop()
{
try
{
// disconnect from the video source
Application.DoEvents();
SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
}
catch (Exception ex)
{ // don't raise an error here.
}
}
#endregion
public void Prefer()
{
try
{
SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
}
catch (Exception ex)
{
// MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
}
}
public Image GetFrame()
{
try
{
// get the next frame;
SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
// copy the frame to the clipboard
SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
// get from the clipboard
tempObj = Clipboard.GetDataObject();
tempImg = (System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
return tempImg;
}
catch (Exception ex)
{
// MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
this.Stop();
}
return null;
}
videoDevice.SetCameraProperty(
CameraControlProperty.Zoom,
zoomValue,
CameraControlFlags.Manual);
videoDevice.SetCameraProperty(
CameraControlProperty.Focus,
focusValue,
CameraControlFlags.Manual);
videoDevice.SetCameraProperty(
CameraControlProperty.Exposure,
exposureValue,
CameraControlFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.Brightness,
brightnessValue,
VideoProcAmpFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.Contrast,
contrastValue,
VideoProcAmpFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.Saturation,
saturationValue,
VideoProcAmpFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.Sharpness,
sharpnessValue,
VideoProcAmpFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.WhiteBalance,
whiteBalanceValue,
VideoProcAmpFlags.Manual);
videoDevice.SetVideoProperty(
VideoProcAmpProperty.BacklightCompensation,
backlightCompensationValue,
VideoProcAmpFlags.Manual);