欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#調(diào)用USB攝像頭的方法

 更新時(shí)間:2022年03月27日 08:24:38   作者:程序猿evint  
這篇文章主要為大家詳細(xì)介紹了C#調(diào)用USB攝像頭的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

C#調(diào)用USB攝像頭使用AForge類庫(kù)進(jìn)行開(kāi)發(fā),供大家參考,具體內(nèi)容如下

1、AForge安裝

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

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)抓圖事件,通過(guò)調(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、攝像頭重連,此類庫(kù)中videoSourcePlayer有個(gè)屬性IsRunning可以判斷是否USB攝像頭預(yù)覽中,可以對(duì)設(shè)備進(jìn)行重連

private FilterInfoCollection videoDevices = null; //攝像頭設(shè)備
public VideoCaptureDevice videoSource = null; //視頻的來(lái)源選擇
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)
? ? ? ? ? ? {
? ? ?}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式

    C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式

    這篇文章介紹了C#實(shí)現(xiàn)遞歸調(diào)用的Lambda表達(dá)式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • c#實(shí)現(xiàn)漢諾塔問(wèn)題示例

    c#實(shí)現(xiàn)漢諾塔問(wèn)題示例

    這篇文章主要介紹了c#實(shí)現(xiàn)漢諾塔問(wèn)題示例,需要的朋友可以參考下
    2014-04-04
  • C#日歷樣式的下拉式計(jì)算器實(shí)例講解

    C#日歷樣式的下拉式計(jì)算器實(shí)例講解

    如果我們正在做一個(gè)類似于庫(kù)存控制和計(jì)費(fèi)系統(tǒng)的項(xiàng)目,有些部分可能必須手動(dòng)計(jì)算數(shù)值。因此,用戶就不得不使用計(jì)算器得到結(jié)果,再填入到輸入字段中,或者在工作窗口上單獨(dú)打開(kāi)一個(gè)計(jì)算器窗口??傊?,各種不便和麻煩。
    2015-09-09
  • C#/VB.NET讀取條碼類型及條碼在圖片中的坐標(biāo)位置實(shí)例

    C#/VB.NET讀取條碼類型及條碼在圖片中的坐標(biāo)位置實(shí)例

    我們?cè)趧?chuàng)建條形碼時(shí),如果以圖片的方式將創(chuàng)建好的條碼保存到指定文件夾路徑,可以在程序中直接加載圖片使用;已生成的條碼圖片,需要通過(guò)讀取圖片中的條碼信息,如條碼類型、條碼繪制區(qū)域在圖片中的四個(gè)頂點(diǎn)坐標(biāo)位置等,可參考本文中的方法
    2023-10-10
  • C#使用TimeSpan時(shí)間計(jì)算的簡(jiǎn)單實(shí)現(xiàn)

    C#使用TimeSpan時(shí)間計(jì)算的簡(jiǎn)單實(shí)現(xiàn)

    這篇文章主要給大家介紹了關(guān)于C#使用TimeSpan時(shí)間計(jì)算的相關(guān)資料,以及通過(guò)一個(gè)實(shí)例代碼給大家介紹了C#使用timespan和timer完成一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器的方法,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • C#抽象類和接口的區(qū)別分析

    C#抽象類和接口的區(qū)別分析

    這篇文章主要介紹了C#抽象類和接口的區(qū)別,詳細(xì)的分析了抽象類與接口的概念與特性,并對(duì)二者作出比對(duì)說(shuō)明,需要的朋友可以參考下
    2014-10-10
  • C# 獲取IP及判斷IP是否在區(qū)間

    C# 獲取IP及判斷IP是否在區(qū)間

    本文主要介紹了C# 獲取IP及判斷IP是否在區(qū)間的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • 淺析c#范型中的特殊關(guān)鍵字where & default

    淺析c#范型中的特殊關(guān)鍵字where & default

    以下是對(duì)c#范型中的特殊關(guān)鍵字where和default進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下
    2013-09-09
  • 淺談Silverlight 跨線程的使用詳解

    淺談Silverlight 跨線程的使用詳解

    本篇文章是對(duì)Silverlight跨線程的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法

    VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法

    這篇文章主要介紹了VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法,需要的朋友可以參考下
    2017-02-02

最新評(píng)論