将做工程过程较好的代码段备份一下,下面的代码段是关于一段将多张图片生成AVI视频的C++的代码,应该能对各朋友有一些帮助。
#pragma once
#include <vfw.h>
#pragma comment(lib,"Vfw32.lib")
class ImageToAVI : public base::_class{
protected:
PAVIFILE _file;
PAVISTREAM _aviStream,
_aviCompressed;
int _frames,_delay;
void _initMember();
bool _initStream(LPBITMAPINFOHEADER pbih,HWND parent);
public:
ImageToAVI();
~ImageToAVI();
bool Create(LPCTSTR fn,int delay,int cx = 0,int cy = 0);
void Close();
bool AddFrame(base::gdi::Bitmap& bmp,HWND parent = 0);
bool IsOK(){return _aviStream!=0;}
};
#include "stdafx.h"
#include "AVIVideo.h"
inline WORD PaletteSize(WORD color){
if(color>8) return 0;
}
int CreateDibHeader(base::Memory<byte>& data,int cx,int cy,int bits){
base::gdi::Bitmap bmp;
bmp.CreateCompatibleBitmap(cx,cy);
if(bits==0) bits = 32;
DWORD hLen = sizeof(BITMAPINFOHEADER) + PaletteSize(bits);
data.SetLength(hLen+16);
data.Zero();
LPBITMAPINFO pbi = (LPBITMAPINFO)data.Handle();
HPALETTE hpal = (HPALETTE)::GetStockObject(DEFAULT_PALETTE);
base::gdi::DC dc;
dc.GetDC();
dc.RealizePalette();
pbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if(!count) goto exit;
pbi->bmiHeader.biCompression = 0;
pbi->bmiHeader.biBitCount = bits;
pbi->bmiHeader.biClrUsed = PaletteSize(bits);
exit:
dc.SelectPalette(hpal);
if(!count) return 0;
return hLen;
}
ImageToAVI::ImageToAVI(){
AVIFileInit();
_aopts[0] = &_opts;
_initMember();
}
void ImageToAVI::_initMember(){
_size.SetValue(0,0);
_frames = 0;
_file = NULL;
_aviStream = NULL;
_aviCompressed = NULL;
::ZeroMemory(&_opts,sizeof(_opts));
}
bool ImageToAVI::Create(LPCTSTR fn, int delay, int cx, int cy){
if(_file) return 0;
base::FileSystem fs;
fs.Initialize(fn);
fs.Delete();
HRESULT hr = AVIFileOpen(&_file,(LPWSTR)fn,OF_WRITE|OF_CREATE,NULL);
if(hr!=AVIERR_OK) return 0;
_size.SetValue(cx,cy);
_delay = delay;
return 1;
}
bool ImageToAVI::_initStream(LPBITMAPINFOHEADER pbih,HWND parent){
AVISTREAMINFO strhdr;
::ZeroMemory(&strhdr,sizeof(strhdr));
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 1;
strhdr.dwSuggestedBufferSize = pbih->biSizeImage;
SetRect(&strhdr.rcFrame,0,0,pbih->biWidth,pbih->biHeight);
HRESULT hr = AVIFileCreateStream(_file,&_aviStream,&strhdr);
if(_aviStream==0){
PrintD(L"ImageToAVI::_initStream: _aviStream = 0");
return 0;
}
PrintD(L"ImageToAVI::_initStream: AVISaveOptions failed");
return 0;
}
hr = AVIMakeCompressedStream(&_aviCompressed,_aviStream,&_opts,NULL);
if(_aviCompressed==0){
base::Warning(L"Compressed");
PrintD(L"ImageToAVI::_initStream: _aviCompressed = 0");
return 0;
}
if(hr != AVIERR_OK){
PrintD(L"ImageToAVI::_initStream: AVIStreamSetFormat failed");
return 0;
}
return 1;
}
void ImageToAVI::Close(){
if(_aviStream) AVIStreamClose(_aviStream);
if(_aviCompressed) AVIStreamClose(_aviCompressed);
if(_file) AVIFileClose(_file);
_initMember();
}
ImageToAVI::~ImageToAVI(){
Close();
AVIFileExit();
}
bool ImageToAVI::AddFrame(base::gdi::Bitmap& bmp,HWND hwnd){
if(!_file){
PrintD(L"ImageToAVI::AddFrame: object not init,call Create first");
return false;
}
if(bmp.IsNull()){
PrintD(L"ImageToAVI::AddFrame: bmp is null");
return 0;
}
if(_size.Value==0) _size = bmp.Size();
if(bmp.Size()!=_size){
if(!bmp.Resize(_size.x,_size.y)) return 0;
}
base::Memory<byte> dib;
int dibLen = bmp.GetDib(dib,32);
if(dibLen==0) return false;
LPBITMAPINFO pbi = (LPBITMAPINFO)dib.Handle();
LPBITMAPINFOHEADER pbih = &pbi->bmiHeader;
if(_aviCompressed==0){
if(!_initStream(pbih,hwnd)) return 0;
}
pbih->biSize + PaletteSize(32),
NULL,
NULL);
if(hr!=AVIERR_OK){
Close();
return false;
}
_frames++;
return true;
}
网友评论