Realize video capture programming with VC ++ 5.0 (reproduced)

xiaoxiao2021-03-06  43

In a desktop video conference, a visual phone and other multimedia applications are a key premise. Capturing digital video is an extremely complex job before Video for Windows (VFW). Microsoft's Visual C has started supporting Video for Windows (VFW) since version 4.0, which brings great convenience to video capture. About multimedia application development, more introduction to MCI (Media Control Interface) in the market epidemic data, and this article focuses on how to use the Avicap window class provided by Visual C for video capture and the concepts and key issues involved. 1. Video for Windows Introduction VFW is a software package that Microsoft introduced on digital video in 1992, which enables the application to digitize and play video clips from traditional analog video sources. A key idea of ​​VFW is that there is no need for dedicated hardware when playing, in order to solve the problem of large number of digital video data, the data needs to be compressed. It introduces a document standard called AVI, which does not specify how to capture, compress and play with video, only specify how video and audio are stored on the hard disk, alternately store video frames in the AVI file and match it. Audio data. VFW provides programmers with advanced programming tools for the .vbx and Avicap window classes, allowing programmers to capture, play, and edit video clips by sending a message or setting properties. Now the user does not have to install the VFW specifically, the Windows95 itself includes Video for Windows 1.1, when the user installs Windows, the installer automatically installs the components required to configure the video, such as device drivers, video compression, and more. The VFW is mainly composed of the following six modules: (1) avicap.dll: contains a function that performs video capture, which provides an advanced interface to the AVI file I / O and video; (2) msvideo.dll: Use a special DrawDIB function to handle video operations on the screen; (3) mciavi.drv: This driver includes an interpreter for the MCI command to the VFW; (4) avifile.dll: Support by standard multimedia I / O Mmio) Function provides higher commands to access .avi files; (5) Compression Manager (ICM): Managing codecs for video compression-decompression; (6) Audio Compression Manager ACM: Provides similar services similar to ICM, different is that it is suitable for waveforming audio. Visual C provides similar libraries such as VFW32.LIB, MSACM32.LIB, Winmm.Lib, etc. in support VFW. In particular, it provides powerful, easy and easy, similar to the MCIWND window. Avicap provides a simple, message-based interface to enable it to access video and waveform audio hardware, and can control during the process of capturing video streams to the hard disk. Second, Avicap Programming Introduction Avicap supports real-time video stream capture and single frame capture and provides control over video sources.

Although MCI also provides a digital video service, such as the video superposition of the .avi file, provides the overlay command set for the video superposition, but these commands are based primarily based on file-based operations, which cannot meet live directly from videos Cache requires data, for the PC that uses a capture card without a video superimposable ability, the command set provided by MCI is unable to capture the video stream. Avicap has a certain advantage in capturing the video, it can directly access the video buffer, do not need to generate an intermediate file, strong real-time, high efficiency. At the same time, it can also capture digital video to files. Before video capture, you need to create a capture window, all capture operations and their settings are based on it. The window created with the Avicap window class (created via the CapCreateCaptureWindow function) is called a "capture window", which is generally WS_CHILD and WS_VISIBLE. In concept, the capture window is similar to standard control (such as buttons, list boxes, etc.). The capture window has the following functions: (1) capture a video stream and audio stream into an AVI file; (2) Dynamically connect or disconnect with video and audio input devices; (3) inputs in Overlay or Preview mode The video stream performs real-time display; (4) You can specify the file name used and copy the contents of the capture file to another when captured; (5) Set the capture rate; (6) Display control video source, video format, video Compressed dialog; (7) creation, saving, or load palette; (8) copy image and related palette to clipboard; (9) Save a single frame image of the captured as a DIB format . Here, I need to explain the two modes provided when Avicap are displayed: (a) Preview mode: This mode uses CPU resources, the video frame first passes from the capture hardware to the system memory, then use the GDI function in the capture window . Physically, this mode needs to be displayed on the monitor via the VGA card. (B) Overlay mode: This mode uses hardware superposition to perform video display, the display of the superimposed video does not pass the VGA card, the hardware superimposed video combines the output signal of the VGA with its own output signal, form a combined signal displayed in the computer Monitor. Only some video capture cards have video superposition capabilities. In addition to using nine functions of the capture window, flexible writing of Avicap is also able to meet some special needs, such as using the macro CapCapturequencyNofile as a callback function registered with CapSetCallbackOnvideStream to make the application directly using video and audio data, in videos This can be used in the app to obtain video frames, and the callback function will pass the captured image to the remote computer.

