AFORGE 라이브러리 학습의 봉인 카메라 조작

오늘부터 aforge 라이브러리를 사용하여 카메라의 조작을 봉인합니다.관련된 조작은 카메라를 켜고 사진을 찍고 카메라를 끄는 것이다.
나는 이 조작들을 DLL에 봉인했다. 

준비 작업


1, AFORGE 라이브러리를 참조하는 DLL 추가:
AForge.Video.dll;
AForge.Video.DirectShow.dll;
이름 공간 추가
using AForge.Video;
using AForge.Video.DirectShow;

2, 인용 추가: 이것은 시스템에 있는 것으로 인용을 추가합니다.net 아래
system.drawing.dll;

이름 공간을 추가하려면 다음과 같이 하십시오.
using System.Drawing;

------------------DLL 소스

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Webcam;

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private VideoCaptureDevice video;
        private Bitmap bitmap;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// ---      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] videoDevicesName = WebcamHelper.VideoDevicesName();
            foreach (string item in videoDevicesName)
            {
                comboBox1.Items.Add(item);
            }
            comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// ---     
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void connBtn_Click(object sender, EventArgs e)
        {
            video = WebcamHelper.ConnectWebam(comboBox1.SelectedIndex);
            video.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
        }
        ///------     ,            
        private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs)
        {
            bitmap = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = bitmap;
           ///---      
            GC.Collect();
        }

        /// <summary>
        /// ---    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeBtn_Click(object sender, EventArgs e)
        {
            WebcamHelper.DisConnectWebcam(ref video);
        }

        /// <summary>
        /// --      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (video != null)
            {
                ///---       
                if (video.IsRunning)
                {
                    video.Stop();
                }
            }  
        }

        /// <summary>
        /// ---    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void captureBtn_Click(object sender, EventArgs e)
        {
            ///---       
            ///---          。   
            string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".jpg";  

            if (true == WebcamHelper.SaveCapturePicture(ref bitmap, filepath, 200, 100))
            {
                MessageBox.Show("      ");
            }
            else
            {
                MessageBox.Show("      ");
            }
        }  
    }
}

-------------------- DLL 예제 사용


1, 참조 추가: 직접 작성한 DLL(Webcam.dll)
2, 소스:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Webcam;

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

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private VideoCaptureDevice video;
        private Bitmap bitmap;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// ---      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] videoDevicesName = WebcamHelper.VideoDevicesName();
            foreach (string item in videoDevicesName)
            {
                comboBox1.Items.Add(item);
            }
            comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// ---     
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void connBtn_Click(object sender, EventArgs e)
        {
            video = WebcamHelper.ConnectWebam(comboBox1.SelectedIndex);
            video.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
        }
        ///------     ,            
        private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs)
        {
            bitmap = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = bitmap;
           ///---      
            GC.Collect();
        }

        /// <summary>
        /// ---    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeBtn_Click(object sender, EventArgs e)
        {
            WebcamHelper.DisConnectWebcam(ref video);
        }

        /// <summary>
        /// --      
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (video != null)
            {
                ///---       
                if (video.IsRunning)
                {
                    video.Stop();
                }
            }  
        }

        /// <summary>
        /// ---    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void captureBtn_Click(object sender, EventArgs e)
        {
            ///---       
            ///---          。   
            string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".jpg";  

            if (true == WebcamHelper.SaveCapturePicture(ref bitmap, filepath, 200, 100))
            {
                MessageBox.Show("      ");
            }
            else
            {
                MessageBox.Show("      ");
            }
        }  
    }
}

좋은 웹페이지 즐겨찾기