这篇文章主要为大家详细介绍了C#调用USB摄像头的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
C#调用USB摄像头使用AForge类库进行开发,供大家参考,具体内容如下
1、AForge安装
右击工程,在管理NuGet程序包中搜索Aforge类库,选择安装,如下图所示
2、进行USB摄像头类封装
a、初始化,初始化时要注意,加载的设备分辨率需要人工配置,如果配置分辨率不存在需要从默认的分辨率中选择
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0 && videoDevices.Count >= CameraIndex)
{
FilterInfo info = videoDevices[videoDevices.Count - 1];
videoSource = new VideoCaptureDevice(info.MonikerString);
if (videoSource.VideoCapabilities.Length > 0)
{
VideoCapabilities tmp = videoSource.VideoCapabilities.
First(x => x.FrameSize.Width == LocalSize.Width &&
x.FrameSize.Height == LocalSize.Height);
if (tmp != null)
{
videoSource.SnapshotResolution = tmp;
videoSource.VideoResolution = tmp;
}
else
{
int index = (videoSource.VideoCapabilities.Length + 1) / 2;
tmp = videoSource.VideoCapabilities[index];
}
videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
videoSource.NewFrame += new NewFrameEventHandler(Video_NewFrame);
}
}
}
catch (Exception ex)
{
LogHelper.Debug(ex);
}
b、绑定回调方法,此方法在摄像头成功预览之后会实时返回数据帧,封装时可以传入PictureBox,把回调旋转后的图片显示在此控件上
private void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
BmpRotate(video);
if (UsbVideo != null)
UsbVideo.Image = video;
}
catch (Exception ex)
{
LogHelper.Debug(ex);
}
}
/// <summary>
/// 图像旋转
/// </summary>
/// <param name="_bmp"></param>
private void BmpRotate(Bitmap _bmp)
{
try
{
if (CameraRotate == "0")
{
}
else if (CameraRotate == "90")
{
_bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
else if (CameraRotate == "180")
{
_bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
else if (CameraRotate == "270")
{
_bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
}
catch (Exception ex)
{
LogHelper.Debug(ex);
}
}
c、抓图事件,手动抓图事件,通过调用GetCurrentVideoFrame()方法获取Bitmap图片
public Bitmap GetCurrentVideoFrame()
{
Bitmap bmp = null;
try
{
bmp = videoSourcePlayer.GetCurrentVideoFrame();
BmpRotate(bmp);
}
catch (Exception ex)
{
LogHelper.Debug(ex);
}
return bmp;
}
d、摄像头重连,此类库中videoSourcePlayer有个属性IsRunning可以判断是否USB摄像头预览中,可以对设备进行重连
private FilterInfoCollection videoDevices = null; //摄像头设备
public VideoCaptureDevice videoSource = null; //视频的来源选择
private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
public Bitmap img = null;
public int CameraIndex = 1;
/// <summary>
/// 默认分辨率
/// </summary>
public Size LocalSize = new Size(640, 480);
bool isHave = false;
public string CameraRotate = "0";
private System.Windows.Forms.PictureBox UsbVideo = null;
public void ReConnect()
{
try
{
if (!videoSourcePlayer.IsRunning)
{
videoSource.Stop();
videoSource.Start();
}
}
catch (Exception)
{
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
织梦狗教程
本文标题为:C#调用USB摄像头的方法


基础教程推荐
猜你喜欢
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C# 解析XML和反序列化的示例 2023-04-14
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity shader实现高斯模糊效果 2023-01-16