最近在搞这个考试监控,找来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);
}