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.
source share