skeeph
skeeph

Reputation: 490

Taking a picture from a web-camera using WinAPI in Delphi 7

I have a challange: create a program which takes an image from a web-camera when a button is pressed. Additional condition: do not use third-party components (like DSPack), just the WinAPI. I wrote the following code.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

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

function capCreateCaptureWindowA(lpszWindowName : PCHAR;
    dwStyle : longint;
    x : integer;
    y : integer;
    nWidth : integer;
    nHeight : integer;
    ParentWin : HWND;
    nId : integer): HWND;
    stdcall external 'AVICAP32.DLL';

var
    Form1: TForm1;

implementation
{$R *.dfm}


var hWndC : THandle;

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);

if hWndC <> 0 then 
    SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); 

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  hWndC := 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;

end.

There are two buttons and a panel on the form. The program compiles successfully, and works well when launched the first time; however, at second and subsequent launches there appears a window which offers to select a device, but even after selecting it is not working. I would guess that after the first launch, the program is not returning the camera's driver to its original state.

Is this so? If it is, how can I correct it? If not, why does the program not work on second and other launches? Thanks for suggestions.

Upvotes: 4

Views: 14809

Answers (1)

Kapytanhook
Kapytanhook

Reputation: 856

First of all, I wont be able to really help you with your current code, then again i think no one will so here is an alternative.

I understand you don't want to use a 3e party component but I think using AVICAP32.DLL is not a better option.

This page offers a great way to use webcams under windows, its very reliable and allows you to set any cam property. Its all opensource, easy to use and based on native windows DirectX libraries.

It has never failed me.

http://www.delphibasics.info/home/delphibasicsprojects/directxdelphiwebcamcaptureexample

Good luck and have fun, Delphi rocks.

Upvotes: 3

Related Questions