網(wǎng)站首頁 編程語言 正文
C#調(diào)用USB攝像頭使用AForge類庫進行開發(fā),供大家參考,具體內(nèi)容如下
1、AForge安裝
右擊工程,在管理NuGet程序包中搜索Aforge類庫,選擇安裝,如下圖所示
2、進行USB攝像頭類封裝
a、初始化,初始化時要注意,加載的設(shè)備分辨率需要人工配置,如果配置分辨率不存在需要從默認的分辨率中選擇
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、綁定回調(diào)方法,此方法在攝像頭成功預(yù)覽之后會實時返回數(shù)據(jù)幀,封裝時可以傳入PictureBox,把回調(diào)旋轉(zhuǎn)后的圖片顯示在此控件上
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> ? ? ? ? /// 圖像旋轉(zhuǎn) ? ? ? ? /// </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、抓圖事件,手動抓圖事件,通過調(diào)用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攝像頭預(yù)覽中,可以對設(shè)備進行重連
private FilterInfoCollection videoDevices = null; //攝像頭設(shè)備 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) ? ? ? ? ? ? { ? ? ?} }
原文鏈接:https://evint.blog.csdn.net/article/details/122504038
相關(guān)推薦
- 2022-06-21 C語言詳解無頭單向非循環(huán)鏈表各種操作方法_C 語言
- 2022-10-17 在?C#?中使用?Span<T>?和?Memory<T>?編寫高性能代碼的詳
- 2022-04-22 阿里云ECS服務(wù)器Linux下載安裝JDK8
- 2022-09-01 C語言中static與sizeof查缺補漏篇_C 語言
- 2022-05-04 分享3個非常實用的?Python?模塊_python
- 2022-10-01 sql語法中的concat()函數(shù)詳解_MsSql
- 2022-04-05 C語言實現(xiàn)自動售貨機_C 語言
- 2022-11-07 PostgreSQL常用優(yōu)化技巧示例介紹_PostgreSQL
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支