Summary: Many software (such as video conferencing, video calls, etc.) are applied to video capture technology. Microsoft provides software developers with a VFW SDK specially used for video capture, providing a standard interface for video capture in a Windows system, and greatly reduced the development difficulty of programs. Since the VFW SDK is only VC and VB, there is no Delphi version, therefore need to declare each function and variable in the DLL in Delphi. The document details how to use the VFW to develop video capture procedures in Delphi, and give programs instances.
Keywords: Delphi Video for Windows Video Capture
1 Introduction
Video capture and real-time processing are one of the most critical technologies in the current image processing system, which can accurately capture the specified video images, and achieve accurate data analysis and processing, which is related to the success or failure of the entire system. The author encountered this when developing "road safety line rolling detection system". The system mainly studies the key to the highway, and whether the motor vehicle is instantaneous rolling yellow safety line. Therefore, one of the vehicle rolling safety lines is that vehicle overtaking or reverse exercise violates up and down line rules, which is the most important and direct factor in traffic accidents. The system passes real-time shooting, grasping the instant image, and timely accurately detecting the vehicle driving in a timely manner, thereby driving the control device to make relevant processing.
Obviously, the key to this system is to capture video images in real time. To this end, a package VFW that Microsoft is introduced with respect to digital video. It enables applications to get digital video clips from traditional analog video sources through digital devices. A key idea of VFW is no special hardware when playing. In order to solve the problem with large number of digital video data, the data needs to be compressed, and the VFW introduces AVI's file standard. This standard does not specify how to capture, compress and play with video, and only specify how video and audio stores on the hard disk and alternately store video frames in the AVI file and audio data that matches there. However, VFW allows programmers to capture, play, and edit video clips by sending messages or setting properties. When the user is installing VFW, the installer automatically installs the components required to configure the video, such as device drivers, video compression, and the like. VFW is mainly composed of 6 modules. Specifically, as listed in Table 1. Table 1 VFW function module
The module function avicap.dll contains functions that perform video capture, it gives the AVI file I / O process and video, the audio device driver provides a high-level interface msvideo.dll contains a special DrawDib function, used to handle the screen Video operation MCIAVI.DRV includes the driver of the VFW's MCI command interpreter AVIFILE.DLL contains higher commands provided by the standard multimedia I / O (MMIO) function, uses to access .avi file ICM compression manager, with The management video compression / decompression encoder (CODEC) ACM audio compression manager provides services similar to ICM, suitable for waveform audio
2 Basic steps for video capture procedures
2.1 Using Avicap Window Categories
The author uses Avicap window classes to develop video capture programs. Avicap class supports real-time video stream capture and single frame capture, and provides control over the video source. Normally used MCI controls also provide digital video services. And provide the overlay command set for video superposition, but these commands are based primarily on file-based operations, but also meet the requirements of extracting data from video caches in real time. For a PC that uses a capture card without a video superimposable, the command set provided by MCI is unable to capture the video stream. The Avicap window class has a certain advantage in capturing the video, which can directly access the video buffer without generating an intermediate file, so the real-time performance is very efficient. In addition, it can capture digital video into a file.
2.2 Basic steps development
Development video capture procedures have the following four steps:
(1) Create "capture window".
You must first create a "capture window" before performing video capture, and you should use this based on all capture and setting operations. The "Capture window" can be created with the "CAP CREATE CAPTURE WINDOW" function of the Avicap window class, which can be set to WSCHILD and WS_VISIBLE parameters.
"Capture Window" is similar to standard control, which has the following functions:
* Cat video streams and audio streams into an AVI file;
* Dynamically 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 you can copy the contents of the capture file to another.
* Set the capture rate;
* Display dialog box for control video sources, video formats, and video compression;
* Create, save, or load palette;
* Copy the image and related palette to the clipboard;
* Save the captured single frame image to the DIB format file.
(2) Association capture window and driver
A separately defined capture window cannot work, it must be associated with a device to obtain a video signal. Function Capdriver Connect enables the capture window to associate with its device drivers.
(3) Set the properties of the video device
By setting the various member variables of the TcaptureParms structural variable, the sampling frequency, interrupt sampling buttons, and status behavior of the device can be controlled. After setting the TcaptureParms structure variable, you can use the CapCaptureetSetup to make the settings take effect. You can also use CAPPREVIEWSCALE, CAPPREVIEWRATE to set the proportion and speed of the preview, or directly use the default value of the device.
(4) Open preview
Use the function Capoverlay to choose whether to use the superimposed mode preview to make the system resource is small, and the video display speed is accelerated. 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 by the above four steps, but if you want to handle the video data captured from the device, you should use the capture window callback function, such as a frame, one frame, to obtain video data or in streaming The way video data is obtained.
3 video capture program based on Delphi
According to the system access, processing speed, etc., the author selects Delphi as the development tool. The following is to develop a program that captures video data from the video device as an example, and the specific process of development of each function. The function of the routine is to display the captured video on the screen, and image data for each frame can be obtained. Specific steps are as follows:
(1) Create a new project and include Avicap32.PAS to Uses.
(2) Place a TPANEL control on Form1, set NAME to "GCAPVideoArea", which is used to display video. After the two TBUTTON controls, a name is "OpenVideo" and another name is "Closevideo".
(3) Define global variables
GHCAPWND: THANDLE; / / Define capture window handle
VideoTr: lpvideohdr; // You can get the structural variable of the video data pointer for the callback function.
CAPPARMS: TcaptureParms; // Table for setting the structure variable for device properties
(4) Write code
Write the following code in the Click event of Name "OpenVideo" TButton:
Procedure TFORM1.Openvidoclick (Sender: TOBJECT);
Begin
// Use the TPANEL control to create a capture window
GHCAPWND: = CapcreateCaptureWindow (Pchar ('KruWosoft'), WS_CHILD or WS_Visible, // Window Style
0, // x coordinate
0, // y coordinate
GCapVideoarea, width, // window wide
GCapVideoarea, Handle, // Window handle
0); // is usually 0
In order to capture the video, you should start a capture frame callback function videostreamCallback. When capturing a video stream or the current device status, the following functions should be used separately:
CapsetCallbackonvideStream; // Capture a video stream
CapsetCallbackONERROR; / / Get a device error
CapsetCallbackOnStatus // Get a device status
/ / Define a frame capture callback function
CapsetCallbackonframe (GhcapWnd, Longint (@VideStreamCallback);
// Associate a capture window with a device driver, the second parameter is a serial number, when a plurality of display drivers are installed in the system, their values are 0 to the total number.
CapdreiverConnect (GHCAPWND, 0);
CAPPARMS, DWREQUESTMICROSECPERFRAME: = 40000;
Capparms.flimitenabled: = false;
Capparms.fcaptureaudio: = false; // no Audio
Capparms.fmcicontrol: = false;
Capparms.Fyield: = true;
Capparms.vkeyabort: = vk_escape;
Capparms.fabortleftmouse: = flase;
Capparms.faBorTrightMouse: = false;
/ / Make the settings
CapcaptureetSetup (GhcapWnd, Longint (@capparms), sizeof (tcaptureparms));
CAPPREVIEWSCALE (GHCAPWND, 1);
CAPPREVIEWRATE (GHCAPWND, 66);
If you want to capture a video stream, use a function to specify that you do not generate a file. Otherwise, the AVI file will be automatically generated:
Capcapturequencing; GhcapWnd;
Specifies whether to use a superposition mode, 1 is used, otherwise 0;
Capoverlay (GHCAPWND, 1);
CAPPREVIEW (GHCAPWND, 1);
END;
Write the following code in the Click event of Name "Closevideo" TButton:
Procedure TFORM1.CLOSEVIDEOCLICK (Sender: TOBJECT);
Begin
Capcaptureabort (GHCAPWND); // Stop capture
CapdriveDisconnect (ghcapwnd); // Disconnect the capture window with the drive
END;
Define capture frame callback function:
Function framecallback (hwnd: hwnd; lpvhdr: longint): longint; stdcall;
VAR
DataPoint: ^ Byte;
Diblen, RectWidth, RectHeight: Integer; Begin
VideoTr: = lpvideohdr (LPVHDR);
DIBLEN: = videostr ^ .dwbufferLength;
GetMem (DataPoint, 64000);
// In one memory, pay attention: DataPoint must allocate space
CopyMemory (DataPoint, Videostr ^ .lpdata, Diblen);
......
END;
4 Conclusion
Flexible use of the callback function of the Avicap window class can meet various needs, but pay attention to the format of the video data captured from the video card and the design of the image to refer to the video card parameters. Additionally, some video cards can support multiple formats and images long width, so when the image is restored, pay attention to the parameters of the video card used.