首页 技术 正文
技术 2022年11月16日
0 收藏 651 点赞 3,694 浏览 6402 个字
AForge.net 使用之录像拍照功能实现
最近使用aforge.NET拍照录像功能实现记录一下以便以后好学习,哈哈,直接上代码连接摄像头设备,这里需要引入AForge.Video;AForge.Video.DirectShow;AForge.Video.FFMPEG;还需要添加引用,aforge.dll,aforge.control,在工具箱中还需要添加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 = ; //默认帧率
private Stopwatch stopWatch = null;
IVideoSource iVideoSource = null;复制代码
private void InitUI()
{
//连接 //开启摄像头
videoDevices = vh.GetDevices();
if (videoDevices != null && videoDevices.Count > )
{
videoSource = vh.VideoConnect();
}
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}
复制代码
开始录像复制代码
private void btnStartVideotape_Click(object sender, EventArgs e)
{
//开始录像
if (btnStartVideotape.Text == "开始录像")
{
stopREC = false;
frameRate = Convert.ToInt32(txtFrameRate.Text.Trim());
btnStartVideotape.Text = "停止录像";
}
else if (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 = new Font("Arial", , FontStyle.Bold, GraphicsUnit.Millimeter);
int xPos = image.Width - (image.Width - );
int yPos = ;
//写到屏幕上的时间
drawDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);
if (!Directory.Exists(videoPath))
Directory.CreateDirectory(videoPath); //创建文件路径
//fileFullPath = path + fileName; if (stopREC)
{
stopREC = true;
createNewFile = true; //这里要设置为true表示要创建新文件
if (videoWriter != null)
videoWriter.Close();
}
else
{
//开始录像
if (createNewFile)
{
videoFileName = DateTime.Now.ToString("yyyy.MM.dd HH.mm.ss") + ".avi";
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);
}
}
}
复制代码
拍照代码复制代码
/// <summary>
/// 手动拍照或抓图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCapture_Click(object sender, EventArgs e)
{
try
{
int number=;
number++;
string fileImageName = g_s_RequestNo + "-" + number + ".bmp";
string fileCapturePath = g_s_AutoSavePath + g_s_RequestNo + "\\";
if (!Directory.Exists(fileCapturePath))
Directory.CreateDirectory(fileCapturePath); //抓到图保存到指定路径
Bitmap bmp = null;
bmp = videoSourcePlayer1.GetCurrentVideoFrame();
if (bmp == null)
{
MessageBox.Show("捕获图像失败!", "提示");
return;
} bmp.Save(fileCapturePath + fileImageName, ImageFormat.Bmp); }
catch (Exception ex)
{
MessageBox.Show("捕获图像失败!" + ex.Message, "提示");
}
}

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

 

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

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

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

AForge.Video;

AForge.Video.DirectShow;

AForge.Video.FFMPEG;

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

在工具箱中还需要添加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;

AForge.net  录像拍照功能实现 转

private void InitUI()
{
//连接
//开启摄像头
videoDevices = vh.GetDevices();
if (videoDevices != null && videoDevices.Count > 0)
{
videoSource = vh.VideoConnect();
}
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}

AForge.net  录像拍照功能实现 转

开始录像

AForge.net  录像拍照功能实现 转

 private void btnStartVideotape_Click(object sender, EventArgs e)
{
//开始录像
if (btnStartVideotape.Text == "开始录像")
{
stopREC = false;
frameRate = Convert.ToInt32(txtFrameRate.Text.Trim());
btnStartVideotape.Text = "停止录像";
}
else if (btnStartVideotape.Text == "停止录像")
{
stopREC = true;
btnStartVideotape.Text = "开始录像";
}
}

AForge.net  录像拍照功能实现 转

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

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

AForge.net  录像拍照功能实现 转

private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
//录像
Graphics g = Graphics.FromImage(image);
SolidBrush drawBrush = new SolidBrush(Color.Yellow); Font drawFont = new Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Millimeter);
int xPos = image.Width - (image.Width - 15);
int yPos = 10;
//写到屏幕上的时间
drawDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);
if (!Directory.Exists(videoPath))
Directory.CreateDirectory(videoPath); //创建文件路径
//fileFullPath = path + fileName; if (stopREC)
{
stopREC = true;
createNewFile = true; //这里要设置为true表示要创建新文件
if (videoWriter != null)
videoWriter.Close();
}
else
{
//开始录像
if (createNewFile)
{
videoFileName = DateTime.Now.ToString("yyyy.MM.dd HH.mm.ss") + ".avi";
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  录像拍照功能实现 转

拍照代码

AForge.net  录像拍照功能实现 转

/// <summary>
/// 手动拍照或抓图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCapture_Click(object sender, EventArgs e)
{
try
{
int number=0;
number++;
string fileImageName = g_s_RequestNo + "-" + number + ".bmp";
string fileCapturePath = g_s_AutoSavePath + g_s_RequestNo + "\\";
if (!Directory.Exists(fileCapturePath))
Directory.CreateDirectory(fileCapturePath); //抓到图保存到指定路径
Bitmap bmp = null;
bmp = videoSourcePlayer1.GetCurrentVideoFrame();
if (bmp == null)
{
MessageBox.Show("捕获图像失败!", "提示");
return;
} bmp.Save(fileCapturePath + fileImageName, ImageFormat.Bmp); }
catch (Exception ex)
{
MessageBox.Show("捕获图像失败!" + ex.Message, "提示");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856