首页 > 编程语言 >【转】【C#】 AForge调用摄像头

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

时间:2023-04-06 12:33:23浏览次数:46  
标签:info C# List AForge videoDevices var new 摄像头

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

 

标签:info,C#,List,AForge,videoDevices,var,new,摄像头
From: https://www.cnblogs.com/mqxs/p/17292399.html

相关文章

  • Hadoop技术之Apache Hadoop集群搭建Apache Hadoop概述
    Hadoop离线是大数据生态圈的核心与基石,是整个大数据开发的入门。本次分享内容让初学者能高效、快捷掌握Hadoop必备知识,大大缩短Hadoop离线阶段学习时间,下面一起开始今天的学习!一、Hadoop集群简介Hadoop集群整体概述Hadoop集群包括两个集群: HDFS集群、YARN集群两个集群逻辑上分离......
  • 【过滤器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
    简介过滤器模式(FilterPattern)或标准模式(CriteriaPattern),是一种结构型模式。这种模式允许使用不同的标准条件来过滤一组对象,并通过逻辑运算的方式把各条件连接起来,它结合多个标准来获得单一标准。例子将创建一个Person对象、Criteria接口和实现了该接口的实体类,来过滤Perso......
  • vscode配置docker插件
    参考:https://zhuanlan.zhihu.com/p/530413695  ================================================  本文不对vscode、docker的安装及docker插件的安装进行讲解,本文只讲在vscode上如何配置以使docker插件正常使用。 VSCODE上安装好docker插件后会发现并不能正常使用......
  • VsCode开发工具的入门及基本使用
    (VsCode开发工具的入门及基本使用)一、VsCode介绍1.VsCode简介VisualStudioCode(简称“VSCode”)是Microsoft在2015年4月30日Build开发者大会上正式宣布一个运行于MacOSX、Windows和Linux之上的,针对于编写现代Web和云应用的跨平台源代码编辑器,可用于Windows,macOS和Lin......
  • org.apache.shiro.session.InvalidSessionException: java.lang.I
    1.遇到以下异常,找了好长时间,终于解决,报的异常如下:七月07,20173:02:16下午org.apache.catalina.core.StandardWrapperValveinvoke严重:Servlet.service()forservlet[SpringMVC]incontextwithpath[/IMP]threwexception[org.apache.shiro.session.InvalidSessionEx......
  • Oracle 11g asm中不同au size下datafile的au分布初探
    今天有朋友问11g中asm的ausize不为1m的情况下,datafile的au分布是怎么样的?通过10g的方式去kfedread,发现不对了,原帖地址:~~【高手请进】在oracle11g中通过kfed找到ASMAU空间分布信息?下午花了一点时间研究了一下,其中还有些没有明白,不过基本上搞清楚了,下面是简单的实验过程:开始我......
  • oracle如何恢复被误误删除的pdb
    最近太忙,一直没时间写blog;加上前段时间blog空间除了点问题,因此整体迁移到阿里云并且重新备案了。后续有时间我会定期更新,保持写作习惯,不能把技术荒废了。这是去年底某客户的一个case,误删除了6个pdb且带了includingdatafiles参数;这是一个非常复杂的恢复;据说用户开始找了国内很多恢......
  • C# WebApi - Basic验证实现;
    1.Filter文件夹下添加如下BasicAuthorizeAttribute类;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Web;usingSystem.Web.Http;usingSystem.Web.Security;namespaceABBPMP.WebApi.Filter{///<summary>///自定......
  • 8-springcloud-eureka-4-向Eureka服务注册中心注册服务
    eureka-server就是我们自己开发的module,是eureka的服务端,扮演注册中心的角色;eureka-client就是我们自己开的微服务module,向eureka-server注册中心注册服务;前面搭建了服务提供者项目,接下来我们就可以将该服务提供者注册到Eureke注册中心,步骤如下:demo1-provider项目中操作1、在......
  • socks5的两种验证方法
    Socks5协议支持两种类型的验证方法:无验证(NoAuthentication)和用户名密码验证(Username/PasswordAuthentication)。1、无验证(NoAuthentication):在此方法中,Socks5客户端与Socks5服务器建立连接后,客户端发送一个不带验证信息的请求,直接进行连接。这种方式比较简单,但是缺乏安全性,因为任......