C#利用摄像头拍照功能实现

本文介绍了一个使用 C# 和 AForge 框架实现的摄像头图像捕获程序。该程序通过 VideoCaptureDevice 类从指定摄像头捕获图像,并能够保存到本地。此外,还实现了基本的开始和停止摄像头的功能。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

using AForge;
using AForge.Video;
using AForge.Controls;
using AForge.Video.DirectShow;
using AForge.Video.FFMPEG;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.IO;
namespace Camera
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;
        private bool stopREC = true;
        private bool createNewFile = true;

        private string videoFileFullPath = string.Empty; //视频文件全路径
        private string imageFileFullPath = string.Empty; //图像文件全路径
        private string videoPath = @"E:\video\"; //视频文件路径

        private string videoFileName = string.Empty; //视频文件名
        private string imageFileName = string.Empty; //图像文件名
        private string drawDate = string.Empty;
        private VideoFileWriter videoWriter = null;

        public delegate void MyInvoke(); //定义一个委托方法

        string g_s_AutoSavePath = AppDomain.CurrentDomain.BaseDirectory + "Capture\\";
        object objLock = new object(); //定义一个对象的锁
        int frameRate = 20; //默认帧率
        private Stopwatch stopWatch = null;
        IVideoSource iVideoSource = null;
        private int flag = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // 枚举所有视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

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

                foreach (FilterInfo device in videoDevices)
                {
                    tscbxCameras.Items.Add(device.Name);
                }

                tscbxCameras.SelectedIndex = 0;
            }
            catch (ApplicationException)
            {
                tscbxCameras.Items.Add("No local capture devices");
                videoDevices = null;
            }
        }

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

        private void CameraConn()
        {
            videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
            videoSource.DesiredFrameSize = new Size(320, 240);
            videoSource.DesiredFrameRate = 1;

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

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            videoPlayer.SignalToStop();
            videoPlayer.WaitForStop();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            toolStripButton2_Click(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                flag = 0;
                videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("捕获图像失败!" + ex.Message, "提示");
            }
        }
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
            if (flag == 0)
            {
                string img = "E://img//" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                bitmap.Save(img);
                MessageBox.Show("ok");
                flag = 1;
            }
        }

       
    }
}

  具体:1.调用各个dll文件

              2.AForge.Controls生成自身控件videoPlayer

转载于:https://www.cnblogs.com/wangzhenghua/p/4481069.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值