Use Visual C ++ to capture images of AVI Files

Source: Internet
Author: User
Tags bmp image

An AVI file is a multimedia file. An Avi image is a video image. It is a riff description file used to obtain, edit, and demonstrate audio and video sequences. A general AVI file contains audio streams and video streams. Some special AVI files also contain a control path or MIDI path as an additional data stream.

Currently, there are many software programs for playback of AVI files, but most of them cannot read an image from an AVI video file and generate a file in BMP format. I have accumulated some experience in operating AVI files in the process of using the AVI file development project. For how to obtain image data of any frame from the AVI Video Stream and store it as a BMP file, among them, the most important thing is to obtain the image data of a specific frame from the AVI file. Therefore, I use the API functions provided by windows to implement a custom Cavi class for operating AVI files.

When using API functions to operate AVI files, you must use avifileinit () to initialize the AVI library. When the program ends, use avifileexit () to release the AVI library. Otherwise, the API functions cannot be used. Taking an AVI file containing a real-color image as an example, the following section describes how to implement some functions of the Cavi class. The cavicreate () function is used to read information about the AVI file and initialize members of the Cavi class, for example, the width, height, and information header structure of each image frame are defined based on the AVI file information. aviread (INT mframe) is used to read the mframe from the AVI file. The implementation code is shown as follows:

// Specifies the header file of the Cavi class;
Class Cavi file: // Avi class to process AVI Files
{
Public:
Int Cy; // Image Height
Int CX; // Image Width
File: // long m_maxframe;
Byte * pdata; // Store Image Data
Bitmapinfo * m_pbmi; // bitmap file information Header
Pavistream pavi; // AVI stream
Pavifile pfile; // AVI file pointer
Avifileinfo * PFI; file: // Avi Information
Bool aviread (INT mframe); // read the mframe of an AVI file
Cavi (); // standard Constructor
Cavicreate (cstring & string); // use the file name to initialize members of the AVI class.
Virtual ~ Cavi ();
};
// The implementation part of the Cavi class file;
Cavi: Cavi ()
{Avifileinit (); // initialize the AVI Library
Cx = 0; // defines image width, height, and other Members
Cy = 0;
M_pbmi = NULL;
Pdata = NULL;
File: // m_maxframe = 0;
PFI = NULL;
}
Cavi ::~ Cavi () // destructor, release pointer
{
// Avifileclose (pfile );
Avifileexit ();
If (pdata! = NULL)
Delete pdata;
Pdata = NULL;

If (m_pbmi! = NULL)
Delete m_pbmi;
M_pbmi = NULL;
If (PFI! = NULL)
Delete PFI;
PFI = NULL;
}
Cavi: cavicreate (cstring & string) // Read File initialization class
{
Hresult hr;
PFI = new avifileinfo;
HR = avifileopen (& pfile, // returned file pointer
String, // file name
Of_read, // mode to open file
Null );
HR = avifileinfo (pfile, file: // obtain Avi information and put it in PFI.
PFI,
Sizeof (avifileinfo)
);
Cx = PFI-> dwwidth; // image width and height
Cy = PFI-> dwheight;
HR = avifilegetstream (// convert Avi to video stream
Pfile,
& Pavi,
Streamtypevideo,
0 // long lparam
);
M_pbmi = new bitmapinfo; // defines the BMP Information header.
M_pbmi-> bmiheader. bibitcount = 24;
M_pbmi-> bmiheader. biclrimportant = 0;
M_pbmi-> bmiheader. biclrused = 0;
M_pbmi-> bmiheader. bicompression = bi_rgb;
M_pbmi-> bmiheader. biheight = Cy;
M_pbmi-> bmiheader. biwidth = Cx;
M_pbmi-> bmiheader. biplanes = 1;
M_pbmi-> bmiheader. bisize = sizeof (bitmapinfoheader );
M_pbmi-> bmiheader. bixpelspermeter = 0;
M_pbmi-> bmiheader. biypelspermeter = 0;
M_pbmi-> bmiheader. bisizeimage = Cx * Cy * 3;
Pdata = (byte *) New char [CX * Cy * 3]; // defines the buffer based on the information of the BMP image in Avi.
}
Bool Cavi: aviread (INT mframe) // read the M-frame data of the AVI file into the pdata buffer.
{
Hresult hr;
HR = avistreamread (pavi,
Mframe,
1,
Pdata,
CX * Cy * 3,
Null,
Null
);
If (hR = 0)
Return true;
Else
Return false;
}

For the API functions involved in the implementation of the above Cavi class, refer to the msdn provided by Microsoft. The pdata buffer in the Cavi class stores a specific frame of image data in the AVI file, And the m_pbmi In the Cavi class is the information structure of the BMP image file. In this case, the BMP Image File Header structure can be defined based on the image size, I will not talk more about the storage of BMP files due to space reasons, if you are interested, refer to the author's tutorial for "Visual C ++ 6.0 grayscale Bitmap Processing" (published in). This article describes how to access BMP files. The above programs are successfully compiled and run normally in Windows 6.0 and Visual C ++ environments.

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.