C#利用VFW实现摄像头程序
最近在搞这个考试监控,找来VFW的资料,胡编乱凑而成。
VFW全称为Video for Windows,是微软提供的,内嵌windows系统。
首先定义一个VideoAPI类。
首先调用avicap32.dll
[DllImport("avicap32.dll")]
public static extern IntPtr capCreateCaptureWindow(byte[] strWindowName, int dwStyle, int x, int y, int width, int height, IntPtr hwdParent, int nID);
[DllImport("avicap32.dll")]
public static extern bool capGetDriverDescription(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
再调用User32.dll
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);
[DllImport("User32.dll")]
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, string lParam);
再定义一堆消息。
//常量
public const int WM_USER = 0x400;
public const int WS_CHILD = 0x40000000;
public const int WS_VISIBLE = 0x10000000;
public const int SWP_NOMOVE = 0x2;
public const int SWP_NOZORDER = 0x4;
public const int WM_CAP_DRIVER_CONNECT = WM_USER + ;
public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + ;
public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + ;
public const int WM_CAP_SET_PREVIEW = WM_USER + ;
public const int WM_CAP_SET_PREVIEWRATE = WM_USER + ;
public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + ;
public const int WM_CAP_START = WM_USER;
public const int WM_CAP_SAVEDIB = WM_CAP_START + ;
public const int WM_CAP_SET_SCALE = WM_USER + ;
public const int WM_COPYTOCLIPBORAD = WM_USER + ;
public const int WM_CAP_SEQUENCE = WM_USER + ;
public const int WM_CAP_FILE_SET_CAPTURE_FILE = WM_USER + ;
public const int WM_CAP_STOP = WM_USER + ;
定义一个VideoClass,用于调用API,启动摄像头,关闭摄像头,保存图片等。
启动摄像头:
public bool StartWebcam()
{
byte[] lpszName = new byte[];
byte[] lpszVer = new byte[];
VideoAPI.capGetDriverDescription(, lpszName, , lpszVer, ); caphwnd = VideoAPI.capCreateCaptureWindow(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, , , this.width, this.height, this.controlhwnd, ); if (caphwnd == null)
{
return false;
} bool isconnect = VideoAPI.SendMessage(caphwnd,VideoAPI.WM_CAP_DRIVER_CONNECT,,);
if(isconnect == false)
{
VideoAPI.CloseHandle(caphwnd);
return false;
} if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEWRATE, , ) == false)
{
return false;
} if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEW, true, ) == false)
{
return false;
} if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_SCALE, , ) == false)
{
return false;
} isstart = true;
return true;
}
关闭摄像头:
/// <summary>
/// 关闭视频设备
/// </summary>
/// <returns></returns>
public bool StopWebcam()
{
if (caphwnd != null)
{
isstart = false;
return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_DRIVER_DISCONNECT, , );
}
else
return false;
}
截图:
public bool GrabImage(IntPtr hWndC, string path)
{
if (caphwnd != null)
return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SAVEDIB, , path);
else
return false;
} public bool SaveImage(string path)
{
return GrabImage(this.caphwnd, path);
}
截图并转换为jpg:
public void CopyToClipBorad()
{
VideoAPI.SendMessage(caphwnd, VideoAPI.WM_COPYTOCLIPBORAD, , );
} public System.Drawing.Image getCaptureImage()
{
System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
System.Drawing.Image retImage = null;
if (iData != null)
{
if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
{
retImage = (System.Drawing.Image)iData.GetData(System.Windows.Forms.DataFormats.Bitmap);
}
else if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Dib))
{
retImage = (System.Drawing.Image)iData.GetData(System.Windows.Forms.DataFormats.Dib);
}
}
return retImage;
}
在调用类中写:
public void capture(string strname)
{
vc.CopyToClipBorad();
System.Drawing.Image img = vc.getCaptureImage();
img.Save(strname, System.Drawing.Imaging.ImageFormat.Jpeg);
}
附上视频类和控件:http://files.cnblogs.com/qiu2013/MyWebcam.zip
C#利用VFW实现摄像头程序的更多相关文章
- Jenkins简明入门(二) -- 利用Jenkins完成Python程序的build、test、deployment
大家可能还没搞清楚,Jenkins到底能做什么? 本节内容利用Jenkins完成python程序的build.test.deployment,让大家对Jenkins能做的事情有一个直观的了解. 本节内 ...
- Python 3 利用 Dlib 实现摄像头实时人脸检测和平铺显示
1. 引言 在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工:这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取: 单个/多个人脸检测,并依次在摄像头窗口,实时平铺显示检测到的 ...
- 编写高质量代码改善C#程序的157个建议——建议156:利用特性为应用程序提供多个版本
建议156:利用特性为应用程序提供多个版本 基于如下理由,需要为应用程序提供多个版本: 应用程序有体验版和完整功能版. 应用程序在迭代过程中需要屏蔽一些不成熟的功能. 假设我们的应用程序共有两类功能: ...
- 利用msfvenom制作木马程序(你可以得到她的一切)
实验环境: 虚拟机:kali (攻击机) 192.168.1.2 虚拟机:windwos 2003 (靶机) 192.168.1.100 1. 制作木马 说明: -p payl ...
- C# 利用AForge进行摄像头信息采集
概述 AForge.NET是一个专门为开发者和研究者基于C#框架设计的,提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,机器人 ...
- 利用manifest文件对程序目录下的dll进行分类
1 背景 对于大部分的券商和机构投资者,只能通过有交易所交易系统接入资质的券商提供的柜台系统来进行现货交易.相对于期货市场,现货市场的柜台系统千差万别,接入协议有明文字符串.二进制数据和FIX协议等, ...
- php利用svn hooks将程序自动发布到测试环境
利用svn hooks将php程序自动发布到测试环境 复制仓库hooks目录下的post-commit.tmpl为post-commit cp post-commit.tmpl post-commit ...
- android利用数字证书对程序签名
签名的必要性 1. 防止你已安装的应用被恶意的第三方覆盖或替换掉. 2. 开发者的身份标识,签名可以防止抵赖等事件的发生. 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同 ...
- android 利用数字证书对程序签名
签名的必要性 1. 防止你已安装的应用被恶意的第三方覆盖或替换掉. 2. 开发者的身份标识,签名可以防止抵赖等事件的发生. 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同样 ...
随机推荐
- Real VNC软件
RealVNC5.2.3+key http://yunpan.cn/cjchAkeIgEAPG (提取码:4092)
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...
- Awt & Swing
AWT 是抽象窗口组件工具包,是 java 最早的用于编写图形节目应用程序的开发包. Swing 是为了解决 AWT 存在的问题而新开发的包,它以 AWT 为基础的. 具体的说就是: AWT 是Abs ...
- 第8条:覆盖equals时请遵守通用约定
第8条:覆盖equals时请遵守通用约定 引言:尽管Object是一个具体类,但是设计它主要是为了拓展.它所有的非final方法(equals.hashCode.toString.clone和fina ...
- 修改第三方库内容,carsh提示"image not found"
在图示位置把提示的东西加上即可 参考: iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta ...
- JavaScript: window.onload = function() {} 里面的函数不执行
问题:写了一个最简单的页面.在script标签中使用的 window.onload = function() { function add() { //... } } 页面上:<div oncl ...
- Percona-Tookit工具包之pt-pmp
Preface Sometimes we need to know the details of a program(porcess) when it is running even ...
- Mac中Mysql开启远程访问(不同于linux直接改配置文件)
在mac中安装Mysql Workbench 用root用户连上安装的Mysql. 开启远程访问的服务 如下图可以看到是root用户绑定的是localhost 如果不做修改的话,直接访问是访问不了 ...
- 正则(re 模块)
就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹 ...
- js三目运算符执行多个条件
三元运算符的结果语句可以执行多个操作,每个操作用逗号分隔就可以,例子如下: var a=1: a>5?(alert(1),alert(2)):(alert(3),alert(4))