最近在搞这个考试监控,找来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 + 10; public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5; public const int WM_CAP_SET_PREVIEW = WM_USER + 50; public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45; public const int WM_CAP_START = WM_USER; public const int WM_CAP_SAVEDIB = WM_CAP_START + 25; public const int WM_CAP_SET_SCALE = WM_USER + 53; public const int WM_COPYTOCLIPBORAD = WM_USER + 30; public const int WM_CAP_SEQUENCE = WM_USER + 62; public const int WM_CAP_FILE_SET_CAPTURE_FILE = WM_USER + 20; public const int WM_CAP_STOP = WM_USER + 68;
定义一个VideoClass,用于调用API,启动摄像头,关闭摄像头,保存图片等。
启动摄像头:
public bool StartWebcam() { byte[] lpszName = new byte[100]; byte[] lpszVer = new byte[100]; VideoAPI.capGetDriverDescription(0, lpszName, 100, lpszVer, 100); caphwnd = VideoAPI.capCreateCaptureWindow(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, this.width, this.height, this.controlhwnd, 1); if (caphwnd == null) { return false; } bool isconnect = VideoAPI.SendMessage(caphwnd,VideoAPI.WM_CAP_DRIVER_CONNECT,0,0); if(isconnect == false) { VideoAPI.CloseHandle(caphwnd); return false; } if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEWRATE, 66, 0) == false) { return false; } if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_PREVIEW, true, 0) == false) { return false; } if (VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SET_SCALE, 1, 0) == 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, 0, 0); } else return false; }
截图:
public bool GrabImage(IntPtr hWndC, string path) { if (caphwnd != null) return VideoAPI.SendMessage(caphwnd, VideoAPI.WM_CAP_SAVEDIB, 0, path); else return false; } public bool SaveImage(string path) { return GrabImage(this.caphwnd, path); }
截图并转换为jpg:
public void CopyToClipBorad() { VideoAPI.SendMessage(caphwnd, VideoAPI.WM_COPYTOCLIPBORAD, 0, 0); } 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