This article refers to the address: http://

Abstract: Many video-based software (such as video conferencing, video telephony, etc.) are developed for video capture technology. Microsoft provides software developers with a VFW SDK dedicated to video capture, providing a standard interface for video capture in Windows systems and greatly reducing the difficulty of program development. Since the VFW SDK only has VC and VB versions, there is no Delphi version, so you need to declare the various functions and variables in the DLL one by one in Delphi. The article describes in detail how to use VFW to develop video capture programs in Delphi, and gives examples of programs.

Keywords: Delphi Video for Windows video capture

1 Introduction

Video capture and real-time processing is one of the most critical technologies in image processing systems. Whether to accurately capture a specified video image and achieve accurate data analysis and processing is related to the success or failure of the entire system. I encountered this situation when developing the “Road Safety Line Rolling Inspection System”. The system mainly studies whether the motor vehicle has been rolling the yellow safety line in a critical section of the road. Therefore, one of the main reasons for the vehicle rolling safety line is that the vehicle overtakes or reverses the operation and violates the upper and lower rules. This is the most important and direct factor causing traffic accidents. The system captures the instantaneous image through real-time shooting, and analyzes and processes the system to timely and accurately detect the driving condition of the vehicle, 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 software package VFW for digital video from Microsoft was used. It enables applications to get digital video clips from traditional analog video sources via digital devices. A key idea of ​​VFW is that no dedicated hardware is required for playback. In order to solve the problem of large amount of digital video data, it is necessary to compress the data, and VFW introduces the AVI file standard. The standard does not specify how to capture, compress, and play video. It only specifies how video and audio should be stored on the hard disk and alternately store video frames and matching audio data in AVI files. But VFW allows programmers to capture, play, and edit video clips by sending messages or setting properties. When the user installs VFW, the installer automatically installs the components needed to configure the video, such as device drivers, video compression programs, and so on. VFW is mainly composed of 6 modules. The details are listed in Table 1.

Table 1 VFW function module

Module Features
AVICAP.DLL Contains functions for performing video capture, which provides an advanced interface for I/O processing of AVI files and video and audio device drivers.
MSVIDEO.DLL Contains a special set of DrawDib functions for handling on-screen video operations
MCIAVI.DRV Includes drivers for the MCI command interpreter for VFW
AVIFILE.DLL Contains higher commands provided by standard multimedia I/O (mmio) functions for accessing .AVI files
ICM Compression Manager, a video compression/decompression codec for managing (Codec)
ACM Audio Compression Manager, which provides similar services to ICM for waveform audio

2 Basic steps for video capture program development

2.1 Using the AVICap Window Class

I use the AVICap window class to develop a video capture program. The AVICap class supports real-time video stream capture and single-frame capture and provides control over the video source. The commonly used MCI controls also provide digital video services. The Overlay command set is provided for video overlay, etc., but these commands are mainly file-based operations, and cannot meet the requirement of extracting data from the video buffer in real time. For PCs that use capture cards that do not have video overlay capabilities, the command set provided by MCI cannot capture video streams. The AVICap window class has certain advantages in capturing video. It can directly access the video buffer without generating intermediate files, so it is very real-time and efficient. In addition, it captures digital video into a single file.

2.2 Basic steps of development

There are four main steps in developing a video capture program:

(1) Create a "capture window".

Before you can capture a video, you must first create a “capture window” and use this as a basis for all capture and setup operations. The "Capture Window" can be created using the "Cap Create Capture Window" function of the AVICap window class, and its window style can be set to the WSCHILD and WS_VISIBLE parameters.

The Capture Window is similar to the standard controls and has the following features:

* Capture video and audio streams into an AVI file;

* Dynamically connected or disconnected from video and audio input devices;

* Real-time display of the input video stream in Overlay or Preview mode;

* When capturing, you can specify the file name used and copy the contents of the capture file to another file;

* Set the capture rate;

* Display a dialog box for controlling video source, video format and video compression;

* Create, save or load a palette;

* Copy images and related palettes to the clipboard;

* Save captured single frame images to DIB format files.

(2) Association capture window and driver

