AForge MultiCamera

界面

这里写图片描述

部分代码

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/ 下载安装。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值