AForge MultiCamera

来源:互联网 发布:用户mac地址绑定错误 编辑:程序博客网 时间:2024/06/08 01:28

界面

这里写图片描述

部分代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;//using System.IO;using AForge.Video.DirectShow;using AForge.Video;  namespace MultiCamara{    public partial class Form1 : Form    {        public FilterInfoCollection USE_Webcams = null;        public VideoCaptureDevice cam1 = null;        public VideoCaptureDevice cam2 = null;        public VideoCaptureDevice cam3 = null;        public VideoCaptureDevice cam4 = null;        Boolean ifCam1 = false;        Boolean ifCam2 = false;        Boolean ifCam3 = false;        Boolean ifCam4 = false;        public Bitmap bm1 = null;        public Bitmap bm2 = null;        public Bitmap bm3 = null;        public Bitmap bm4 = null;         public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            try            {                textBox1.Text=Application.StartupPath;                USE_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);                if (USE_Webcams.Count > 0)                {                    cam1 = new VideoCaptureDevice(USE_Webcams[0].MonikerString);                                        cam1.NewFrame += new NewFrameEventHandler(Cam_NewFrame1);                    cam1.Start();                    ifCam1 = true;                    btnPic1.Enabled = true;                    btnPicAll.Enabled = true;                    trackBar1.Enabled = true;                    textBoxC.AppendText("摄像头1初始化完毕..."+"\n");                    textBoxC.ScrollToCaret();                }                if (USE_Webcams.Count > 1)                {                    cam2 = new VideoCaptureDevice(USE_Webcams[1].MonikerString);                    cam2.NewFrame += new NewFrameEventHandler(Cam_NewFrame2);                    cam2.Start();                    ifCam2 = true;                    btnPic2.Enabled = true;                    trackBar2.Enabled = true;                    textBoxC.AppendText("摄像头2初始化完毕..." + "\n");                    textBoxC.ScrollToCaret();                }                if (USE_Webcams.Count > 2)                {                    cam3 = new VideoCaptureDevice(USE_Webcams[2].MonikerString);                    cam3.NewFrame += new NewFrameEventHandler(Cam_NewFrame3);                    cam3.Start();                    ifCam3 = true;                    btnPic3.Enabled = true;                    trackBar3.Enabled = true;                    textBoxC.AppendText("摄像头3初始化完毕..." + "\n");                    textBoxC.ScrollToCaret();                }                if (USE_Webcams.Count > 3)                {                    cam4 = new VideoCaptureDevice(USE_Webcams[3].MonikerString);                    cam4.NewFrame += new NewFrameEventHandler(Cam_NewFrame4);                    cam4.Start();                    ifCam4 = true;                    btnPic4.Enabled = true;                    trackBar4.Enabled = true;                    textBoxC.AppendText("摄像头4初始化完毕..." + "\n");                    textBoxC.ScrollToCaret();                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }          }        private void Cam_NewFrame1(object obj, NewFrameEventArgs eventArgs)        {            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();            bm1 = (Bitmap)eventArgs.Frame.Clone();          }        private void Cam_NewFrame2(object obj, NewFrameEventArgs eventArgs)        {            pictureBox2.Image = (Bitmap)eventArgs.Frame.Clone();            bm2 = (Bitmap)eventArgs.Frame.Clone();          }        private void Cam_NewFrame3(object obj, NewFrameEventArgs eventArgs)        {            pictureBox3.Image = (Bitmap)eventArgs.Frame.Clone();            bm3 = (Bitmap)eventArgs.Frame.Clone();          }        private void Cam_NewFrame4(object obj, NewFrameEventArgs eventArgs)        {            pictureBox4.Image = (Bitmap)eventArgs.Frame.Clone();            bm4 = (Bitmap)eventArgs.Frame.Clone();          }        private void btnPic1_Click(object sender, EventArgs e)        {            saveCam1();        }        private void btnPic2_Click(object sender, EventArgs e)        {            saveCam2();        }        private void saveCam1()         {             if (ifCam1 == true)            {                string filepath = textBox1.Text + "\\Cam1-" + DateTime.Now.ToString("yyyyMMdd HH-mm-ss") + ".bmp";                if (Directory.Exists(textBox1.Text))//判断路径是否存在                {                    bm1.Save(filepath);                    textBoxC.AppendText("摄像头1保存:" + filepath + "\n");                    textBoxC.ScrollToCaret();                }                else                {                    MessageBox.Show("Cam1路径不准确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);                }            }         }        private void saveCam2()        {            if (ifCam2 == true)            {                string filepath = textBox2.Text + "\\Cam2-" + DateTime.Now.ToString("yyyyMMdd HH-mm-ss") + ".bmp";                if (Directory.Exists(textBox2.Text))//判断路径是否存在                {                    bm2.Save(filepath);                    textBoxC.AppendText("摄像头2保存:" + filepath + "\n");                    textBoxC.ScrollToCaret();                }                else                {                    MessageBox.Show("Cam2路径不准确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);                }            }        }        private void saveCam3()        {            if (ifCam3 == true)            {                string filepath = textBox3.Text + "\\Cam3-" + DateTime.Now.ToString("yyyyMMdd HH-mm-ss") + ".bmp";                if (Directory.Exists(textBox3.Text))//判断路径是否存在                {                    bm3.Save(filepath);                    textBoxC.AppendText("摄像头3保存:" + filepath + "\n");                    textBoxC.ScrollToCaret();                }                else                {                    MessageBox.Show("Cam3路径不准确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);                }            }        }        private void saveCam4()        {            if (ifCam4 == true)            {                string filepath = textBox4.Text + "\\Cam4-" + DateTime.Now.ToString("yyyyMMdd HH-mm-ss") + ".bmp";                if (Directory.Exists(textBox4.Text))//判断路径是否存在                {                    bm4.Save(filepath);                    textBoxC.AppendText("摄像头4保存:" + filepath + "\n");                    textBoxC.ScrollToCaret();                }                else                {                    MessageBox.Show("Cam4路径不准确!", "错误", MessageBoxButtons.OK,MessageBoxIcon.Error);                }            }        }        private void btnPic3_Click(object sender, EventArgs e)        {            saveCam3();        }        private void btnPic4_Click(object sender, EventArgs e)        {            saveCam4();        }        private void btnPicAll_Click(object sender, EventArgs e)        {            saveCam1();            saveCam2();            saveCam3();            saveCam4();        }        private void btnChoose1_Click(object sender, EventArgs e)        {                        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)            {                textBox1.Text = folderBrowserDialog1.SelectedPath;                textBoxC.AppendText("照片1保存路径改为:" + textBox1.Text + "\n");                textBoxC.ScrollToCaret();            }        }        private void btnChoose2_Click(object sender, EventArgs e)        {            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)            {                textBox2.Text = folderBrowserDialog1.SelectedPath;                textBoxC.AppendText("照片2保存路径改为:" + textBox2.Text + "\n");                textBoxC.ScrollToCaret();            }        }        private void btnChoose3_Click(object sender, EventArgs e)        {            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)            {                textBox3.Text = folderBrowserDialog1.SelectedPath;                textBoxC.AppendText("照片3保存路径改为:" + textBox3.Text + "\n");                textBoxC.ScrollToCaret();            }        }        private void btnChoose4_Click(object sender, EventArgs e)        {            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)            {                textBox4.Text = folderBrowserDialog1.SelectedPath;                textBoxC.AppendText("照片4保存路径改为:" + textBox4.Text + "\n");                textBoxC.ScrollToCaret();            }        }        //调亮度按钮        private void trackBar1_Scroll(object sender, EventArgs e)        {            cam1.SetCameraProperty(CameraControlProperty.Exposure, trackBar1.Value, CameraControlFlags.Manual);        }        private void trackBar2_Scroll(object sender, EventArgs e)        {            cam2.SetCameraProperty(CameraControlProperty.Exposure, trackBar2.Value, CameraControlFlags.Manual);        }        private void trackBar3_Scroll(object sender, EventArgs e)        {            cam3.SetCameraProperty(CameraControlProperty.Exposure, trackBar3.Value, CameraControlFlags.Manual);        }        private void trackBar4_Scroll(object sender, EventArgs e)        {            cam4.SetCameraProperty(CameraControlProperty.Exposure, trackBar4.Value, CameraControlFlags.Manual);        }        //退出按钮        private void btnExit_Click(object sender, EventArgs e)        {            if (ifCam1)            {                cam1.SignalToStop();                cam1.WaitForStop();            }            else if(ifCam2)            {                cam2.SignalToStop();                cam2.WaitForStop();            }            else if (ifCam3)            {                cam3.SignalToStop();                cam3.WaitForStop();            }            else if(ifCam4)            {                cam4.SignalToStop();                cam4.WaitForStop();            }            Hide();            Close();            Dispose();        }    }}

本文简介C#Aforge多摄像头的控制20170927MultiCamera

安装文件AForge.NET Framework-2.2.5.exe

Aforge.Net 是一个 C# 版的图像和计算机视觉库,网站 http://www.aforgenet.com/ 下载安装。