A separately defined capture window is not functional and must be associated with a device to obtain a video signal. Use the function CapDriver Connect to associate a capture window with its device driver.

(3) Set the properties of the video device

By setting each member variable of the TcaptureParms structure variable, you can control the sampling frequency of the device, interrupt the sampling button, and state behavior. After setting the TcaptureParms structure variable, you can use the CapCaptureSetSetup to make the settings take effect. You can also use CapPreviewScale, CapPreviewRate to set the scale and speed of the preview, or you can use the default values ​​of the device directly.

(4) Open preview

Use the function CapOverlay to choose whether to use the overlay mode preview, so that the system resources are small and the video display speed is faster. Then use CapPreview to start the preview function, then you can see the image from the camera on the screen.

Through the above four steps, you can establish a basic video capture program, but if you want to process the video data captured from the device yourself, you need to use the capture window callback function to process, for example, to obtain video data or stream by frame. Ways to get video data, etc.

3 Delphi-based video capture program

According to the special requirements of the system for system access, processing speed, etc., I chose Delphi as a development tool. The following is an example of developing a program for capturing video data from a video device frame by frame to illustrate the function of each function and the specific process of development. The function of the given routine is to display the captured video on the screen and obtain image data for each frame. Specific steps are as follows:

(1) Create a new project and include AVICAP32.PAS in USES.

(2) Place a Tpanel control on Form1 and set the Name to "gCapVideoArea", which is used to display the video. Then place two Tbutton controls, one with the name "Openvideo" and the other with the name "Closevideo".

(3) define global variables

ghCapWnd:Thandle; //Define the capture window handle

VideoStr: LPVIDEOHDR; / / can get the structure variable of the video data pointer, used in the callback function

CapParms: TcaptureParms; / / structure variable used to set device properties

(4) writing code

Write the following code in the Click event of the Tbutton with the name "Openvideo":

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 coordinates

0, / / ​​Y coordinates

gCapVideoArea, Width, // window width

gCapVideoArea, Handle, // window handle

0); //Generally 0

In order to be able to capture video, a capture frame callback function VideoStreamCallBack should be started. When capturing a video stream or current device state, the following functions should be used separately:

CapSetCallbackOnVideoStream; //Capture a video stream

CapSetCallbackOnError; //Get a device error

CapSetCallbackOnStatus //Get a device status

/ / Define a frame capture callback function

CapSetCallbackOnFrame (ghCapWnd, LongInt(@VideoStreamCallBack));

// Associate a capture window with a device driver. The second parameter is a sequence number. When multiple display drivers are installed in the system, the 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 take effect

CapCaptureSetSetup(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 no files are generated. Otherwise an AVI file will be generated automatically:

CapCaptureSequenceNoFile(ghCapWnd);

Specify whether to use the overlay mode, 1 is used, otherwise 0;

CapOverlay(ghCapWnd,1);

CapPreview(ghCapWnd,1);

End;

Write the following code in the Click event of the Tbutton with Name "Closevideo":

Procedure TForm1.ClosevideoClick(Sender:Tobject);

Begin

capCaptureAbort(ghCapWnd); //stop capturing

capDriveDisconnect(ghCapWnd); //Disconnect the capture window from the drive

End;

Define the capture frame callback function:

Function FrameCallBack(hWnd:HWND;lpVHdr:LongInt):LongInt;stdcall;

Var

DataPoint: ^byte;

DibLen, RectWidth, RectHeight: integer;

Begin

VideoStr:=LPVIDEOHDR(lpVHdr);

DibLen:=VideoStr^.dwBufferLength;

GetMem(DataPoint,64000);

/ / COPY frame data into a memory, note: DATAPOINT must first allocate space

CopyMemory (DataPoint, VideoStr^.lpData, Diblen);

......

End;

4 Conclusion

Flexibility to use the callback function of the AVICap window class can meet various needs, but note that the format of the video data captured from the video card and the length and width of the image should refer to the parameters of the video card. In addition, some video cards can support multiple formats and image lengths and widths. Therefore, when restoring images, pay attention to the parameters of the video card used.

Ningbo Autrends International Trade Co.,Ltd. , https://www.vapee-cigarettes.com

Posted on