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

C# WPF使用AForge類庫(kù)操作USB攝像頭拍照并保存

 更新時(shí)間:2022年03月27日 08:20:57   作者:曉風(fēng)-楊柳  
這篇文章主要為大家詳細(xì)介紹了C# WPF使用AForge類庫(kù)操作USB攝像頭拍照并保存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

項(xiàng)目中用到 USB 攝像頭,需要根據(jù)情況進(jìn)行圖像抓拍,查了半天資料,比較多的是使用 WPFMediaKit 和 AForge 。
但是由于項(xiàng)目要求不顯示 USB 攝像頭拍攝的畫面,最終確定使用 AForge 解決。
下面用一個(gè)測(cè)試程序記錄一下。

一、無預(yù)覽拍照

首先建立一個(gè) WPF 項(xiàng)目,我的就叫 AForgeTest,你們隨意就好:

然后在 NuGet 包管理器中安裝 AForge 庫(kù):

我只安裝了圖中打勾的幾個(gè)庫(kù),這個(gè)根據(jù)自己項(xiàng)目需要安裝就好。
不過用 USB 攝像頭拍照必須安裝:
AForge.Video
AForge.Control
AForge.Video.DirectShow
這三個(gè)庫(kù)文件。

不習(xí)慣使用 NuGet 也可以到 AForge 的 .NET lib 下載頁(yè)面下載。

在 MainWindow.xaml 文件中添加兩個(gè)按鈕:

<Window x:Class="AForgeTest.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:local="clr-namespace:AForgeTest"
  mc:Ignorable="d"
  Title="MainWindow" Height="300" Width="300"
  Closing="Window_Closing">
 <StackPanel>
  <Button Name="btnCapture" Click="btnCapture_Click">拍照</Button>
  <Button Name="btnOpenCamera" Click="btnOpenCamera_Click">打開</Button>
 </StackPanel>
</Window>

后臺(tái)交互邏輯如下:

using System;
using System.Windows;

namespace AForgeTest
{
 /// <summary>
 /// MainWindow.xaml 的交互邏輯
 /// </summary>
 public partial class MainWindow : Window
 {
  public MainWindow()
  {
   InitializeComponent();
  }

  private void btnOpenCamera_Click(object sender, EventArgs e)
  {
   CameraHelper.UpdateCameraDevices();
   if (CameraHelper.CameraDevices.Count > 0)
   {
    CameraHelper.SetCameraDevice(0);
   }
  }

  private void btnCapture_Click(object sender, EventArgs e)
  {
   CameraHelper.CaptureImage(@"E:\1");
  }

  private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   CameraHelper.CloseDevice();
  }
 }
}

CameraHelper 類代碼如下:

