/// <summary> /// 从视频数据放回一个Bmp /// </summary> /// <param name="data"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> //public static Bitmap GetBitmapFromCallBackData(byte[] data, int width, int height) //{ // Bitmap bit = new Bitmap(width, height, PixelFormat.Format24bppRgb); // BitmapData bitdata = bit.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // unsafe // { // byte* p = (byte*)(void*)bitdata.Scan0; // for (int i = 0; i < bit.Height; i++) // { // for (int k = 0; k < bitdata.Stride; k++) // { // int index = (bit.Height - (i + 1)) * bitdata.Stride + k; // p[0] = data[index]; // p++; // } // } // } // bit.UnlockBits(bitdata); // return bit; //} // start the web camera public void StartWebCam() { byte[] lpszName = new byte[100]; byte[] lpszVer = new byte[100]; ShowVideo.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100); this.lwndC = ShowVideo.capCreateCaptureWindowA(lpszName, ShowVideo.WS_CHILD | ShowVideo.WS_VISIBLE, 0, 0, mWidth, mHeight, this.mControlPtr, 0); if (this.capDriverConnect(this.lwndC, 0)) { //有这句,图像才会和mWidth, mHeight相适应 ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_SET_SCALE, 1, 0); this.capPreviewRate(this.lwndC, 66); this.capPreview(this.lwndC, true); //ShowVideo.BITMAPINFO bitmapinfo = new ShowVideo.BITMAPINFO(); //bitmapinfo.bmiHeader.biSize = ShowVideo.SizeOf(bitmapinfo.bmiHeader); //bitmapinfo.bmiHeader.biWidth = 353; //bitmapinfo.bmiHeader.biHeight = 288; //bitmapinfo.bmiHeader.biPlanes = 1; //bitmapinfo.bmiHeader.biBitCount = 24; //设置格式不一定成功,看设备是否支持 //this.capSetVideoFormat(this.lwndC, ref bitmapinfo, ShowVideo.SizeOf(bitmapinfo)); this.mFrameEventHandler = new ShowVideo.FrameEventHandler(FrameCallBack); this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler); ShowVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth, mHeight, 6); //ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_GET_VIDEOFORMAT, ShowVideo.SizeOf(bitmapinfo), ref bitmapinfo); } }
/// <summary> /// 录像,保存avi文件的路径 /// </summary> /// <param name="lwnd"></param> /// <param name="path"></param> public void CapScope(IntPtr lwnd, string path) { IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32()); ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_SEQUENCE, 0, 0); }
/// <summary> /// 抓图 /// </summary> /// <param name="path"></param> public void CapImage(string path) { ShowVideo.BITMAPINFO bitmapinfo = new ShowVideo.BITMAPINFO(); IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_SAVEDIB, 0, hBmp.ToInt32()); }
private bool capPreviewRate(IntPtr lwnd, short wMS) { // bool s = ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_SET_PREVIEWRATE, wMS, 0); //ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_SET_OVERLAY, 1, 0); return(s); }
private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr) { ShowVideo.VIDEOHDR videoHeader = new ShowVideo.VIDEOHDR(); byte[] VideoData; videoHeader = (ShowVideo.VIDEOHDR)ShowVideo.GetStructure(lpVHdr, videoHeader); VideoData = new byte[videoHeader.dwBytesUsed]; ShowVideo.Copy(videoHeader.lpData, VideoData); if (this.RecievedFrame != null) { this.RecievedFrame(VideoData); } }
private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr) { if (this.RecievedFrame != null) { ShowVideo.VIDEOHDR videoHeader = new ShowVideo.VIDEOHDR(); byte[] VideoData; videoHeader = (ShowVideo.VIDEOHDR)ShowVideo.GetStructure(lpVHdr, videoHeader); VideoData = new byte[videoHeader.dwBytesUsed]; ShowVideo.Copy(videoHeader.lpData, VideoData); IntPtr hdc = API.CreateCompatibleDC(0); API.BITMAPINFO bitheder = new API.BITMAPINFO(); ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_GET_VIDEOFORMAT, ShowVideo.SizeOf(bitheder), ref bitheder); API.BITMAPINFOHEADER hhh = bitheder.bmiHeader; API.BITMAPINFO bitinfo = new API.BITMAPINFO(); IntPtr ht = API.CreateDIBSection(IntPtr.Zero, bitheder, API.DIB_RGB_COLORS, videoHeader.lpData, 0, 0); this.RecievedFrame(this, VideoData); } }
/// <summary> /// 显示压缩设置 /// </summary> public void ShowCompressionSetting() { ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0); }
/// <summary> /// 显示图像格式设置窗口 /// </summary> public void ShowImageFormatSetting() { ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_DLG_VIDEOFORMAT, 0, 0); }
private bool capSetVideoFormat(IntPtr hCapWnd, ref ShowVideo.BITMAPINFO BmpFormat, int CapFormatSize) { return(ShowVideo.SendMessage(hCapWnd, ShowVideo.WM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat)); }
private bool capSetCallbackOnFrame(IntPtr lwnd, ShowVideo.FrameEventHandler lpProc) { return(ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_SET_CALLBACK_FRAME, 0, lpProc)); }
private bool capPreview(IntPtr lwnd, bool f) { return(ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_SET_PREVIEW, f, 0)); }
private bool capDriverDisconnect(IntPtr lwnd) { return(ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_DRIVER_DISCONNECT, 0, 0)); }
// private functions private bool capDriverConnect(IntPtr lwnd, short i) { return(ShowVideo.SendMessage(lwnd, ShowVideo.WM_CAP_DRIVER_CONNECT, i, 0)); }
/// <summary> /// 摄像头设置 /// </summary> public void ShowSelectCameraOption() { ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_DLG_VIDEOSOURCE, 0, 0); }
public void ReSetWindowSize(int width, int height) { ShowVideo.SetWindowPos(this.lwndC, 0, 0, 0, width, height, 6); }
/// <summary> /// 获取摄像头实际图像大小 /// </summary> /// <returns></returns> public Size GetVideoSize() { ShowVideo.CAPSTATUS status = new ShowVideo.CAPSTATUS(); ShowVideo.SendMessage(this.lwndC, ShowVideo.WM_CAP_GET_STATUS, ShowVideo.SizeOf(status), ref status); return(new Size(status.uiImageWidth, status.uiImageHeight)); }