实例介绍
【实例简介】API摄像头视频c#源码
【实例截图】
【核心代码】
using System; using System.Runtime.InteropServices; public class VideoAPI { [DllImport("avicap32.dll")] public static extern IntPtr capCreateCaptureWindowA(byte[] IpszWindowName,int dwStyle,int x,int y,int nWidth,int nHeight,IntPtr hWndParent,int nID); [DllImport("avicap32.dll")] public static extern bool capGetDriverDescriptionA(short wDriver,byte[] lpszName,int cbName,byte[] ipszVer,int cbVer); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd,int wMsg,bool wParam,int iparam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd,int wMsg,short wParam,long iparam); #region 消息常量(向窗口发送消息的指令) //消息常量 -------------------------------------------- public const int WM_START = 0x400; //此并非摄像头消息0x400表示的就是1024 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_GET_CAPSTREAMPTR = WM_START 1; public const int WM_CAP_SET_CALLBACK_ERROR = WM_START 2;//设置收回错误 public const int WM_CAP_SET_CALLBACK_STATUS = WM_START 3;//设置收回状态 public const int WM_CAP_SET_CALLBACK_YIELD = WM_START 4;//设置收回出产 public const int WM_CAP_SET_CALLBACK_FRAME = WM_START 5;//设置收回结构 public const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_START 6;//设置收回视频流 public const int WM_CAP_SET_CALLBACK_WAVESTREAM = WM_START 7;//设置收回视频波流 public const int WM_CAP_GET_USER_DATA = WM_START 8;//获得使用者数据 public const int WM_CAP_SET_USER_DATA = WM_START 9;//设置使用者数据 public const int WM_CAP_DRIVER_CONNECT = WM_START 10;//驱动程序连接 public const int WM_CAP_DRIVER_DISCONNECT = WM_START 11;//断开启动程序连接 public const int WM_CAP_DRIVER_GET_NAME = WM_START 12;//获得驱动程序名字 public const int WM_CAP_DRIVER_GET_VERSION = WM_START 13;//获得驱动程序版本 public const int WM_CAP_DRIVER_GET_CAPS = WM_START 14;//获得驱动程序帽子 public const int WM_CAP_FILE_SET_CAPTURE_FILE = WM_START 20;//设置捕获文件 public const int WM_CAP_FILE_GET_CAPTURE_FILE = WM_START 21;//获得捕获文件 public const int WM_CAP_FILE_ALLOCATE = WM_START 22;//分派文件 public const int WM_CAP_FILE_SAVEAS = WM_START 23;//另存文件为 public const int WM_CAP_FILE_SET_INFOCHUNK = WM_START 24;//设置开始文件 public const int WM_CAP_FILE_SAVEDIB = WM_START 25;//保存文件 public const int WM_CAP_EDIT_COPY = WM_START 30;//编辑复制 public const int WM_CAP_SET_AUDIOFORMAT = WM_START 35;//设置音频格式 public const int WM_CAP_GET_AUDIOFORMAT = WM_START 36;//捕获音频格式 public const int WM_CAP_DLG_VIDEOFORMAT = WM_START 41;//1065 打开视频格式设置对话框 public const int WM_CAP_DLG_VIDEOSOURCE = WM_START 42;//1066 打开属性设置对话框,设置对比度亮度等 public const int WM_CAP_DLG_VIDEODISPLAY = WM_START 43;//1067 打开视频显示 public const int WM_CAP_GET_VIDEOFORMAT = WM_START 44;//1068 获得视频格式 public const int WM_CAP_SET_VIDEOFORMAT = WM_START 45;//1069 设置视频格式 public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_START 46;//1070 打开压缩设置对话框 public const int WM_CAP_SET_PREVIEW = WM_START 50;//设置预览 public const int WM_CAP_SET_OVERLAY = WM_START 51;//设置覆盖 public const int WM_CAP_SET_PREVIEWRATE = WM_START 52;//设置预览比例 public const int WM_CAP_SET_SCALE = WM_START 53;//设置刻度 public const int WM_CAP_GET_STATUS = WM_START 54;//获得状态 public const int WM_CAP_SET_SCROLL = WM_START 55;//设置卷 public const int WM_CAP_GRAB_FRAME = WM_START 60;//逮捕结构 public const int WM_CAP_GRAB_FRAME_NOSTOP = WM_START 61;//停止逮捕结构 public const int WM_CAP_SEQUENCE = WM_START 62;//次序 public const int WM_CAP_SEQUENCE_NOFILE = WM_START 63;//使用WM_CAP_SEUENCE_NOFILE消息(capCaptureSequenceNoFile宏),可以不向磁盘文件写入数据。该消息仅在配合回调函数时有用,它允许你的应用程序直接使用音视频数据。 public const int WM_CAP_SET_SEQUENCE_SETUP = WM_START 64;//设置安装次序 public const int WM_CAP_GET_SEQUENCE_SETUP = WM_START 65;//获得安装次序 public const int WM_CAP_SET_MCI_DEVICE = WM_START 66;//设置媒体控制接口 public const int WM_CAP_GET_MCI_DEVICE = WM_START 67;//获得媒体控制接口 public const int WM_CAP_STOP = WM_START 68;//停止 public const int WM_CAP_ABORT = WM_START 69;//异常中断 public const int WM_CAP_SINGLE_FRAME_OPEN = WM_START 70;//打开单一的结构 public const int WM_CAP_SINGLE_FRAME_CLOSE = WM_START 71;//关闭单一的结构 public const int WM_CAP_SINGLE_FRAME = WM_START 72;//单一的结构 public const int WM_CAP_PAL_OPEN = WM_START 80;//打开视频 public const int WM_CAP_PAL_SAVE = WM_START 81;//保存视频 public const int WM_CAP_PAL_PASTE = WM_START 82;//粘贴视频 public const int WM_CAP_PAL_AUTOCREATE = WM_START 83; //自动创造 public const int WM_CAP_PAL_MANUALCREATE = WM_START 84;//手动创造 public const int WM_CAP_SET_CALLBACK_CAPCONTROL = WM_START 85;// 设置收回的错误 #endregion 消息常量 } public class Video { private IntPtr lwndc;//保存无符号句柄 private IntPtr mControlPtr;//保存管理指示器 private int mWidth; private int mHeight; /// <summary> /// 初始化Video /// </summary> /// <param name="handle">对象的handle</param> /// <param name="width">对象的高</param> /// <param name="height">对象的宽</param> public Video(IntPtr handle,int width,int height) { mControlPtr =handle; mWidth =width; mHeight = height; } /// <summary> /// 开启 /// </summary> public void StartVideo() { byte[] ipszName =new byte[100]; byte[] ipszVer = new byte[100]; VideoAPI.capGetDriverDescriptionA(0,ipszName,100,ipszVer,100); this.lwndc=VideoAPI.capCreateCaptureWindowA(ipszName,VideoAPI.WS_CHILD|VideoAPI.WS_VISIBLE,0,0,mWidth,mHeight,mControlPtr,0); if (VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0)) { VideoAPI.SendMessage(this.lwndc, VideoAPI.WM_CAP_SET_OVERLAY, 1, 0);//启用叠加 注:据说启用此项可以加快渲染速度 VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SET_PREVIEWRATE, 30, 0);//设置在PREVIEW模式下设定视频窗口的刷新率 设置每30毫秒显示一帧,即显示帧速为每秒29帧 VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SET_PREVIEW, 1, 0);//设置显示图像启动预览模式 PREVIEW }else { VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_SET_PREVIEW,true,0); } } /// <summary> /// 关闭视频 /// </summary> public void CloseVideo() { VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_DRIVER_DISCONNECT,0,0); } /// <summary> /// 截图 /// </summary> /// <param name="path">保存图片的路径</param> public void GrabImage(String path) { IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_FILE_SAVEDIB,0,hBmp.ToInt64()); } /// <summary> /// 开始录像 /// </summary> /// <param name="PATH">保存文件的路径</param> public void StartKinescope(String PATH) { IntPtr hBmp = Marshal.StringToHGlobalAnsi(PATH); VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_FILE_SET_CAPTURE_FILE,0, hBmp.ToInt64()); VideoAPI.SendMessage(lwndc, VideoAPI.WM_CAP_SEQUENCE, 0, 0); } /// <summary> /// 停止录像 /// </summary> public void Stop() { VideoAPI.SendMessage(lwndc,VideoAPI.WM_CAP_STOP,0,0); } /// <summary> /// 视频压缩 /// </summary> public void setVideoCompression() { VideoAPI.SendMessage(this.lwndc, VideoAPI.WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论