public bool ReSizePic(int width, int height) { try { // CloseWebcam(); // StartWebCam(); // this.lwndC = VideoAPI.capCreateCaptureWindow("", VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0); // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_GET_STATUS, VideoAPI.SizeOf(CapStatus), ref CapStatus);//获得当前视频流的尺寸 存入CapStatus结构 // VideoAPI.FrameEventHandler FrameEventHandler = new VideoAPI.FrameEventHandler(FrameCallback); // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_ERROR, 0, 0);//注册错误回调函数 // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_STATUS, 0, 0);//注册状态回调函数 // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);//注册视频流回调函数 // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_FRAME, 0, FrameEventHandler);//注册帧回调函数 //// if (VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0)) // { // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_SCALE, 1, 0); // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 40, 0); // VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_PREVIEW, 1, 0); return(VideoAPI.SetWindowPos(this.lwndC, 0, 0, 0, width, height, VideoAPI.SWP_NOZORDER | VideoAPI.SWP_NOMOVE) > 0); // } // return false; } catch { return(false); } }
/// <summary> /// 打开视频设备 /// </summary> /// <param name="width">捕获窗口的宽度</param> /// <param name="height">捕获窗口的高度</param> /// <returns></returns> public bool StartWebCam(int width, int height) { //byte[] lpszName = new byte[100]; //byte[] lpszVer = new byte[100]; //VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100); //this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, 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(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0); // VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0); // return true; //} //else //{ // return false; //} this.lwndC = VideoAPI.capCreateCaptureWindow("", VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0); //AVICap类的捕捉窗口 VideoAPI.FrameEventHandler FrameEventHandler = new VideoAPI.FrameEventHandler(FrameCallback); VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_ERROR, 0, 0); //注册错误回调函数 VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_STATUS, 0, 0); //注册状态回调函数 VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); //注册视频流回调函数 VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_CALLBACK_FRAME, 0, FrameEventHandler); //注册帧回调函数 //if (!CapDriverCAPS.fCaptureInitialized)//判断当前设备是否被其他设备连接已经连接 //{ if (VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0)) { //----------------------------------------------------------------------- VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_DRIVER_GET_CAPS, VideoAPI.SizeOf(CapDriverCAPS), ref CapDriverCAPS); //获得当前视频 CAPDRIVERCAPS定义了捕获驱动器的能力,如有无视频叠加能力、有无控制视频源、视频格式的对话框等; VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_GET_STATUS, VideoAPI.SizeOf(CapStatus), ref CapStatus); //获得当前视频流的尺寸 存入CapStatus结构 VideoAPI.BITMAPINFO bitmapInfo = new VideoAPI.BITMAPINFO(); //设置视频格式 (height and width in pixels, bits per frame). bitmapInfo.bmiHeader = new VideoAPI.BITMAPINFOHEADER(); bitmapInfo.bmiHeader.biSize = VideoAPI.SizeOf(bitmapInfo.bmiHeader); bitmapInfo.bmiHeader.biWidth = mWidth; bitmapInfo.bmiHeader.biHeight = mHeight; bitmapInfo.bmiHeader.biPlanes = 1; bitmapInfo.bmiHeader.biBitCount = 24; VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 40, 0); //设置在PREVIEW模式下设定视频窗口的刷新率 设置每40毫秒显示一帧,即显示帧速为每秒25帧 VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_SCALE, 1, 0); //打开预览视频的缩放比例 VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_VIDEOFORMAT, VideoAPI.SizeOf(bitmapInfo), ref bitmapInfo); this.mFrameEventHandler = new VideoAPI.FrameEventHandler(FrameCallback); this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler); VideoAPI.CAPTUREPARMS captureparms = new VideoAPI.CAPTUREPARMS(); VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_GET_SEQUENCE_SETUP, VideoAPI.SizeOf(captureparms), ref captureparms); if (CapDriverCAPS.fHasOverlay) { VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_OVERLAY, 1, 0); //启用叠加 注:据说启用此项可以加快渲染速度 } VideoAPI.SendMessage(this.lwndC, VideoAPI.WM_CAP_SET_PREVIEW, 1, 0); //设置显示图像启动预览模式 PREVIEW VideoAPI.SetWindowPos(this.lwndC, 0, 0, 0, width, height, VideoAPI.SWP_NOZORDER | VideoAPI.SWP_NOMOVE); //使捕获窗口与进来的视频流尺寸保持一致 return(true); } else { flag = false; return(false); } }