TVideoGrabber的使用(一)捕捉摄像头

使用TVideoGrabber捕捉摄像头,相当容易,只需几句代码即可解决问题,首先我们新建一个工程,然后从控件面板上拉取一个TVideoGrabber控件到窗体中,然后再在窗体上放置四个Button,分别将Caption设为播放,暂停,停止,关闭,name分别设为BtnPlay,BtnPause,BtnStop,BtnClose将TVideoGrabber1的VideoSource的属性设置为vs_VideoCaptureDevice

为各个按钮添加点击事件,完整代码如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, VidGrab;

type
  TForm1 = class(TForm)
    VideoGrabber1: TVideoGrabber;
    BtnPlay: TButton;
    BtnPause: TButton;
    BtnStop: TButton;
    BtnClose: TButton;
    procedure BtnPlayClick(Sender: TObject);
    procedure BtnPauseClick(Sender: TObject);
    procedure BtnStopClick(Sender: TObject);
    procedure BtnCloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//开启视屏

procedure TForm1.BtnPlayClick(Sender: TObject);
begin
  videoGrabber1.StartPreview;
end;

//暂停或继续

procedure TForm1.BtnPauseClick(Sender: TObject);
begin
 if BtnPause.Caption = '暂停' then
    begin
      btnPause.Caption := '继续';
      VideoGrabber1.PausePreview;
    end
 else
    begin
      btnPause.Caption := '暂停';
      VideoGrabber1.ResumePreview;
    end;
end;

//停止视屏

procedure TForm1.BtnStopClick(Sender: TObject);
begin
  videoGrabber1.StopPreview;
end;

//关闭窗体

procedure TForm1.BtnCloseClick(Sender: TObject);
begin
  close;
end;

end.

{以上代码在Delphi7+XP}

posted @ 2018-09-20 16:51  熊大熊二  阅读(488)  评论(0编辑  收藏  举报