【转】【C#】 AForge调用摄像头

AForge.NET专为计算机视觉和人工智能应用而设计,这种C#框架适用于图像处理、神经网络、遗传算法、模糊逻辑、机器学习和机器人等。
该库是一个开源项目,包括:
AForge.Imaging —— 一些日常的图像处理和过滤器
 AForge.Vision —— 计算机视觉应用类库
 AForge.Neuro —— 神经网络计算库AForge.Genetic -进化算法编程库
 AForge.MachineLearning —— 机器学习类库
 AForge.Robotics —— 提供一些机器人的工具类库
 AForge.Video —— 一系列的视频处理类库
 AForge.Fuzzy —— 模糊推理系统类库
 AForge.Controls—— 图像,三维,图表显示控件
具体见官网:http://www.aforgenet.com/
简单用法如下:
1、安装包:VS→工具→包管理器→输入关键字“AForge”→安装
2、获取本机摄像头、声卡设备列表

List<string> VideoList = GetVideoInDevicesList();
VideolistBox.DataSource = VideoList;
 
List<string> AudioList = GetAudioInDevicesList();
AudiolistBox.DataSource = AudioList;
 
/// <summary>
/// 获取摄像头列表
/// </summary>
/// <returns></returns>
public static List<string> GetVideoInDevicesList()
{
    List<string> devicesList = new List<string>();
    try
    {
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in videoDevices)
{
    devicesList.Add(device.Name);
}
    }
    catch (Exception ex)
    {
Console.WriteLine(ex.Message);
    }
    return devicesList;
}
 
/// <summary>
/// 获取音频设备列表
/// </summary>
/// <returns></returns>
public static List<string> GetAudioInDevicesList()
{
    List<string> devicesList = new List<string>();
    try
    {
var videoDevices = new FilterInfoCollection(FilterCategory.AudioInputDevice);//输入设备
foreach (FilterInfo device in videoDevices)
{
    devicesList.Add(device.Name);
}
    }
    catch (ApplicationException)
    {
Console.WriteLine("No local capture devices");
    }
    return devicesList;
}

运行结果:

 3、打开摄像头显示内容:

var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
FilterInfo info = videoDevices[VideolistBox.SelectedIndex];
 
var videoSource = new VideoCaptureDevice(info.MonikerString);
AForge.Controls.VideoSourcePlayer videoSourcePlayer1 = new AForge.Controls.VideoSourcePlayer();
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Width = 1600;
videoSourcePlayer1.Height = 900;
this.Controls.Add(videoSourcePlayer1);
videoSourcePlayer1.Start();

4、调整分辨率

private void SetResolution()
{
    //分辨率调整至最高
    try
    {
        var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        FilterInfo info = videoDevices[VideolistBox.SelectedIndex];
        var videoSource = new VideoCaptureDevice(info.MonikerString);
        var formats = videoSource.VideoCapabilities;
        if (formats.Length > 1)
        {
            videoSource.VideoResolution = formats[0];//默认第一个就是最高分辨率
        }
    }
    catch { }
}

5、调用摄像头参数面板

//摄像头参数设置
private void CamSetup()
{
    var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    FilterInfo info = videoDevices[VideolistBox.SelectedIndex];
 
    VideoCaptureDevice videoCaptureDevice = new VideoCaptureDevice(info.MonikerString);
    try
    {
        videoCaptureDevice.DisplayPropertyPage(this.Handle);
    }
    catch 
    {
        MessageBox.Show("所选视频设备不支持设置页面","出错",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
    }
 
}

 6、画面的放大、平移

方法:

详见:http://www.aforgenet.com/framework/docs/html/077a7afb-c9bd-91b6-6870-61440e2f4060.htm

public bool SetCameraProperty(
    CameraControlProperty property,
    int value,
    CameraControlFlags controlFlags
)

其中:property

Pan水平移动(放大后)-16 至 +16
Tilt垂直移动(放大后)-16 至 +16
Roll旋转 0 至 3
Zoom缩放 100 至 400
Exposure曝光 -12 至 -3
Iris红外灯
Focus聚焦


实例:

var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
FilterInfo info = videoDevices[VideolistBox.SelectedIndex];
 
VideoCaptureDevice videoCaptureDevice = new VideoCaptureDevice(info.MonikerString);
try
{
    videoCaptureDevice.SetCameraProperty(CameraControlProperty.Zoom, 400, CameraControlFlags.Manual);
}
catch 
{
    MessageBox.Show("所选视频设备不支持","出错",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}

7、图片镜像

调用videoCaptureDevice.NewFrame 事件

 var temp = e.Frame.Clone();

temp.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX);//水平镜像
之后显示temp图片即可

原文地址:https://blog.csdn.net/dgnankai/article/details/128823793

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值