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;

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.Video; AForge.Video.DirectShow; AForge.Video.FFMPEG; 还需要添加引用,aforge.dll,aforge ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. Cocos2d-x使用android拍照功能加载照片内存过大,通过另存照片尺寸大小解决

    使用2dx调用android拍照功能,拍照结束后在2dx界面显示拍照照片,如果不对照片做处理,会出现内存过大的问题,导致程序崩溃,如果仅仅另存拍照照片,则照片质量大小均下降,导致照片不够清晰,后来发现 ...

随机推荐

  1. Idea的基本介绍

    Idea的基本介绍 Idea一般是作为一个Java和Scala的开发工具来使用的,它的使用方法和Eclipse有一些不同,这里记录以下一些基本点. 1.创建项目 创建一个新项目的时候,用户必须选择一个 ...

  2. web页面在ios下不支持fixed可用absolute替代的方案

    本文引用出处:http://www.cnblogs.com/PeunZhang/p/3553020.html. 对于 ios4 和 android2.2 以下不支持 position:fixed 的问 ...

  3. java基础之JDBC九:DbUtils的简介及使用

    DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能. 使用步骤: A: 获取可以执行SQL语句的对象. pu ...

  4. codeforce 459 DIV2 D题

    题意   在一个DAG上面有N个点M条边,每一条边上都有一个小写字母.两个人Max and Lucas 每个人一颗棋子,两个人轮流行棋,当前这一步选择的路上面的字母必须大于等于上一步路上面的字母,当轮 ...

  5. Linux 下Nginx 运行Vue

    首相基础的安装Node.js  npm 先建个目录把 /node/www 然后在这个目录下 wget https://nodejs.org/dist/v8.11.1/node-v8.11.1-linu ...

  6. 利用PHPExcel将数据导出到xls格式的excel文件

    在开发某地的经营许可证管理系统的时候需要将数据导出打excel文件,虽然一年前做某集团的ERP的时候用到过一次导入和导出,但是那时候太忙没时间写博客,一年过去了我也忘的差不多了,所以趁着今天将此次的使 ...

  7. c语言实践输出某个区间中不是3的倍数的偶数

    OK,先审题,我们最后要输出的那些数是需要满足两个条件的,第一个条件是,这个数不是3的倍数,第二个条件是这个数是偶数.也就是这样的数需要同时满足这两个条件的时候才把这个数输出. 不是3的倍数这个条件在 ...

  8. 各种RNAseq原理

    RNA Sequencing Methods Low-Level RNA Detection CEL-Seq CirSeq CLaP CytoSeq Digital RNA Sequencing DP ...

  9. Java 扫描器类 Scanner类

    1.Scanner是SDK1.5新增的一个类,可是使用该类创建一个对象.Scanner reader=new Scanner(System.in); 2.reader对象调用下列方法(函数),读取用户 ...

  10. 《Wonderland: A Novel Abstraction-Based Out-Of-Core Graph Processing System》章明星

    在2018年3月28日于美国弗吉尼亚州威廉斯堡结束的ACM ASPLOS 2018会议上,计算机系高性能所师生发表了两篇长文.一篇是我系博士生章明星为第一作者,导师武永卫为通讯作者的“Wonderla ...