Capturing from a webcam using WinAPI in Delphi 7

I have a challenge: create a program that takes an image from a webcam when a button is clicked. An additional condition: do not use third-party components (for example, DSPack), just 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. 

The form has two buttons and a panel. The program successfully compiles and works well when launched for the first time; however, on the second and subsequent launches, a window appears that prompts you to select a device, but even after selecting it it does not work. I assume that after the first start the program will not return the camera driver to its original state.

This is true? If so, how can I fix this? If not, then why does the program not work in the second and other starts? Thanks for the suggestions.

+4
source share
1 answer

First of all, I can not really help you with your current code, then again I think that no one will be such an alternative.

I understand that you do not want to use the 3rd party component, but I think that using AVICAP32.DLL is not the best option.

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

It never let me down.

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

Good luck and have fun, Delphi.

+1
source

All Articles