AForge.net 使用之录像拍照功能实现

最近使用aforge.net拍照录像功能实现

记录一下以便以后好学习,哈哈,直接上代码

连接摄像头设备,这里需要引入

AForge.Video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

还需要添加引用,aforge.dll,aforge.video,aforge.video.directshow,aforge.control,aforge.video.ffmpeg,

在工具箱中还需要添加AForge.Control,然后找到VideoSourcePlayer这个控件添加到界面上

然后定义变量

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 imagePath = @"E:\video\images\&;; //图像文件路径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 void InitUI(){ (msc.OpenPort()){msc.ConnectMicroScopeControl();//开启摄像头videoDevices = vh.GetDevices();if (videoDevices != null && videoDevices.Count > 0){videoSource = vh.VideoConnect();}videoSourcePlayer1.VideoSource = videoSource;videoSourcePlayer1.Start();}}

开始录像

private void btnStartVideotape_Click(object sender, EventArgs e){(btnStartVideotape.Text == ){stopREC = false;frameRate = Convert.ToInt32(txtFrameRate.Text.Trim());btnStartVideotape.Text = ;}){stopREC = true;btnStartVideotape.Text = ;}}

添加aforge.net的一个VideoSourcePlayer控件之后找到NewFrame事件,代码如下:

下面是控件的一个事件,是真正录像的代码

private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image){//录像Graphics g = Graphics.FromImage(image);SolidBrush drawBrush = new SolidBrush(Color.Yellow);Font drawFont = , 6, FontStyle.Bold, GraphicsUnit.Millimeter);int xPos = image.Width – (image.Width – 15);int yPos = 10;//写到屏幕上的时间drawDate = DateTime.Now.ToString();g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);if (!Directory.Exists(videoPath))Directory.CreateDirectory(videoPath);//创建文件路径 (stopREC){stopREC = true;createNewFile = (videoWriter != null)videoWriter.Close();}else{ (createNewFile){videoFileName = DateTime.Now.ToString() + ;videoFileFullPath = videoPath + videoFileName;createNewFile = false;if (videoWriter != null){videoWriter.Close();videoWriter.Dispose();}videoWriter = new VideoFileWriter();//这里必须是全路径,否则会默认保存到程序运行根据录下了videoWriter.Open(videoFileFullPath, image.Width, image.Height, frameRate, VideoCodec.MPEG4);videoWriter.WriteVideoFrame(image);}else{videoWriter.WriteVideoFrame(image);}}}

拍照代码

劝君更尽一杯酒,西出阳关无故人。

AForge.net 使用之录像拍照功能实现

相关文章:

你感兴趣的文章:

标签云: