用C#写一个连接相机,拍照并保存图像的界面

如果你也想实现这个好玩的简单界面,就来试试吧(代码并不完善,可以自行修改)

1.界面设置

控件对应在代码中的名字分别是:

2.安装一些包:

(我这里的包太多了,具体看代码用到了哪些,自行安装)

3.代码

注释部分用不到可以忽视

using AForge.Imaging;
using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using AForge.Video;

using static System.Windows.Forms.VisualStyles.VisualStyleElement;



namespace TEST


{

    public partial class Form1 : Form
    {


        public FilterInfoCollection videoDevice;
        Bitmap img;



        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;






        public Form1()
        {
            InitializeComponent();

        }

       
        


       
            //if (temp > 1)
            //{
            //    if (comboBox1.SelectedIndex > 0)
            //    {
            //        comboBox2.SelectedIndex = 0;
            //    }
            //    else
            //    {
            //        comboBox2.SelectedIndex = 1;
            //    }

            //}
        

        //private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    int temp = comboBox2.Items.Count;
        //    if (temp > 1)
        //    {
        //        if (comboBox2.SelectedIndex > 0)
        //        {
        //            comboBox1.SelectedIndex = 0;
        //        }
        //        else
        //        {
        //            comboBox1.SelectedIndex = 1;
        //        }

        //    }
        //}




        //函数的调用
        [Obsolete]
        private void CameraConn()
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoSource.DesiredFrameSize = new Size(320, 240);
            videoSource.DesiredFrameRate = 1;

            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();
        }

        public void CameraDiscon(int num)
        {

            if (num == 1)
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
            }
            //else
            //{

            //    videoSourcePlayer2.SignalToStop();
            //    videoSourcePlayer2.WaitForStop();

            //}

        }

        private void Refresh_ComboBox()
        {

            int temp = 0;

            comboBox1.Items.Clear();
            //comboBox2.Items.Clear();

            try
            {
                videoDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                if (videoDevice.Count == 0)
                    throw new ApplicationException();
                foreach (FilterInfo filter in videoDevice)
                {
                    temp = (temp + 1);

                    comboBox1.Items.Add(Convert.ToString(temp) + " " + filter.Name);
                    //comboBox2.Items.Add(Convert.ToString(temp) + " " + filter.Name);
                }

                //if (temp == 1)
                //{
                //    comboBox2.Items.Clear();
                //    comboBox2.Items.Add("没有找到第二个设备");
                //    comboBox1.SelectedIndex = 0;
                //    comboBox2.SelectedIndex = 0;
                //}
                //if (temp > 1)
                //{
                //    comboBox1.SelectedIndex = 0;
                //    comboBox2.SelectedIndex = 1;
                //}

            }
            catch (Exception)
            {
                comboBox1.Items.Add("没有找到设备");
                //comboBox2.Items.Add("没有找到设备");
                comboBox1.SelectedIndex = 0;
                //comboBox2.SelectedIndex = 0;
                videoDevice = null;

            }

        }




        private void Connect_Click(object sender, EventArgs e)
        {
            CameraConn();
        }

        private void Break_Click(object sender, EventArgs e)
        {
            CameraDiscon(1);
        }

        private void Photo_Click_1(object sender, EventArgs e)
        {
            try
            {
                //以当前时间为文件名,保存为jpg格式
                //图片路径在程序bin目录下的Debug下
                img = videoSourcePlayer1.GetCurrentVideoFrame();
                TimeSpan tss = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long a = Convert.ToInt64(tss.TotalMilliseconds) / 1000;  //以秒为单位
                img.Save(string.Format("{0}.jpg", a.ToString()));
                MessageBox.Show("保存成功!");
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            int temp = comboBox1.Items.Count;
            comboBox1.SelectedIndex = 0;

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            Refresh_ComboBox();

            //初始化按钮状态

            

            try
            {
                // 枚举所有视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                foreach (FilterInfo device in videoDevices)
                {
                    int i = 1;
                    comboBox1.Items.Add(device.Name);



                }

                comboBox1.SelectedIndex = 0;

            }
            catch (ApplicationException)
            {
                comboBox1.Items.Add("没有本地设备");
                videoDevices = null;
            }

        }

        
        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                string file = "C:\\Users\\Administrator\\Desktop\\image2";
                //以当前时间为文件名,保存为jpg格式

              
                TimeSpan tss = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long a = Convert.ToInt64(tss.TotalMilliseconds) / 1000;  //以秒为单位
                string filename = string.Format(file + "\\{0}. jpg", a.ToString());

                pictureBox1.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                MessageBox.Show("保存成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Photo_Click(object sender, EventArgs e)
        {
            img = videoSourcePlayer1.GetCurrentVideoFrame();
            //图片路径在程序bin目录下的Debug下
            pictureBox1.Image = img;
        }
    }
}












4.效果展示

先连接,后拍照,读取的图片会显示在下方的框中,保存图片的路径根据代码中的自行修改即可。

希望对大家有所帮助,相互学习

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值