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

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;

private void InitUI()
{
//连接
//开启摄像头
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)
{
//开始录像
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", 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);
}
}
}

拍照代码

/// <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, "提示");
}
}

最基本的工作就完成了,如果要更加完美的话还需要细化,优化等等,就先记录到此,以便记录自己的点点滴滴的进步

AForge.net 使用之录像拍照功能实现的更多相关文章

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

    AForge.net 使用之录像拍照功能实现 最近使用aforge.NET拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...

  2. C# - VS2019调用AForge库实现调用摄像头拍照功能

    前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...

  3. WinForm利用AForge.NET调用电脑摄像头进行拍照和视频

    当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...

  4. C#操作摄像头 实现拍照功能

    从正式工作以来一直做的都是基于B/S的Web开发,已经很长时间不研究C/S的东西了,但是受朋友的委托,帮他做一下拍照的这么个小功能.其实类似的代码网上有很多,但是真的能够拿来运行的估计也没几个.本来是 ...

  5. Android开发 Camera2开发_1_拍照功能开发

    介绍 google已经在Android5.1之后取消了对Camera1的更新,转而提供了功能更加强大的Camera2.虽然新版本依然可以使用Camera1但是,不管是各种机型适配还是拍照参数自定义都是 ...

  6. UWP开发之Template10实践二:拍照功能你合理使用了吗?(TempState临时目录问题)

    最近在忙Asp.Net MVC开发一直没空更新UWP这块,不过有时间的话还是需要将自己的经验和大家分享下,以求共同进步. 在上章[UWP开发之Template10实践:本地文件与照相机文件操作的MVV ...

  7. 文件件监听器,android系统拍照功能调用后删除系统生成的照片

    先说说要实现的功能: android调用系统拍照功能实时 预览 删除 上传 保存 (用户不能再本地文件夹中看到拍的照片) 再说说遇到的问题: 1.调用系统拍照在系统自带的拍照文件夹中生成一张随机命名图 ...

  8. ios照片获取,拍照功能

    // //  HYBPhotoPickerManager.h //  ehui // //  Created by 黄仪标 on 14/11/26. //  Copyright (c) 2014年 黄 ...

  9. Android--启动拍照功能并返回结果

    因为没有深入学习拍照这块功能,所以只是简单的调用了一下系统的拍照功能,下面代码: //拍照的方法 private void openTakePhoto(){ /** * 在启动拍照之前最好先判断一下s ...

随机推荐

  1. Windows Phone App Studio发布

    Windows Phone App Studio发布重要更新-支持Windows 8.1 源代码生成 自2013年8月Apps Team发布Windows Phone App Studio以来,由于其 ...

  2. 《剑指Offer》面试题-从头到尾打印链表

    题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例.每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点.第一行是链表第一个节点的值,依次类推 ...

  3. 园 首页 新随笔 联系 管理 订阅 订阅 RTSP协议转换RTMP直播协议

    RTSP协议转换RTMP直播协议 RTSP协议也是广泛使用的直播/点播流媒体协议,最近实现了一个RTSP协议转换RTMP直播协议的程序,为的是可以接收远端设备或服务器的多路RTSP直播数据,实时转换为 ...

  4. [转]Whirlwind Tour of ARM Assembly

    ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...

  5. 图解Javascript之字符串

    好东西分享给大家,但要尊重事实!!!因此特别说明:本图非我本人亲自所作,乃我大天朝网友所绘制.个人感觉此图,覆盖全面,细节考虑甚周全,因此分享给大家,同时在此特别感谢网友的无私分享!

  6. PHP实现一个简单url路由功能

    如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染.很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的 ...

  7. 安装dnvm

    打开powershell,运行:&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.gith ...

  8. 【C#】Smtp发送邮件

    class SmtpEmail { SmtpClient smtpclient; MailMessage msg; Attachment attachment; public void sendMai ...

  9. Entity Framework实体模型 入门视频教程

    Entity Framework实体模型 入门视频教程 恢复内容开始--- 第一步 创建一个 控制台应用程序 第二步 创建一个ADO.NET 数据实体模型 DbModel.edmx 需要跟数据库进行连 ...

  10. centos安装svn

    原文链接:http://blog.csdn.net/liuyuan_jq/article/details/2110814 1.SVN简介由于前些年在版本的管理上采用的都是CVS系统,总体上而言还是很优 ...