今天分享一段aforge连接摄像头,并配置摄像头图像参数和相机控制的代码。
连接相机:
public void cameraOpen()
{
// 设定初始视频设备
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0)
{ // 默认设备
// CloseCaptureDevice();
cb.ItemsSource = videoDevices;
cb.SelectedValuePath = "MonikerString";
cb.DisplayMemberPath = "Name";
cb.SelectedIndex = 1;
UsbVideo = new VideoCaptureDevice(videoDevices[1].MonikerString);
camera_Chemistry.vp.videoMonkeyName = videoDevices[1].MonikerString;
sourcePlayer.VideoSource = UsbVideo;
UsbVideo.VideoResolution = UsbVideo.VideoCapabilities[0];
UsbVideo.SnapshotFrame += UsbVideo_SnapshotFrame;
UsbVideo.NewFrame += UsbVideo_NewFrame;
sourcePlayer.Start();
}
else
{
//button_Play.IsEnabled = false;
//button_Capture.IsEnabled = false;
}
}
设置图像和控制相机
#region 视频相关
private void GanHuaxueCamera()
{
camera_Chemistry.Cameratype = CameraType.USB;
camera_Chemistry.Usage = CameraUsage.干化学;
camera_Chemistry.videoDevice = UsbVideo;
camera_Chemistry.ReadProperty(UsbVideo);
stcvediosliders.Children.Clear();
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Brightness, "亮度", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Gain, "增益", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Contrast, "对比度", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Hue, "色调", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Saturation, "饱和度", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Sharpness, "清晰度", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_Gamma, "伽马值", true);
// InitVideoParam(VideoProcAmpProperty.VideoProcAmp_ColorEnable, "对比度", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_WhiteBalance, "白平衡", true);
InitVideoParam(VideoProcAmpProperty.VideoProcAmp_BacklightCompensation, "逆光对比", true);
InitVideoParam(VideoProcAmpProperty.VideoProAmp_Red, "红色分量", true);
InitVideoParam(VideoProcAmpProperty.VideoProAmp_Green, "绿色分量", true);
InitVideoParam(VideoProcAmpProperty.VideoProAmp_Blue, "蓝色分量", true);
// InitVideoParam(VideoProcAmpProperty.VideoProAmp_Electric, "对比度", true);
InitCameraParam(CameraControlProperty.Pan, "全景", true);
InitCameraParam(CameraControlProperty.Iris, "光圈", true);
InitCameraParam(CameraControlProperty.Roll, "Roll_", true);
InitCameraParam(CameraControlProperty.Tilt, "倾斜", true);
InitCameraParam(CameraControlProperty.Exposure, "曝光", true);
InitCameraParam(CameraControlProperty.Focus, "焦点", true);
InitCameraParam(CameraControlProperty.Zoom, "缩放", true);
InitCameraParam(CameraControlProperty.Roll, "滚动", true);
InitCameraParam(CameraControlProperty.LowCompensation, "低亮度补偿", true);
InitCameraParam(CameraControlProperty.DeNoise, "降噪", true);
}
private void InitCameraParam(CameraControlProperty cp, string vslidername, bool Check)
{
CameraProperty vpp = (CameraProperty)camera_Chemistry.CameraProperty[cp];
if (vpp == null) return;
VideoSlider vs = new VideoSlider(camera_Chemistry);
vs.Name = vslidername;
vs.VideoProcAmpPropertyItemName = vslidername;
if (vpp != null)
{
vs.CameraPropertyCustom = vpp;
}
stccanmreraliders.Children.Add(vs);
}
///
/// //////////////////////
///
///
///
///
private void InitVideoParam(VideoProcAmpProperty cp, string vslidername, bool Check)
{
VideoAMPProperty vpp = (VideoAMPProperty)camera_Chemistry.videoAMPProperty[cp];
VideoSlider vs = new VideoSlider(camera_Chemistry);
vs.Name =vslidername;
vs.VideoProcAmpPropertyItemName = vslidername;
if (vpp != null)
{
vs.AmpProperty = vpp;
}
stcvediosliders.Children.Add(vs);
}