public void Clear( ) { mCapDriverConnectStatus = false; // capDriverConnect() mHWNDCapture = IntPtr.Zero; mSTVideoCaptureFormat = STVideoCaptureFormat.BGR320x240; mCapturePeriodMilliseconds = 67; // 67 milliseconds --> 15 frames per second maximum mClientCaptureCallback = null; mBGRData = null; }
Initialize ( IntPtr hwndParentWindow, // This must be non-null STVideoCaptureFormat videoCaptureFormat, int capturePeriodMilliseconds, DelegateClientCaptureCallback delegateClientCaptureCallback ) { this.Clear( ); // Validate capture format int captureWidth = 0; int captureHeight = 0; switch (videoCaptureFormat) { case STVideoCaptureFormat.BGR320x240: { captureWidth = 320; captureHeight = 240; } break; } if ((0 == captureWidth) || (0 == captureHeight)) { System.Console.WriteLine("Invalid capture format."); this.Clear( ); return(false); } this.mSTVideoCaptureFormat = videoCaptureFormat; // We create a capture window only because we are required to create // such a window to use the Video for Windows (VFW32) API. We make // this window tiny to keep it out of the way of our application. // For our purposes, this window is simply a necessary interface to // VFW functionality. this.mHWNDCapture = capCreateCaptureWindow ( "STVideoCapture Window", (WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS), // style 0, // x 0, // y 4, // width 4, // height hwndParentWindow, // Parent window : This must be non-null 0 // nID ); if (IntPtr.Zero == this.mHWNDCapture) { System.Console.WriteLine("Failed to create video capture window."); DestroyWindow(this.mHWNDCapture); this.Clear( ); return(false); } this.mCapDriverConnectStatus = capDriverConnect(this.mHWNDCapture, 0); if (false == this.mCapDriverConnectStatus) { System.Console.WriteLine("Failed to connect to video driver."); DestroyWindow(this.mHWNDCapture); this.Clear( ); return(false); } // CAPDRIVERCAPS capdrivercaps = new CAPDRIVERCAPS( ); // NOT FINISHED SUPPORT FOR THIS: capDriverGetCaps( mHWNDCapture, ref capdrivercaps, 44 ); //capDlgVideoFormat( this.mHWNDCapture ); //capDlgVideoSource( this.mHWNDCapture ); // Source; Brightness, Contrast, Saturation, Exposure // Allocate a local buffer to store capture data int bytesPerPixel = 3; switch (videoCaptureFormat) { case STVideoCaptureFormat.BGR320x240: { bytesPerPixel = 3; } break; } int totalImageBytes = 0; totalImageBytes = (captureHeight * (captureWidth * bytesPerPixel)); this.mBGRData = new byte[totalImageBytes]; // Set the video stream callback function this.mClientCaptureCallback = delegateClientCaptureCallback; capSetCallbackOnFrame(this.mHWNDCapture, STVideoCapture.mCommonCaptureCallback); // Set the preview rate in milliseconds this.mCapturePeriodMilliseconds = capturePeriodMilliseconds; capPreviewRate(this.mHWNDCapture, capturePeriodMilliseconds); // Disable preview mode capPreview(this.mHWNDCapture, false); // Set the format of the data we wish returned to us BITMAPINFO capbitmapinfo = new BITMAPINFO( ); capbitmapinfo.bmiHeader.biSize = 40; // sizeof( BITMAPINFOHEADER ) capbitmapinfo.bmiHeader.biWidth = captureWidth; capbitmapinfo.bmiHeader.biHeight = captureHeight; capbitmapinfo.bmiHeader.biPlanes = 1; capbitmapinfo.bmiHeader.biBitCount = 24; capbitmapinfo.bmiHeader.biCompression = BI_RGB; capbitmapinfo.bmiHeader.biSizeImage = totalImageBytes; capbitmapinfo.bmiHeader.biXPelsPerMeter = 100; capbitmapinfo.bmiHeader.biYPelsPerMeter = 100; bool setVideoFormatResult = false; setVideoFormatResult = capSetVideoFormat(this.mHWNDCapture, capbitmapinfo, 44); if (false == setVideoFormatResult) { System.Console.WriteLine("Failed to set the desired video capture format."); capSetCallbackOnFrame(this.mHWNDCapture, null); // disable the callback function DestroyWindow(this.mHWNDCapture); this.Clear( ); return(false); } return(true); }