Video capture technology is applied in many software (such as video conferencing, video calls, etc.) development. Microsoft provides software developers with a VFW (Video for Windows) SDK specifically used for video capture. The VFW SDK provides a standard interface to implement video capture in the Windows system, which greatly reduces the development difficulty of programs. Since only VFW SDK has only VC and VB, no Delphi version, you need to declare each function and variable in DLL in Delphi (you can refer to the function declaration of VC in MSDN). This section 3 details how to use VFW to develop video capture programs in Delphi.
Introduction to VFW
VFW is a package that Microsoft introduced on digital video in 1992, which enables applications to get digital video clips from traditional analog video sources through digital devices. 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 file standard called AVI, which does not specify how to capture, compress and play with video, and specify how video and audio are stored on the hard disk, and alternately store video frames and related to the AVI file. Match audio data. VFW allows programmers to capture, play, and edit video clips by sending messages or setting properties. In the Windows 9x system, when the user installs the VFW, the installer automatically installs the components required to configure the video, such as device drivers, video compression programs, and the like.
VFW is mainly composed of the following six modules:
● Avicap.dll: Contains functions that perform video capture, which gives the I / O process and video of the AVI file, and the audio device driver provides an advanced interface;
● MSVideo.dll: Contains a special DrawDib function to handle video operations on the screen;
● mciavi.drv: including the driver of the MCI command interpreter for the VFW;
● Avifile.dll: Contains a higher command provided by the standard multimedia I / O (MMIO) function, uses to access the .avi file;
● Compression Manager (ICM): Video Compression / Umbrace Code Codulator (CODEC) for Managing;
● Audio compression manager ACM: Provides services similar to ICM for waveform audio.
Development steps
Avicap window supports real-time video stream capture and single frame capture and provides control over the video source. Although MCI also provides digital video services (such as the video of the video that is displayed .avi files), the overlay command set is provided for the video superposition, but these commands are based primarily on file-based operations, they cannot meet real-time The video cache is required to take the data, and the command set provided by MCI is unable to capture the video stream for the PC that does not have a video superimposed capture card. The Avicap window class has a certain advantage in capturing the video, it can directly access the video buffer, do not need to generate an intermediate file, very powerful, and high efficiency. Moreover, it can capture digital video into a file.
1. Create a "capture window"
You must create a "capture window" before performing video capture, and perform all capture and setting operations based on it. "Capture Window" is created with the "CapCreateCaptureWindow" function of the Avicap window, and its window style is generally WS_CHILD and WS_VISIBLE.
Capture window is similar to standard controls (such as buttons, list boxes, etc.) and has the following features:
● Cat video streams and audio streams into an AVI file;
● Connect or disconnect with video and audio input devices;
● Real-time display of the input video stream in Overlay or Preview mode;
● When captured, you can specify the file name used and copy the contents of the capture file to another; ● Set the capture rate;
● Display dialog box for control video sources, video formats, video compression;
● Create, save, or load palette;
● Copy the image and related palette to the clipboard;
● Save the captured single frame image as files in DIB format.
2. Association capture window and driver
One capture window separately defined is unable to work, it must be associated with a device, so that the video signal can be obtained. With function CAPDriverConnect enables a capture window to associate with a device driver.
3. Set the properties of the video device
The sampling frequency, interrupt sample button, state behavior, and the like can be controlled by setting the various member variables of the TcaptureParms structural variable. After setting the TcaptureParms structure variable, you can use the function CapCaptureetSetUp to make the settings take effect. You can also use CAPPREVIEWSCALE, CAPPREVIEWRATE to set the proportion and speed of the preview, or you can directly use the default value of the device.
4. Open preview
Use the function Capoverlay to choose whether to use the superimposed mode preview, which takes up the system resources, and the video display speed is fast. Then start the preview function with the CappreView, then you can see images from the camera on the screen.
A basic video capture program can be created through steps above. However, if you want to process the video data that has been captured from the device, you should use the capture window callback function to obtain video data or in a stream, such as one frame, and the like.
Instance programming
The role of each function will be described as an example with a frame one frame, one frame, captures the Delphi program of video data, as an example, and the development process.
The function of the program is to be visible to the video on the screen, and image data of each frame can be obtained.
Create a new project and include Avicap32.pa to Uses.
Place a TPANEL control on Form1, set NAME to "GCAPVideoarea", which is used to display video. Place two TBUTTON controls, a name is "OpenVideo", another name is "closevideo".
Define global variables:
VAR
/ / Define the capture window handle
GHCAPWND: THCAPWND: THCAPWND:
/ / You can get the structural variable of the video data pointer, used in the callback function
Videostr: LPVIDEOHDR;
// Table for setting the structure variable of the device attribute
Capparms: TcaptureParms;
Write the following code in the Click event of Name "OpenVideo" TButton:
Procedure TFORM1.OpenvideoClick (Sender: TOBJECT);
Begin
// Use the TPANEL control to create a capture window
GHCAPWND: = CapcreateCaptureWindow
(PCHAR ('KruWosoft'), // Capture the name of the window
WS_CHILD or WS_VISIBLE, / / Window Style
0, // x coordinate
0, // y coordinate
gcapvideoarea.width, // Window Wide
gcapvideoarea.height, // High window
gcapvideoarea.handle, // Window handle
0); // is usually 0
{In order to capture the video frame, start a capture frame callback function videostreamcallback. Use the following functions when capturing a video stream or the current device status:
// Capture a video stream
CapsetCallbackonvideStream;
// Get a device error
CapsetCallbackONERROR;
// Get a device status
CapsetCallbackOnStatus
}
/ / Define a frame capture callback function
CapsetCallbackOnframe (GHCAPWND, LONGINT); // Associate a capture window with a device driver, the second parameter is a serial number, when a plurality of visual drivers are installed in the system, their values are sequentially 0 to total
CapdriverConnect (GHCAPWND, 0);
/ / Set the structural variable of the device properties
CAPPARMS.DWREQUESTMICROSECPERFRAME: = 40000;
Capparms.flimitenabled: = false;
Capparms.fcaptureaudio: = false; // no Audio
Capparms.fmcicontrol: = false;
Capparms.Fyield: = true;
Capparms.vkeyabort: = vk_escape;
Capparms.fabortleftmouse: = false;
Capparms.faBorTrightMouse: = false;
/ / Make the settings
CapcaptureetSetup (GhcapWnd, Longint (@capparms), sizeof (tcaptureparms));
/ / Set the proportion of preview
CAPPREVIEWSCALE (GHCAPWND, 1);
/ / Set the frame frequency at the preview
CAPPREVIEWRATE (GHCAPWND, 66);
// If you want to capture a video stream, you should use a function to specify that you do not generate a file. Otherwise, AVI file will be automatically generated.
Capcapturequencing; GhcapWnd;
/ / Specify whether to use a superimposed mode, use it to 1, otherwise 0
Capoverlay (GHCAPWND, 1);
// Open preview
CAPPREVIEW (GHCAPWND, 1);
END;
Write the following code in the Click event of Name "Closevideo" TButton:
Procedure TFORM1.CLOSEVIDEOCLICK (Sender: TOBJECT);
Begin
// Stop capture
Capcaptureabort (GHCAPWND);
// Disconnect the capture window with the drive
Capdriverdisconnect (GHCAPWND);
END;
Define capture frame callback function:
Function framecallback (hwnd: hwnd; lpvhdr: longint): longint; stdcall;
VAR
DataPoint: ^ Byte;
Diblen, RectWidth, RectHeight: Integer;
Begin
/ / Convert the pointer obtained from the callback function
VideoTr: = lpvideohdr (LPVHDR);
// Get the returned data size
DIBLEN: = videostr ^ .dwbufferLength;
GetMem (DataPoint, 64000);
/ / In one memory, pay attention to DataPoint to allocate space
CopyMemory (DataPoint, Videostr ^ .lpdata, Diblen);
// Some other processing
......
END;
Flexible use of the callback function of the Avicap window class can meet a variety of different needs, but pay attention to the format of the video data captured from the video card and the parameters of the video card. Moreover, some video cards can support multiple formats and images long width by setting, so pay attention to the parameters of the video card used when the image is restored.