这篇文章主要介绍了C#调用AForge实现摄像头录像的示例代码,非常具有实用价值,需要的朋友可以参考下
这篇文章主要介绍了C#调用AForge实现摄像头录像的示例代码,非常具有实用价值,需要的朋友可以参考下
这篇文章主要介绍了C#调用AForge实现摄像头录像的示例代码,非常具有实用价值,需要的朋友可以参考下
1:首先下载库文件>>
下载本教程全代码>>
输出为mp4需要用到ffmpeg相关的文件,我打包的库已经带了,去官网找的库可以在这个目录找到:

2:添加这些引用:

3:两个全局变量:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
//用来操作摄像头 private videocapturedevice camera = null; //用来把每一帧图像编码到视频文件 private videofilewriter videooutput = new videofilewriter(); 开始代码: //获取摄像头列表var devs = new filterinfocollection(filtercategory.videoinputdevice);//实例化设备控制类(我选了第1个)camera = new videocapturedevice(devs[0].monikerstring);//配置录像参数(宽,高,帧率,比特率等参数)videocapabilities这个属性会返回摄像头支持哪些配置,从这里面选一个赋值接即可,我选了第1个camera.videoresolution = camera.videocapabilities[0];//设置回调,aforge会不断从这个回调推出图像数据camera.newframe += camera_newframe;//打开摄像头camera.start();//打开录像文件(如果没有则创建,如果有也会清空),这里还有关于videooutput.open("e:/video.mp4", camera.videoresolution.framesize.width, camera.videoresolution.framesize.height, camera.videoresolution.averageframerate, videocodec.mpeg4, camera.videoresolution.bitcount);给aforge输出图像数据的回调方法://图像缓存private bitmap bmp = new bitmap(1, 1);//摄像头输出回调private void camera_newframe(object sender, newframeeventargs eventargs){ //写到文件 videooutput.writevideoframe(eventargs.frame); lock (bmp) { //释放上一个缓存 bmp.dispose(); //保存一份缓存 bmp = eventargs.frame.clone() as bitmap; }} |
结束代码:
|
1
2
3
4
5
|
//停摄像头camera.stop();//关闭录像文件,如果忘了不关闭,将会得到一个损坏的文件,无法播放videooutput.close(); |
4:修改app.config,兼容net2.0的一些东西:

|
1
2
3
4
5
6
7
|
<?xml version="1.0" encoding="utf-8"?><configuration> <startup uselegacyv2runtimeactivationpolicy="true"> <supportedruntime version="v4.0" sku=".netframework,version=v4.5"/> </startup> <supportedruntime version="v2.0.50727"/></configuration> |
不然会这样:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持米米素材网。
原文链接:http://www.cnblogs.com/DragonStart/p/7563351.html?utm_source=tuicool&utm_medium=referral
发表评论