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

C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼

 更新時(shí)間:2020年09月20日 11:11:25   作者:svq18656  
這篇文章主要介紹了C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

老師要求我們學(xué)生做一套拍照身份驗(yàn)證系統(tǒng),經(jīng)過(guò)長(zhǎng)時(shí)間的學(xué)習(xí),有了這篇文章,希望能幫到讀者們。

正文

首先介紹本文的主角:AForge
創(chuàng)建一個(gè)C#項(xiàng)目,引用必備的幾個(gè)DLL

  • AForge.dll
  • AForge.Controls.dll
  • AForge.Imaging.dll
  • AForge.Math.dll
  • AForge.Video.DirectShow.dll
  • AForge.Video.dll

這些DLL讀者們可以在文末下載我附帶的Demon

引用必要的命名空間

using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;

至此,便可以開(kāi)始編寫(xiě)代碼了。

首先遍歷操作系統(tǒng)上的攝像頭控件:

public static bool GetDevices()
    {
      try
      {
        //枚舉所有視頻輸入設(shè)備
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        if (videoDevices.Count != 0)
        {
          Console.WriteLine("已找到視頻設(shè)備.");
          return true;
        }

        return false;
      }
      catch (Exception ex)
      {
        Console.WriteLine("error:沒(méi)有找到視頻設(shè)備!具體原因:" + ex.Message);
        return false;
      }

    }

找到控件后就可以初始化攝像頭:

private static void CameraConn()
    {
      videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);
      vid.VideoSource = videoSource;
      vid.Start();
    }

但是這里為止,都只是攝像拍攝,如果需要拍照,則需要通過(guò)eventArgs.Frame.Clone()截取視頻中的某一幀圖像
這里就需要通過(guò)事件來(lái)處理:

public static void GrabBitmap()
    {
      if (videoSource == null)
      {
        return;
      }
      videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame); //新建事件
    }

    static void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
      Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();  //Clone攝像頭中的一幀
      bmp.Save(path, ImageFormat.Png);
      videoSource.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame);    //如果這里不寫(xiě)這個(gè),一會(huì)兒會(huì)不停的拍照,
    }

代碼中的path變量就是圖片保存的位置,讀者們可以自行設(shè)置路徑。我這里默認(rèn)是用戶桌面下的Temp.png文件

測(cè)試代碼下載地址:https://gitee.com/GiveCVE/csharp_camera/raw/master/OpenCamera.zip

到此這篇關(guān)于C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼的文章就介紹到這了,更多相關(guān)C#調(diào)用攝像頭拍照內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論