溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#怎么調用USB攝像頭

發(fā)布時間:2022-03-28 09:12:49 來源:億速云 閱讀:585 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“C#怎么調用USB攝像頭”,在日常操作中,相信很多人在C#怎么調用USB攝像頭問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#怎么調用USB攝像頭”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

1、AForge安裝

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

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、綁定回調方法,此方法在攝像頭成功預覽之后會實時返回數(shù)據(jù)幀,封裝時可以傳入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攝像頭”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

usb
AI