Webcam with Delphi ( III )

Follow with the project management of a webcam with Delphi I present the procedure to stop recording a video sequence:

You have to create a TButton called "PararVideo" and in the onclick event type the following:

PROCEDURE TForm1.PararVideoClick(Sender: TObject);
BEGIN
IF ventana <> 0 THEN
BEGIN
SendMessage(ventana, WM_CAP_STOP, 0, 0);
END;
END;

Related links

Webcam con Delphi ( I )


Webcam con Delphi ( II )

Webcam with Delphi ( II )

Below you will see new uses for using a webcam with Delphi.

STORING A SEQUENCE OF VIDEO
New components of the form:

tSaveDialog,Properties:
- Name = Guardar

tButtonProperties:
- Name = BtnAlmacenarVideo
- Caption = AlmacenarVideo

In the Onclic event of the TButton write:

PROCEDURE TForm1.BtnAlmacenarVideoClick(Sender: TObject);
BEGIN
   IF Ventana <> 0 THEN
   BEGIN
      Guardar.Filter := 'Fichero AVI (*.avi)*.avi';
      Guardar.DefaultExt := 'avi';
      Guardar.FileName := 'FicheroAvi';
      IF Guardar.Execute THEN
      BEGIN
         SendMessage(Ventana, WM_CAP_FILE_SET_CAPTURE_FILEA, 0,
            Longint(pchar(Guardar.Filename)));
         SendMessage(Ventana, WM_CAP_SEQUENCE, 0, 0);
      END;
   END;
END;



Save a picture from the capture window
Add a tButton

tButtonProperties:
- Name = BtnGuardarImagen
- Caption = Guardar Imagen

Code of the Botón

PROCEDURE TForm1.BtnGuardarImagenClick(Sender: TObject); 
BEGIN
IF Ventana <> 0 THEN
BEGIN
Guardar.FileName := 'Captura de la imagen';
Guardar.DefaultExt := 'bmp';
Guardar.Filter := 'Fichero Bitmap (*.bmp)*.bmp';
IF Guardar.Execute THEN
SendMessage(Ventana, WM_CAP_SAVEDIB, 0,
longint(pchar(Guardar.FileName)));
END;
END;




Related articles

Webcam con Delphi ( I )

Webcam con Delphi ( III )

Webcam with Delphi (I)

I present you the software that allows manage your Webcam with Delphi.

First you have to install the software on your system "Microsoft Video for Windows SDK" that contains the library avicap32.dll.

Among the functions contained use "capCreateCaptureWindowA" to initialize the driver and image capture.

After handling the capture window will have to use the "SendMessage" making it easier and greatly simplifies the developers work.

Now we are goint to the program:

We add global variables:

Ventana: hwnd; //Handle de la ventana de captura

In the section "const" we write


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;
WM_CAP_EDIT_COPY = WM_CAP_START + 30;
WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;


Section "implementation":


FUNCTION capCreateCaptureWindowA(lpszWindowName: PCHAR; dwStyle: longint; x: integer; y: integer; nWidth: integer; nHeight: integer; ParentWin: HWND; nId: integer): HWND; STDCALL EXTERNAL 'AVICAP32.DLL';


is the call of the external library Avicap32.dll

Elements of the interface:

-Botón "iniciar" (Pressing start capturing the image from the Webcam)

-Botón "detener" (Press to stop capturing)

-Control de Imagen "tImage" (We called "Image1")




The code to be included within the buttons is as follows:
Botón "Iniciar"
PROCEDURE TForm1.Button1Click(Sender: TObject);
BEGIN
   Ventana := capCreateCaptureWindowA('Ventana de captura',
      WS_CHILD OR WS_VISIBLE, image1.Left, image1.Top, image1.Width,
      image1.Height, form1.Handle, 0);
   IF Ventana <> 0 THEN
   BEGIN
      TRY
         SendMessage(Ventana, WM_CAP_DRIVER_CONNECT, 0, 0);
         SendMessage(Ventana, WM_CAP_SET_PREVIEWRATE, 40, 0);
         SendMessage(Ventana, WM_CAP_SET_PREVIEW, 1, 0);
      EXCEPT
         RAISE;
      END;
   END
   ELSE
   BEGIN
      MessageDlg('Error al conectar Webcam', mtError, [mbok], 0);
   END;
END;

Botón "Detener"



PROCEDURE TForm1.Button2Click(Sender: TObject);
BEGIN
   IF Ventana <> 0 THEN
   BEGIN
      SendMessage(Ventana, WM_CAP_DRIVER_DISCONNECT, 0, 0);
      Ventana := 0;
   END;
END;

In the event:  Onclose we make a call to the procedure included in the button "Detener".

And that's all for now, we will be adding more items and tips.

Related links:
Webcam con Delphi ( II )
WebCam con Delphi ( III )