Development of VFW video capture Program Based on Delphi

Source: Internet
Author: User

Abstract:Many video software (such as video conferencing and videophone) development is applied to video capture technology. Microsoft provides software developers with a vfw sdk dedicated for video capture, which provides a standard interface for video capture in windows and greatly reduces the difficulty of program development. Because the vfw sdk only has VC and VB versions and does not have Delphi versions, you need to declare the functions and variables in the DLL in Delphi one by one. This article describes in detail how to use VFW to develop a video capture program in Delphi and provides a program example.

Keywords:Delphi video for Windows video capture

1 Introduction

Video Capture and real-time processing are currently one of the most critical technologies in the image processing system. Whether a specified video image can be accurately captured to achieve precise data analysis and processing is related to the success or failure of the entire system. I encountered this problem when developing the highway safety line rolling pressure detection system. The system mainly studies whether the passing motor vehicles are instantly pressed with yellow safety lines in critical highway areas. Therefore, one of the main causes of rolling safety lines is that the vehicle is overtaking or reversely exercising and violates the upstream and downstream rules. This is the most important and direct cause of a traffic accident. The system captures instant images in real time and analyzes and processes the images to detect the driving status of vehicles in a timely and accurate manner.

Obviously, the key of this system is to capture video images in real time. To this end, we use a software package VFW for digital video launched by Microsoft. It enables applications to obtain digital video clips from traditional analog video sources through digital devices. A key idea of VFW is that dedicated hardware is not required during playback. To solve the problem of large data volumes in digital videos, data needs to be compressed, while VFW introduces AVI file standards. This standard does not specify how to capture, compress, and play a video, only specify how videos and audios should be stored on hard disks and stored alternately in AVI files. However, VFW allows programmers to capture, play, and edit video clips by sending messages or setting attributes. When you install VFW, the installer automatically installs the components required for video configuration, such as the device driver and video compression program. VFW consists of six modules. See table 1.

Table 1 VFW function modules

Module Merit
Avicap. dll Includes the video capture function, which provides an Advanced Interface for I/O processing of AVI files and video and audio device drivers.
Msvideo. dll Contains a set of special drawdib functions for processing video operations on the screen.
Mciavi. DRV Include the driver of the MCI command interpreter for VFW
Avifile. dll Contains higher commands provided by standard multimedia I/O (mmio) functions to access. AVI files.
ICM Compression manager, which is used to manage the video compression/Decompression encoding/Decoder (codec)
ACM Audio compression manager, which provides services similar to ICM and is suitable for Waveform audio.

2. Basic steps for developing a video capture program

2.1 use avicap window class

The author uses the avicap window class to develop video capture programs. Avicap supports real-time video stream capturing and single-frame capturing, and provides control over the video source. The commonly used MCI control also provides digital video services. The overlay command set is provided for video superposition. However, these commands are mainly file-based operations and cannot meet the requirements of extracting data from the video cache 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 some advantages in video capturing. It can directly access the video buffer without generating intermediate files. Therefore, it is highly real-time and efficient. In addition, it can capture digital videos into a file.

2.2 basic steps for development

There are four steps to develop a video capture program:

(1) create a "capture window ".

Before capturing a video, you must create a "capture window" and perform all the capture and setting operations on this basis. The "capture window" can be created using the "cap create capture window" function of the avicap window class. The window style can be set to the wschild and ws_visible parameters.

A "capture window" is similar to a standard control and has the following functions:

* Capture video streams and audio streams to an AVI file;

* Dynamically connects to or disconnects from the video and audio input devices;

* Displays input video streams in real time in overlay or preview mode;

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

* Set the capture rate;

* Displays the dialog box for controlling the video source, video format, and video compression;

* Create, save, or load a color palette;

* Copy the image and related color palette to the clipboard;

* Save the captured single-frame image to a Dib format file.

(2) associating capture windows and drivers

A defined capture window cannot work. It must be associated with a device to obtain video signals. The capdriver connect function can be used to associate the capture window with its device driver.

(3) set the attributes of a video device

By setting each member variable of the tcaptureparms structure variable, you can control the sampling frequency, interrupt sampling buttons, and status behaviors of the device. After setting the tcaptureparms structure variable, you can use the letter capcapturesetsetup to make the setting take effect. You can also use cappreviewscale and cappreviewrate to set the preview ratio and speed, or use the default value of the device.

(4) Open Preview

You can use the capoverlay function to select whether to preview in Overlay mode to reduce system resource usage and accelerate video display. Use cappreview to start the preview function. Then, you can view the image from the camera on the screen.

You can create a basic video capture program in the above four steps. However, if you want to process the video data captured from the device, you need to use the capture window callback function to process it, for example, video data is obtained at a frame or video data is obtained by streams.

3. Delphi-based video capture program

Based on the system's special requirements on system access and processing speed, I chose Delphi as the development tool. The following describes how to develop a program to capture video data from a video device frame by frame. The function of the provided routine is to display the captured video on the screen and obtain the image data of each frame. The procedure is as follows:

(1) create a project and include avicap32.pas in uses.

(2) place a tpanel control on form1 and set name to "gcapvideoarea". This control is used to display videos. Then place two tbutton controls, one with the name "openvideo" and the other with the name "closevideo ".

(3) define global variables

Ghcapwnd: thandle; // defines the capture window handle.

Videostr: lpvideohdr; // you can obtain the structure variable of the Video Data Pointer, which is used in the callback function.

Capparms: tcaptureparms; // structure variable used to set device Properties

(4) write code

Write the following code in the click event of tbutton whose name is "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 coordinate

0, // y coordinate

Gcapvideoarea, width, // window width

Gcapvideoarea, handle, // window handle

0); // generally 0

In order to capture videos, a frame capture callback function videostreamcallback should be enabled. When capturing a video stream or the current device status, use the following functions:

Capsetcallbackonvideostream; // capture a video stream

Capsetcallbackonerror; // a device error is returned.

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 serial 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 video streams, you need to use a function to specify not to generate files. Otherwise, the AVI file will be automatically generated:

Capcapturesequencenofile (ghcapwnd );

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

Capoverlay (ghcapwnd, 1 );

Cappreview (ghcapwnd, 1 );

End;

Write the following code in the click event of tbutton whose name is "closevideo:

Procedure tform1.closevideoclick (Sender: tobject );

Begin

Capcaptureabort (ghcapwnd); // stop capture

Capdrivedisconnect (ghcapwnd); // disconnect the capture window from the drive

End;

Define the frame capture 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 the frame data to a memory. Note: The datapoint must first allocate space.

Copymemory (datapoint, videostr ^. lpdata, diblen );

......

End;

4 Conclusion

Flexible use of the avicap window callback function can meet various needs, but pay attention to the video data format captured from the video card and the image length and width refer to the video card parameters. In addition, some video cards support multiple formats and image lengths and widths through settings. Therefore, when restoring an image, pay attention to the parameters of the video card used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.