非常感谢各位的帮助。特别是Another-eYes 和 bethouvnlue的帮助。
在Another_eYes的提示下我成功的读取了Avi的各帧图象。
不过我将Another_eYes 的代码略做了改动:
具体的代码如下:
function GetAviFrame(AviFilename : String;
Index: Integer;
var bmp: TBitmap): boolean;
ËμÃ÷:
avifilename: aviÎļtÃû
index: òaè¡μÄèåoÅ
bmp: ′æ·Å·μ»Øí¼Ïó, èç1ûÎanilÔò×Ô¶ˉ½¨á¢ò»¸öbitmap. èç1û′æÔúÔò°′bmp
¶¨òåμÄ′óD¡′æ·ÅstretchoóμÄí¼Ïó.
·μ»ØÖμ: true 3é1|, false ꧰ü.
′úÂë:
uses windows, graphics;
interface
const
streamtypeAUDIO : longint = $73647561;
streamtypeVIDEO : longint = $73646976;
type
TAVIStream = record
fccType : longint;
fccHandler : longint;
dwFlags : longint;
dwCaps : longint;
wPriority : word;
wLanguage : word;
dwScale : longint;
dwRate : longint;
dwStart : longint;
dwLength : longint;
dwInitialFrames : longint;
dwSuggestedBufferSize : longint;
dwQuality : longint;
dwSampleSize : longint;
rcFrame : TRect;
dwEditCount : longint;
dwFormatChangeCount : longint;
Name : array [0..64] of char;
end;
PAVIStream = ^TAVIStream;
PAVIFile = pointer;
TAVIFileInfo = record
dwMaxBytesPerSec : longint;
dwFlags : longint;
dwCaps : longint;
dwStreams : longint;
dwSuggestedBufferSize : longint;
dwWidth : longint;
dwHeight : longint;
dwScale : longint;
dwRate : longint;
dwLength : longint;
dwEditCount : longint;
szFileType : array[0..63] of char;
end;
PAVIFileInfo = ^TAVIFileInfo;
TAVIStreamInfo = record
fccType : longint;
fccHandler : longint;
dwFlags : longint;
dwCaps : longint;
wPriority : word;
wLanguage : word;
dwScale : longint;
dwRate : longint;
dwStart : longint;
dwLength : longint;
dwInitialFrames : longint;
dwSuggestedBufferSize : longint;
dwQuality : longint;
dwSampleSize : longint;
rcFrame : TRect;
dwEditCount : longint;
dwFormatChangeCount : longint;
szName : array[0..63] of char;
end;
PAVIStreamInfo = ^TAVIStreamInfo;
function AVIFileOpen(avifile : pointer;
filename : pchar;
mode : integer;
CLSID : pointer) : integer;
stdcall;
external 'avifil32.dll' index 16;
function AVIFileRelease(avifile : pointer) : longint;
stdcall;
external 'avifil32.dll' index 20;
function AVIFileGetStream(avifile : pointer;
avistream : PAVIStream;
streamtype : longint;
lParam : longint) : integer;
stdcall;
external 'avifil32.dll' index 11;
function AVIStreamGetFrameOpen(avistream : PAVIStream;
bitmapwanted : pointer) : pointer;
stdcall;
external 'avifil32.dll' index 42;
procedure AVIStreamGetFrameClose(pget : pointer);
stdcall;
external 'avifil32.dll' index 41;
function AVIStreamGetFrame(getframe : pointer;
position : longint) : pointer;
stdcall;
external 'avifil32.dll' index 40;
procedure AVIStreamRelease(avistream : PAVIStream);
stdcall;
external 'avifil32.dll' index 53;
function AVIStreamInfo(pstream : PAVIStream;
psi : PAVISTREAMINFO;
lsize : longint) : integer;
stdcall;
external 'avifil32.dll' index 44;
function GetAviFrame(AviFilename : String;
Index: Integer;
var bmp: TBitmap): boolean;
implementation
function GetAviFrame(AviFilename : String;
Index: Integer;
var bmp: TBitmap): boolean;
var
FAviFile : Pointer;
FVideoStream : Pointer;
FGetFrame : Pointer;
info : TAVIStreamInfo;
FFrameWidth, FFrameHeight : Integer;
FStartFrame, FStopFrame : Integer;
// image : PBitmapInfoHeader;
image : pointer;
imagestart : Integer;
begin
result := false;
if (AVIFileOpen(@favifile, pchar(AviFileName), 0, nil) <> 0) then
exit;
if (AVIFileGetStream(favifile, @fvideostream, streamtypeVIDEO, 0) <> 0) then
begin
AVIFileRelease(favifile);
exit;
end;
AVIStreamInfo(fvideostream, @info, sizeof(info));
with infodo
begin
fFrameWidth := rcframe.right - rcframe.left;
fFrameHeight := rcframe.bottom - rcframe.top;
fStartFrame := dwStart;
fStopFrame := dwLength - 1;
end;
if (index <fstartframe) or (index > fstopframe) then
begin
AVIStreamRelease(fvideostream);
AVIFileRelease(favifile);
exit;
end;
fgetframe := AVIStreamGetFrameOpen(fvideostream, nil);
if (fgetframe = nil) then
begin
AVIStreamRelease(fvideostream);
AVIFileRelease(favifile);
exit;
end;
// image := AVIStreamGetFrame(fgetframe, Index);
image := AVIStreamGetFrame(fGetFrame, Index);
if assigned(image) then
begin
if not assigned(bmp) then
begin
bmp := tbitmap.create;
bmp.width := fframewidth;
bmp.height := fframeheight;
end
else
if bmp.empty then
begin
bmp.width := fframewidth;
bmp.height := fframeheight;
end;
// imagestart := image^.biSize + image^.biClrUsed * 4;
imagestart := TBitmapInfoHeader(image^).biSize +
TBitmapInfoHeader(image^).biClrUsed * 4;
StretchDIBits(bmp.canvas.handle, 0, 0, bmp.width, bmp.height,
0, 0, fframewidth, fframeheight,
pchar(image) + imagestart,
TBitmapInfo(image^), 0, SRCCOPY);
result := true;
end;
AVIStreamGetFrameClose(fgetframe);
AVIStreamRelease(fvideostream);
AVIFileRelease(favifile);
end;
end.