C#調(diào)用USB攝像頭的方法
C#調(diào)用USB攝像頭使用AForge類庫進(jìn)行開發(fā),供大家參考,具體內(nèi)容如下
1、AForge安裝
右擊工程,在管理NuGet程序包中搜索Aforge類庫,選擇安裝,如下圖所示


2、進(jìn)行USB攝像頭類封裝
a、初始化,初始化時要注意,加載的設(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ù)覽之后會實(shí)時返回?cái)?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è)備進(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)
? ? ? ? ? ? {
? ? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#調(diào)用usb攝像頭的實(shí)現(xiàn)方法
- C# WPF使用AForge類庫操作USB攝像頭拍照并保存
- C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭實(shí)例
- C#調(diào)用AForge實(shí)現(xiàn)攝像頭錄像的示例代碼
- C#實(shí)現(xiàn)控制攝像頭的類
- C#使用Aforge調(diào)用攝像頭拍照的方法
- C#結(jié)合AForge實(shí)現(xiàn)攝像頭錄像
- C#開發(fā)可播放攝像頭及任意格式視頻的播放器
- c#基于opencv,開發(fā)攝像頭播放程序
- C# 利用AForge實(shí)現(xiàn)攝像頭信息采集
相關(guān)文章
基于C#實(shí)現(xiàn)PDF按頁分割文件和分頁合并
iTextSharp 是一個開源的 PDF 處理庫,用于在 C# 程序中創(chuàng)建、編輯和處理 PDF 文件,本文將使用iTextSharp實(shí)現(xiàn)C# PDF分割與合并,感興趣的可以了解下2025-03-03
C#中C/S端實(shí)現(xiàn)WebService服務(wù)
本文主要介紹了C#中C/S端實(shí)現(xiàn)WebService服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#使用Directoryinfo類獲得目錄信息和屬性的方法
這篇文章主要介紹了C#使用Directoryinfo類獲得目錄信息和屬性的方法,涉及C#操作目錄的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
C# WinForm捕獲全局變量異常 SamWang解決方法
本文將介紹C# WinForm捕獲全局變量異常 SamWang解決方法,需要的朋友可以參考2012-11-11

