Using Visual C + + to realize image interception of AVI file

Source: Internet
Author: User
Tags definition bmp image bool header

AVI file is what we call multimedia files, the so-called AVI image is a video image, the file is a riff description file, which is used to obtain, edit, demo audio, video sequence. The general AVI file contains audio streams and video streams, and some special AVI also contains a control path or MIDI path as an additional data stream.

There are many softwares for playing AVI file now, but most of them can't read one frame image from avi video file and produce BMP format file. The author accumulates some experience in the operation of AVI file in the process of developing project of AVI file, and how to realize the image data of any frame from AVI video stream and store it into BMP file, in which the key is to get the image data of a specific frame from AVI file. For this purpose, I implemented a custom Cavi class using the API functions provided by Windows to manipulate AVI files.

When using API functions to operate AVI files, be sure to use Avifileinit () to initialize the AVI library, the end of the program with Avifileexit () Release AVI library, otherwise the API function can not be used. An example of an AVI file containing true color images is given, in which the cavicreate () function is used to read AVI file information and initialize members of Cavi class, for example, to define the width, height and cavi of each frame image according to AVI file information. The information header structure of each frame image and so on; function aviread (int mframe) is used to read the Mframe frame from an AVI file. The implementation code appears as follows:

Cavi class header file definition;
Class Cavi File://AVI classes, processing AVI files
{
Public
int cy;//Image High
int cx;//Image Width
File://long M_maxframe;
BYTE *pdata;//inch stored image data
Bitmapinfo *m_pbmi;//bitmap File information header
Pavistream Pavi;//avi Flow
Pavifile Pfile;//avi file pointer
Avifileinfo * PFI; File://AVI Information
BOOL aviread (int mframe);//Read mframe frame of AVI file
Cavi ()//Standard constructor
Cavicreate (CString &string); Initialize the members of an AVI class with a filename
Virtual ~cavi ();
};
Cavi class file Implementation section;
Cavi::cavi ()
{Avifileinit ()//initializing AVI Library
cx=0;//definition image wide, high, etc 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 initializes the class
{
HRESULT hr;
Pfi=new Avifileinfo;
hr = AVIFileOpen (&pfile,//returned file pointer
string,//file name
Of_read,//mode to open file with
NULL);
Hr= Avifileinfo (pfile, file://get AVI information, into the PFI
Pfi
sizeof (Avifileinfo)
);
cx=pfi->dwwidth;//image wide and high
cy=pfi->dwheight;
Hr=avifilegetstream (//To turn AVI into a video stream
Pfile
&pavi,
Streamtypevideo,
0//long LParam
);
M_pbmi=new bitmapinfo;//define 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 a buffer based on the information of BMP images in avi
}
BOOL cavi::aviread (int mframe)//To read the M frame data of AVI file into pdata buffer
{
HRESULT hr;
Hr= Avistreamread (Pavi,
Mframe,
1,
PData,
Cx*cy*3,
Null
Null
);
if (hr==0)
return TRUE;
Else
return FALSE;
}


The API functions involved in the above Cavi class implementation section refer to the MSDN provided by Microsoft. Cavi class of pdata buffer storage AVI file in the specific frame image data, and Cavi class of M_PBMI for BMP image file information structure, at this time can be based on the size of the image to define BMP image file header structure, the storage of BMP files, due to the length of the reasons, I do not speak more , interested readers can see the author's flops "Visual c++6.0 development Grayscale Bitmap Processing" (Tenkine software column published in 2001.9.10), which describes how to access BMP files. The above procedures in Windows2000, Visual c++6.0 Environment smoothly compiled through, normal operation.

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.