在Delphi 7中使用avicap32从网络摄像头捕获图像

时间:2019-01-23 19:57:30

标签: delphi delphi-7 webcam

我在项目中使用的是Delphi 7。

该项目的目的是从网络摄像头捕获图像。该程序似乎检测到了网络摄像头,并显示了一个窗口,我可以在其中选择所需的网络摄像头,但是屏幕只是黑色,什么也没显示。

代码如下:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    SaveDialog1: TSaveDialog;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    hWndC : THandle;
    CapturingAVI : bool;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const WM_CAP_START                  = WM_USER;
const WM_CAP_STOP                   = WM_CAP_START + 68;
const WM_CAP_DRIVER_CONNECT         = WM_CAP_START + 10;
const WM_CAP_DRIVER_DISCONNECT      = WM_CAP_START + 11;
const WM_CAP_SAVEDIB                = WM_CAP_START + 25;
const WM_CAP_GRAB_FRAME             = WM_CAP_START + 60;
const WM_CAP_SEQUENCE               = WM_CAP_START + 62;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;

Function capCreateCapt(dwStyle : longint; x : integer; y : integer; nWidth : integer;nHeight : integer;ParentWin  : HWND;  nId : integer): HWND; STDCALL EXTERNAL 'AVICAP32.DLL';

procedure TForm1.FormCreate(Sender: TObject);
begin
  CapturingAVI := false;
  hWndC := 0;
  SaveDialog1.Options := [ofHideReadOnly, ofNoChangeDir, ofPathMustExist]
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  hWndC := capCreateCaptureWindowA('My Own Capture Window', WS_CHILD or WS_VISIBLE, Panel1.Left, Panel1.Top, Panel1.Width, Panel1.Height, Form1.Handle, 0);
  if hWndC <> 0 then
    SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0 );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if hWndC <> 0 then begin
    SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
    hWndC := 0;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if hWndC <> 0 then
    SendMessage(hWndC, WM_CAP_START, 0, 0);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  SendMessage(hWndC, WM_CAP_GRAB_FRAME, 0, 0);
end;

end.

1 个答案:

答案 0 :(得分:0)

您对capCreateCaptureWindowA()的声明是错误的,并且如果要将网络摄像头显示在TPanel内,则将错误的参数值传递给它。

尝试以下方法:

function capCreateCaptureWindowA(const lpszWindowName : PAnsiChar; dwStyle : DWORD; x : integer; y : integer; nWidth : integer; nHeight : integer; hwwndParent : HWND; nId : integer): HWND; stdcall; external 'AVICAP32.DLL';

procedure TForm1.Button1Click(Sender: TObject);
begin
  hWndC := capCreateCaptureWindowA('My Own Capture Window', WS_CHILD or WS_VISIBLE, 0, 0, Panel1.Width, Panel1.Height, Panel1.Handle, 0);
  ...
end;