C# AForgeでカメラ情報取得!基本と実装

AForgeを使用してカメラ情報を収集するには、まずAForge.NET Frameworkをインストールする必要があります。その後、以下のステップに従ってC#コードを使用してカメラ情報を収集することができます。

  1. AForgeライブラリを使用する。
using AForge.Video;
using AForge.Video.DirectShow;
  1. カメラデバイスを初期化します。
private VideoCaptureDevice videoSource;

private void InitializeCamera()
{
    FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
    videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
    videoSource.Start();
}
  1. 動画フレーム処理イベントの実装:
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap videoFrame = (Bitmap)eventArgs.Frame.Clone();
    // 在这里对视频帧进行处理,例如显示到窗口上
    pictureBox1.Image = videoFrame;
}
  1. 動画ストリームのキャプチャを開始する:
private void StartCapture()
{
    videoSource.Start();
}
  1. ビデオストリームのキャプチャを停止します。
private void StopCapture()
{
    if (videoSource.IsRunning)
    {
        videoSource.SignalToStop();
        videoSource.WaitForStop();
    }
}

上記の手順を通じて、AForgeを使用してカメラ情報を収集し、ビデオフレームを処理することができます。ビデオフレームの処理イベントメソッドでは、必要に応じてビデオフレームに保存、表示などの操作を行うことができます。

bannerAds