using System;
using AForge.Video.DirectShow;
using AForge.Controls;
using System.Windows;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace AForgeTest
{
 public static class CameraHelper
 {
  private static FilterInfoCollection _cameraDevices;
  private static VideoCaptureDevice div = null;
  private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();
  private static bool _isDisplay = false;
  //指示_isDisplay設(shè)置為true后,是否設(shè)置了其他的sourcePlayer,若未設(shè)置則_isDisplay重設(shè)為false
  private static bool isSet = false;

  /// <summary>
  /// 獲取或設(shè)置攝像頭設(shè)備,無設(shè)備為null
  /// </summary>
  public static FilterInfoCollection CameraDevices
  {
   get
   {
    return _cameraDevices;
   }
   set
   {
    _cameraDevices = value;
   }
  }
  /// <summary>
  /// 指示是否顯示攝像頭視頻畫面
  /// 默認(rèn)false
  /// </summary>
  public static bool IsDisplay
  {
   get { return _isDisplay; }
   set { _isDisplay = value; }
  }
  /// <summary>
  /// 獲取或設(shè)置VideoSourcePlayer控件,
  /// 只有當(dāng)IsDisplay設(shè)置為true時(shí),該屬性才可以設(shè)置成功
  /// </summary>
  public static VideoSourcePlayer SourcePlayer
  {
   get { return sourcePlayer; }
   set
   {
    if (_isDisplay)
    {
     sourcePlayer = value;
     isSet = true;
    }

   }
  }
  /// <summary>
  /// 更新攝像頭設(shè)備信息
  /// </summary>
  public static void UpdateCameraDevices()
  {
   _cameraDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  }
  /// <summary>
  /// 設(shè)置使用的攝像頭設(shè)備
  /// </summary>
  /// <param name="index">設(shè)備在CameraDevices中的索引</param>
  /// <returns><see cref="bool"/></returns>
  public static bool SetCameraDevice(int index)
  {
   if (!isSet) _isDisplay = false;
   //無設(shè)備,返回false
   if (_cameraDevices.Count <= 0 || index < 0) return false;
   if (index > _cameraDevices.Count - 1) return false;
   // 設(shè)定初始視頻設(shè)備
   div = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
   sourcePlayer.VideoSource = div;
   div.Start();
   sourcePlayer.Start();
   return true;
  }
  /// <summary>
  /// 截取一幀圖像并保存
  /// </summary>
  /// <param name="filePath">圖像保存路徑</param>
  /// <param name="fileName">保存的圖像文件名</param>
  public static void CaptureImage(string filePath, string fileName = null)
  {
   if (sourcePlayer.VideoSource == null) return;
   if (!Directory.Exists(filePath))
   {
    Directory.CreateDirectory(filePath);
   }
   try
   {
    //sourcePlayer.Start();
    Image bitmap = sourcePlayer.GetCurrentVideoFrame();
    if(fileName == null) fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
    bitmap.Save(filePath + @"\" + fileName + "-cap.jpg", ImageFormat.Jpeg);
    bitmap.Dispose();
    //sourcePlayer.Stop();
   }
   catch (Exception e)
   {
    MessageBox.Show(e.Message.ToString());
   }
  }
  /// <summary>
  /// 關(guān)閉攝像頭設(shè)備
  /// </summary>
  public static void CloseDevice()
  {
   if (div != null && div.IsRunning)
   {
    sourcePlayer.Stop();
    div.SignalToStop();
    div = null;
    _cameraDevices = null;
   }
  }
 }
}

最終效果如下:

首先單擊打開按鈕,然后單擊拍照按鈕,就會(huì)在指定路徑下生成一個(gè) jpg 文件。

單擊打開按鈕之后需要等待 2s 以上才能點(diǎn)擊拍照(需要等待連接到攝像頭),否則會(huì)報(bào)出異常,如下圖:

二、顯示攝像頭拍攝畫面和截取的圖片

首先添加 System.Windows.Forms 和 WindowsFormsIntegration 的引用。
然后在 MainWindows.xmal 文件中加命名空間:

xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"12

添加 VideoSourcePlayer 和 Image 控件:

<wfi:WindowsFormsHost Grid.Row="0">
 <aforge:VideoSourcePlayer x:Name="player" Height="480" Width="640"/>
</wfi:WindowsFormsHost>
<Image Grid.Row="0" Grid.Column="1" Name="imgCapture" Stretch="Fill" Height="480" Width="640"/>

這里有個(gè)小細(xì)節(jié),注意對(duì) VideoSourcePlayer 命名時(shí),一定要使用 x:Name 不要省略 x: ,否則無法在后臺(tái)代碼中使用(不要問我是怎么知道的)。

對(duì) CameraHelper.cs 中的 CaptureImage 函數(shù)做一點(diǎn)修改:

/// <summary>
/// 截取一幀圖像并保存
/// </summary>
/// <param name="filePath">圖像保存路徑</param>
/// <param name="fileName">保存的圖像文件名</param>
/// <returns>如果保存成功,則返回完整路徑,否則為 null</returns>
 public static string CaptureImage(string filePath, string fileName = null)
  {
   if (sourcePlayer.VideoSource == null) return null;
   if (!Directory.Exists(filePath))
   {
    Directory.CreateDirectory(filePath);
   }
   try
   {
    Image bitmap = sourcePlayer.GetCurrentVideoFrame();
    if(fileName == null) fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
    string fullPath = Path.Combine(filePath, fileName + "-cap.jpg");
    bitmap.Save(fullPath, ImageFormat.Jpeg);
    bitmap.Dispose();
    return fullPath;
   }
   catch (Exception e)
   {
    MessageBox.Show(e.Message.ToString());
    return null;
   }
}

修改后臺(tái)代碼如下:

using System;
using System.Windows;
using System.Windows.Media.Imaging;

namespace AForgeTest
{
 /// <summary>
 /// MainWindow.xaml 的交互邏輯
 /// </summary>
 public partial class MainWindow : Window
 {
  public MainWindow()
  {
   InitializeComponent();
   CameraHelper.IsDisplay = true;
   CameraHelper.SourcePlayer = player;
   CameraHelper.UpdateCameraDevices();
  }

  private void btnOpenCamera_Click(object sender, EventArgs e)
  {
   if (CameraHelper.CameraDevices.Count > 0)
   {
    CameraHelper.SetCameraDevice(0);
   }
  }

  private void btnCapture_Click(object sender, EventArgs e)
  {
   string fullPath = CameraHelper.CaptureImage(AppDomain.CurrentDomain.BaseDirectory + @"\Capture");

   BitmapImage bit = new BitmapImage();
   bit.BeginInit();
   bit.UriSource = new Uri(fullPath);
   bit.EndInit();
   imgCapture.Source = bit;
  }

  private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  {
   CameraHelper.CloseDevice();
  }
 }
}

最終結(jié)果如下:

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

相關(guān)文章

  • C#身份證號(hào)碼驗(yàn)證是否正確

    C#身份證號(hào)碼驗(yàn)證是否正確

    這一篇關(guān)于C#語(yǔ)言驗(yàn)證18位身份證號(hào)碼的驗(yàn)證方法和實(shí)例代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • C# 匿名方法基礎(chǔ)回顧

    C# 匿名方法基礎(chǔ)回顧

    本篇文章主要介紹了C#的匿名方法的參數(shù)使用范圍以及委托示例。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • mvc開啟gzip壓縮示例分享

    mvc開啟gzip壓縮示例分享

    這篇文章主要介紹了mvc開啟gzip壓縮示例,需要的朋友可以參考下
    2014-03-03
  • C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解

    這篇文章主要介紹了C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要朋友們參考下。
    2019-08-08
  • C#中跨線程訪問控件的實(shí)現(xiàn)方法

    C#中跨線程訪問控件的實(shí)現(xiàn)方法

    C#中不允許跨線程直接訪問界面控件,即一個(gè)線程中如主線程創(chuàng)建的控件不允許被其他線程例如子線程直接訪問,在一個(gè)線程中設(shè)置其他線程所有的控件屬性通常有兩種方法,本文將詳細(xì)的給大家介紹一下,需要的朋友可以參考下
    2024-12-12
  • C#基礎(chǔ)繼承和多態(tài)詳解

    C#基礎(chǔ)繼承和多態(tài)詳解

    C#基礎(chǔ)繼承和多態(tài)詳解,需要的朋友可以參考一下
    2013-03-03
  • C#程序調(diào)用cmd.exe執(zhí)行命令

    C#程序調(diào)用cmd.exe執(zhí)行命令

    這篇文章介紹了C#程序調(diào)用cmd.exe執(zhí)行命令的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 在C#中如何獲取程序集

    在C#中如何獲取程序集

    某一天我正在寫一些反射代碼,目的是遍歷所有的程序集來查找一個(gè)特定的接口,然后在Startup中調(diào)用其上的一個(gè)方法,這篇文章主要介紹了在C#中如何獲取程序集,需要的朋友可以參考下
    2024-03-03
  • C#生成PDF的方法

    C#生成PDF的方法

    這篇文章主要介紹了C#生成PDF的方法,幫助大家更好的理解和使用c#編程語(yǔ)言,感興趣的朋友可以了解下
    2020-12-12
  • C#子類對(duì)基類方法的繼承、重寫與隱藏詳解

    C#子類對(duì)基類方法的繼承、重寫與隱藏詳解

    這篇文章主要介紹了C#子類對(duì)基類方法的繼承、重寫與隱藏的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

扶风县| 靖州| 雷州市| 白河县| 德安县| 西吉县| 新巴尔虎左旗| 海晏县| 云林县| 社会| 天水市| 舒城县| 鹿泉市| 拉萨市| 青川县| 延吉市| 芮城县| 九龙县| 郸城县| 藁城市| 万州区| 容城县| 三台县| 滁州市| 印江| 平阴县| 泰兴市| 新竹县| 尖扎县| 东明县| 安康市| 安阳县| 平昌县| 兴城市| 绥宁县| 青神县| 永仁县| 五常市| 仙居县| 百色市| 化州市|