给你个实例,要安装directx8.1!
在delphi6.0+directshow+dspack2.1下调试通过!
unit videocap;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DSUtil, StdCtrls, DSPack, DirectShow, Menus, ExtCtrls,Buttons;
type
TVideoForm = class(TForm)
FilterGraph: TFilterGraph;
Filter: TFilter;
SampleGrabber: TSampleGrabber;
Panel1: TPanel;
videocapture: TBitBtn;
Panel2: TPanel;
videocut: TButton;
Panel3: TPanel;
GroupBox1: TGroupBox;
VideoWindow: TVideoWindow;
GroupBox2: TGroupBox;
Image: TImage;
GroupBox3: TGroupBox;
Label1: TLabel;
videosource: TComboBox;
setvideo: TBitBtn;
setformat: TBitBtn;
Label2: TLabel;
videodevices: TComboBox;
videosave: TBitBtn;
savestop: TBitBtn;
Label3: TLabel;
Edit1: TEdit;
setsave: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure videocutClick(Sender: TObject);
procedure videocaptureClick(Sender: TObject);
procedure videosourceChange(Sender: TObject);
procedure setvideoClick(Sender: TObject);
private
{ D閏larations priv閑s }
public
{ D閏larations publiques }
end;
var
VideoForm: TVideoForm;
SysDev: TSysDevEnum;
CapFile: WideString = 'd:/capture.avi';
implementation
uses activex, format;
{$R *.dfm}
procedure TVideoForm.FormCreate(Sender: TObject);
var
i: integer;
begin
SysDev:= TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
if (SysDev.CountFilters = 0) then
begin
Messagebox(0,'没安装视频采集卡或没开启视频输入设备!','视频初始化错误',id_ok);
SysDev.Destroy;
// CaptureGraph.Disconnect;
Application.Terminate;
exit;
end;
if SysDev.CountFilters > 0 then
for i := 0 to SysDev.CountFilters - 1do
begin
videodevices.Items.Add(SysDev.Filters.FriendlyName);
videodevices.ItemIndex:=0;
end;
end;
procedure TVideoForm.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
SysDev.Free;
FilterGraph.ClearGraph;
FilterGraph.Active := false;
end;
procedure TVideoForm.videocutClick(Sender: TObject);
begin
SampleGrabber.GetBitmap(Image.Picture.Bitmap);
end;
procedure TVideoForm.videocaptureClick(Sender: TObject);
var
in_type:ToleEnum;
tmp_crossbar:IAMCrossbar;
multiplexer: IBaseFilter;
Writer: IFileSinkFilter;
begin
FilterGraph.ClearGraph;
FilterGraph.Active := false;
SysDev:= TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
Filter.BaseFilter.Moniker := SysDev.GetMoniker(0);
Filter.FilterGraph := FilterGraph;
FilterGraph.Active := true;
with FilterGraph as IcaptureGraphBuilder2do
// set the output filename
SetOutputFileName(MEDIASUBTYPE_Avi, PWideChar(CapFile), multiplexer, Writer);
// Connect Video preview (VideoWindow)
with FilterGraph as IcaptureGraphBuilder2do
if Filter.BaseFilter.DataLength > 0 then
RenderStream(@PIN_CATEGORY_PREVIEW, nil, Filter as IBaseFilter,
SampleGrabber as IBaseFilter , VideoWindow as IBaseFilter);
// Connect Video capture streams
with FilterGraph as IcaptureGraphBuilder2do
if Filter.FilterGraph <> nil then
RenderStream(@PIN_CATEGORY_CAPTURE, nil, Filter as IBaseFilter,
nil, multiplexer as IBaseFilter);
FilterGraph.Play;
end;
procedure TVideoForm.videosourceChange(Sender: TObject);
var
in_pinlist:integer;
in_type:ToleEnum;
tmp_crossbar:IAMCrossbar;
begin
SysDev:= TSysDevEnum.Create(AM_KSCATEGORY_CROSSBAR);//设置crossbar属性
FilterGraph.ClearGraph;
FilterGraph.Active := false;
Filter.BaseFilter.Moniker := SysDev.GetMoniker(0);
Filter.FilterGraph := FilterGraph;
FilterGraph.Active := true;
with FilterGraph as ICaptureGraphBuilder2do
FindInterface(nil,nil,Filter as IBaseFilter,IAMCrossbar,tmp_crossbar);
tmp_crossbar.get_CrossbarPinInfo(true,videosource.ItemIndex,in_pinlist,in_type);
tmp_crossbar.Route(0,in_type-1);
end;
procedure TVideoForm.setvideoClick(Sender: TObject);
begin
if FilterGraph.Active then
ShowFilterPropertyPage(Self.Handle, filter as IBaseFilter);
end;
end.