最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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)
? ? ? ? ? ? {
? ? ?}
}

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

相關(guān)文章

  • C#實(shí)現(xiàn)線性查找算法

    C#實(shí)現(xiàn)線性查找算法

    這篇文章介紹了C#實(shí)現(xiàn)線性查找的算法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • WinForm窗體間傳值的方法

    WinForm窗體間傳值的方法

    這篇文章主要介紹了WinForm窗體間傳值的方法,包括了靜態(tài)變量、公共變量、共有屬性等方式,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-11-11
  • 基于C#實(shí)現(xiàn)PDF按頁分割文件和分頁合并

    基于C#實(shí)現(xiàn)PDF按頁分割文件和分頁合并

    iTextSharp 是一個(gè)開源的 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ù)

    本文主要介紹了C#中C/S端實(shí)現(xiàn)WebService服務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 詳解C# 結(jié)構(gòu)體

    詳解C# 結(jié)構(gòu)體

    這篇文章主要介紹了C# 結(jié)構(gòu)體的的相關(guān)資料,文中示例代碼非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C# 模擬瀏覽器并自動(dòng)操作的實(shí)例代碼

    C# 模擬瀏覽器并自動(dòng)操作的實(shí)例代碼

    這篇文章主要介紹了C# 模擬瀏覽器并自動(dòng)操作的實(shí)例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#判斷DLL文件是32位還是64位的示例代碼

    C#判斷DLL文件是32位還是64位的示例代碼

    有些時(shí)候我們需要判斷一下dll文件是32位還是64位,糾結(jié)該如何操作呢,下面小編通過實(shí)例代碼給大家介紹下C#判斷DLL文件是32位還是64位,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • C#使用Directoryinfo類獲得目錄信息和屬性的方法

    C#使用Directoryinfo類獲得目錄信息和屬性的方法

    這篇文章主要介紹了C#使用Directoryinfo類獲得目錄信息和屬性的方法,涉及C#操作目錄的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Unity實(shí)現(xiàn)切割圖集工具

    Unity實(shí)現(xiàn)切割圖集工具

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)切割圖集工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C# WinForm捕獲全局變量異常 SamWang解決方法

    C# WinForm捕獲全局變量異常 SamWang解決方法

    本文將介紹C# WinForm捕獲全局變量異常 SamWang解決方法,需要的朋友可以參考
    2012-11-11

最新評(píng)論

方城县| 宣恩县| 上蔡县| 烟台市| 右玉县| 松潘县| 讷河市| 康乐县| 深水埗区| 泸西县| 乌拉特前旗| 绥中县| 浮梁县| 肇源县| 西青区| 原平市| 尼玛县| 溧水县| 唐海县| 绥化市| 水城县| 黄平县| 乌审旗| 容城县| 丰城市| 安阳县| 乌拉特前旗| 南川市| 漳平市| 松滋市| 横峰县| 彩票| 尼勒克县| 临漳县| 凤凰县| 晋江市| 永和县| 彩票| 南昌县| 蓝田县| 敦化市|