網(wǎng)站首頁 編程語言 正文
C#調(diào)用USB攝像頭使用AForge類庫進(jìn)行開發(fā),供大家參考,具體內(nèi)容如下
1、AForge安裝
右擊工程,在管理NuGet程序包中搜索Aforge類庫,選擇安裝,如下圖所示
2、進(jìn)行USB攝像頭類封裝
a、初始化,初始化時(shí)要注意,加載的設(shè)備分辨率需要人工配置,如果配置分辨率不存在需要從默認(rèn)的分辨率中選擇
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ù)覽之后會(huì)實(shí)時(shí)返回?cái)?shù)據(jù)幀,封裝時(shí)可以傳入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、抓圖事件,手動(dòng)抓圖事件,通過調(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有個(gè)屬性IsRunning可以判斷是否USB攝像頭預(yù)覽中,可以對(duì)設(shè)備進(jìn)行重連
private FilterInfoCollection videoDevices = null; //攝像頭設(shè)備 public VideoCaptureDevice videoSource = null; //視頻的來源選擇 private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer(); public Bitmap img = null; public int CameraIndex = 1; ? ? ? ? /// <summary> ? ? ? ? /// 默認(rèn)分辨率 ? ? ? ? /// </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-08-04 C#中ListView用法實(shí)例_C#教程
- 2022-07-18 SQL?Server中實(shí)現(xiàn)錯(cuò)誤處理_MsSql
- 2022-10-08 C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析_C#教程
- 2022-03-19 使用Docker部署Spring?Boot項(xiàng)目的實(shí)現(xiàn)步驟_docker
- 2021-12-18 C++?STL容器詳解之紅黑樹部分模擬實(shí)現(xiàn)_C 語言
- 2022-11-28 深入了解C++函數(shù)重載解析策略_C 語言
- 2022-06-30 C++?select模型簡單聊天室的實(shí)現(xiàn)示例_C 語言
- 2022-12-03 高并發(fā)技巧之Redis和本地緩存使用技巧分享_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支