这篇文章主要介绍“C#怎么调用USB摄像头”,在日常操作中,相信很多人在C#怎么调用USB摄像头问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么调用USB摄像头”的疑惑有所帮助!接下来,请跟着小编一起来
这篇文章主要介绍“C#怎么调用USB摄像头”,在日常操作中,相信很多人在C#怎么调用USB摄像头问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么调用USB摄像头”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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摄像头”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: C#怎么调用USB摄像头
本文链接: https://www.lsjlt.com/news/325784.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0