The application can use the capture window to register the callback function (written by the user, which is called by the system) so that it can notify the application to make a corresponding reaction: (1) Capture window state changes; (2) error; 3) The video frame and audio cache can be used; (4) During the capture process, other applications are in the Yield position. Like ordinary SDK, video capture programming also uses structures, macros, messages, and functions involving video capture. Let programmers feel easy to send the function that the send Avicap window message can be completed to complete the corresponding macro. For example, SendMessage (HWNDCAP, WM_CAP_DRIVER_CONNECT, 0, 0L) is the same as CAPDriverConnect (HWNDCAP, 0), which is connected to the created capture window to the video input device. When using Avicap programming, you should be familiar with the structure associated with video capture, and a brief introduction to the commonly used four structures, for the first three structures, there is a corresponding function to set and obtain the information included: (1) CapStatus defines the current state of the capture window, such as the width, high; (2) CapdriverCaps: Defines the ability to capture the drive, if there is no video stack, there is no control video source, dialog box, etc .; 3) CaptureParms: Contains parameters for control the video stream capture process, such as capturing frame rate, specifying the keyboard or mouse button to terminate capture, capture time limit, etc. (4) VideoHDR: Define the header information of the video data block, writing a callback function It is often used to use its data member LPDATA (pointing to the pointer to the data cache) and DWBufferLength (the size of the data cache). Third, the Avicap Programming Example The following uses a simple application as an example of Avicap, which is real-time display and capture for the input video stream, demo a video capture card and camera. The menu item in the interface is shown in Figure 1. The menu item Display can display an image in a preview or overlay mode; menu item Setting can set the capture by popping the dialog provided by Avicap, Video Source, Video Format, and Video Display, the image in Figure 4 is followed by Figure 2, 3 The dialog box is set, and the result is displayed in the Preview mode; the menu item Capture captures the video stream or single frame image to the specified file. Figure 1 Menu item Figure 2 Video Format dialog Figure 3 Video Source dialog Figure 4 Figure 2 and Figure 3 Setting one frame diagram shown under Settings Due to limited space, only the programming related to video capture is described below.

1. Define global variables: hWnd ghwndcap; // capture the handle of the handle CapdriverCaps gcapdrivercaps; // Video drive CapStatus gcapstatus; // Capture the state 2, process the WM_CREATE message: // Create a capture window, where hwnd is the main window Handle GHWNDCAP = CapcreateCaptureWindow ((lpstr) "Capture Window", WS_CHILD | WS_VISIBLE, 0, 0, 300, 240, (HWND) HWND, (INT) 0); // Register three callback functions, they should be admitted to CapSetCallbackONERROR (GHWNDCAP , (FARPROC) ErrorCallbackProc); capSetCallbackOnStatus (ghWndCap, (FARPROC) StatusCallbackProc); capSetCallbackOnFrame (ghWndCap, (FARPROC) FrameCallbackProc); capDriverConnect (ghWndCap, 0); // the ability // drive for the capture window with drive connections, related information on the structure of the variable gCapDriverCaps capDriverGetCaps (ghWndCap, & gCapDriverCaps, sizeof (CAPDRIVERCAPS)); 3, a handler for WM_CLOSE: // canceled three callback functions capSetCallbackOnStatus (ghWndCap, NULL) registered; capSetCallbackOnError (ghWndCap, NULL); capSetCallbackOnFrame (ghWndCap, NULL); capCaptureAbort (ghWndCap); // stops capturing capDriverDisconnect (ghWndCap); // the capture window 4 with the drive is disconnected, the processing menu item Preview: capPreviewRate (ghWndCap, 66); // set Preview mode Display rate CAPPREVIEW (GHWNDCAP, TRUE ); // Start preview mode 5, process menu item overlay: if (gcapdrivercaps.fhaasoverlay) // check if the drive has superposed capabilities CapoverLay (GHWNDCAP, TRUE); // Start Overlay mode 6, process menu item EXIT: SendMessage (hwnd) , Wm_close, wparam, lparam; 7, three menu items under Setting, respectively control the video source, video format, and display respectively: if (gcapdrivercaps.fhasdlgvideosource) CapdlgvideSource (GHWNDCAP); // Video Source dialog IF (

gapDriverCaps.fHasDlgVideoFormat) capDlgVideoFormat (ghWndCap); // Video format dialog box if (CapDriverCaps.fHasDlgVideoDisplay) capDlgVideoDisplay (ghWndCap); // Video display box 8, processing Video Stream menu item, it captures video stream to a .AVI file: char szCaptureFile [] = "MYCAP.AVI"; capFileSetCaptureFile (ghWndCap, szCaptureFile); // file name specified capture capFileAlloc (ghWndCap, (1024L * 1024L * 5)); // allocate storage space for the files captured capCaptureSequence (ghWndCap); // Start capture video sequence 9, process Single Frame menu item: CapgrabFrame (GHWNDCAP); // capture single frame image 10, define three callback functions: LRESULT CALLBACK STATUSCALLBACKPROC (HWND HWND, INT NID, LPSTR LPSTATUSTEXT) {IF (! ghWndCap) return FALSE; // get capture window state capGetStatus (ghWndCap, & gCapStatus, sizeof (CAPSTATUS)); // update capture window size SetWindowPos (ghWndCap, NULL, 0, 0, gCapStatus.uiImageWidth, gCapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE); if (NID == 0) {// Clear the old status information setWindowText (GHWNDCAP, (LPSTR) gachappname); Return (LRESULT) TRUE;} // Display Status ID and Status Text WSPrintf (Gachbuffer, "status #% d:% s ", NID, lpStatusText); SetWindowText (ghWndCap, (LPSTR) gachBuffer); return (LRESULT) TRUE;} LRESULT CALLBACK ErrorCallbackProc (HWND hWnd, int nErrID, LPSTR lpErrorText) {if (ghWndCap) return FALSE;! If (nErrID = = 0) Return True; // Clear old error WSPrintf (Gachbuffer, "Error #% D", Nerrid);

转载请注明原文地址:https://www.9cbs.com/read-85711.html

New Post(0)