簡單介紹C#呼叫USB攝像頭的方法

大雄45發表於2022-04-08
導讀 這篇文章主要為大家詳細介紹了C#呼叫USB攝像頭的方法,文中示例程式碼介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下

C#呼叫USB攝像頭使用AForge類庫進行開發,供大家參考,具體內容如下

1、AForge安裝

右擊工程,在管理NuGet程式包中搜尋Aforge類庫,選擇安裝,如下圖所示

簡單介紹C#呼叫USB攝像頭的方法簡單介紹C#呼叫USB攝像頭的方法

簡單介紹C#呼叫USB攝像頭的方法簡單介紹C#呼叫USB攝像頭的方法

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);
            }
        }
  
        ////// 影像旋轉
        ////// 
        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;
        ////// 預設解析度
        ///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)
            {
     }
}

以上就是本文的全部內容,希望對大家的學習有所幫助。

原文來自:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69955379/viewspace-2886122/,如需轉載,請註明出處,否則將追究法律責任。

相關文章