U
u2m
Unregistered / Unconfirmed
GUEST, unregistred user!
DirectShow的抓图接口太占资源,我把它单提出来,没有放在初始化接口的时候一并初始化它,否则资源占的很厉害,我提出来之后,关键一句报错了,不知原因,帮忙看看。
用的directshow9.0接口,不过和8.1接口只是那个类型变了个名字而已
type
// DirectShow Media Player.
TDSMP = record
Initialized: boolean;
// DirectShow stuff.
GraphBuilder: IGraphBuilder;
MediaControl: IMediaControl;
MediaSeeking: IMediaSeeking;
MediaPosition: IMediaPosition;
MediaEventEx: IMediaEvent;
BasicAudio: IBasicAudio;
BasicVideo: IBasicVideo;
VideoWindow: IVideoWindow;
SampleGrabber: ISampleGrabber;
VideoFrameStep: IVideoFrameStep;
//AMVideoProcAmp:IAMVideoProcAmp;
// Video info.
VideoAvail: boolean;
VideoWidth: integer;
VideoHeight: integer;
VideoBitRate: integer;
VideoFPS: single;
end;
procedure TFrmPlay.Capture1Frame(var _dsmp: TDSMP);
var
Filter: IBaseFilter;
MediaType: TAMMediaType;
VideoInfoHeader: TVideoInfoHeader;
BitmapInfo: TBitmapInfo;
Bitmap: HBitmap;
Buffer: Pointer;
BufferSize: Integer;
strtime: string;
PicFileName: string;
begin
strtime := FormatDateTime('yymmddhhnnss', now);
PicFileName := 'D:/' + '1.bmp';
with _dsmpdo
begin
if failed(CoCreateInstance(CLSID_SampleGrabber, nil, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, Filter)) then
exit;
Filter.QueryInterface(IID_ISampleGrabber, SampleGrabber);
GraphBuilder.AddFilter(Filter, 'Grabber');
Filter := nil;
ZeroMemory(@MediaType, SizeOf(TAMMediaType));
MediaType.majortype := MEDIATYPE_Video;
MediaType.subtype := MEDIASUBTYPE_RGB24;
MediaType.formattype := FORMAT_VideoInfo;
SampleGrabber.SetMediaType(MediaType);
SampleGrabber.SetBufferSamples(True);
SampleGrabber.GetConnectedMediaType(MediaType);
ZeroMemory(@VideoInfoHeader, SizeOf(TVideoInfoHeader));
//执行后,下句报错,pbFormat为nil
CopyMemory(@VideoInfoHeader, MediaType.pbFormat, SizeOf(VideoInfoHeader));
ZeroMemory(@BitmapInfo, SizeOf(TBitmapInfo));
CopyMemory(@BitmapInfo, @VideoInfoHeader.bmiHeader, SizeOf(VideoInfoHeader.bmiHeader));
Bitmap := CreateDIBSection(0, BitmapInfo, DIB_RGB_COLORS, Buffer, 0, 0);
SampleGrabber.GetCurrentBuffer(BufferSize, Buffer);
frmPicBrowse.imgBrowse.Picture.Bitmap.Handle := Bitmap;
frmPicBrowse.imgBrowse.Picture.SaveToFile(PicFileName);
frmPicBrowse.Show;
end;
end;
用的directshow9.0接口,不过和8.1接口只是那个类型变了个名字而已
type
// DirectShow Media Player.
TDSMP = record
Initialized: boolean;
// DirectShow stuff.
GraphBuilder: IGraphBuilder;
MediaControl: IMediaControl;
MediaSeeking: IMediaSeeking;
MediaPosition: IMediaPosition;
MediaEventEx: IMediaEvent;
BasicAudio: IBasicAudio;
BasicVideo: IBasicVideo;
VideoWindow: IVideoWindow;
SampleGrabber: ISampleGrabber;
VideoFrameStep: IVideoFrameStep;
//AMVideoProcAmp:IAMVideoProcAmp;
// Video info.
VideoAvail: boolean;
VideoWidth: integer;
VideoHeight: integer;
VideoBitRate: integer;
VideoFPS: single;
end;
procedure TFrmPlay.Capture1Frame(var _dsmp: TDSMP);
var
Filter: IBaseFilter;
MediaType: TAMMediaType;
VideoInfoHeader: TVideoInfoHeader;
BitmapInfo: TBitmapInfo;
Bitmap: HBitmap;
Buffer: Pointer;
BufferSize: Integer;
strtime: string;
PicFileName: string;
begin
strtime := FormatDateTime('yymmddhhnnss', now);
PicFileName := 'D:/' + '1.bmp';
with _dsmpdo
begin
if failed(CoCreateInstance(CLSID_SampleGrabber, nil, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, Filter)) then
exit;
Filter.QueryInterface(IID_ISampleGrabber, SampleGrabber);
GraphBuilder.AddFilter(Filter, 'Grabber');
Filter := nil;
ZeroMemory(@MediaType, SizeOf(TAMMediaType));
MediaType.majortype := MEDIATYPE_Video;
MediaType.subtype := MEDIASUBTYPE_RGB24;
MediaType.formattype := FORMAT_VideoInfo;
SampleGrabber.SetMediaType(MediaType);
SampleGrabber.SetBufferSamples(True);
SampleGrabber.GetConnectedMediaType(MediaType);
ZeroMemory(@VideoInfoHeader, SizeOf(TVideoInfoHeader));
//执行后,下句报错,pbFormat为nil
CopyMemory(@VideoInfoHeader, MediaType.pbFormat, SizeOf(VideoInfoHeader));
ZeroMemory(@BitmapInfo, SizeOf(TBitmapInfo));
CopyMemory(@BitmapInfo, @VideoInfoHeader.bmiHeader, SizeOf(VideoInfoHeader.bmiHeader));
Bitmap := CreateDIBSection(0, BitmapInfo, DIB_RGB_COLORS, Buffer, 0, 0);
SampleGrabber.GetCurrentBuffer(BufferSize, Buffer);
frmPicBrowse.imgBrowse.Picture.Bitmap.Handle := Bitmap;
frmPicBrowse.imgBrowse.Picture.SaveToFile(PicFileName);
frmPicBrowse.Show;
end;
end;