Leath online (
Http://yousoft.hi.com.cn) Xu Changyou
Does your computer do not came to? See how others use QQ to play with the screen, will you think?
Here is an easy implementation of camera programming using Delphi Avicap32.dll using MS.
If you add your network programming level, you don't have any problems with a video chat.
Take a look at the code of the code below:
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;
Const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START 63
Const WM_CAP_SET_OVERLAY = WM_CAP_START 51
Const WM_CAP_SET_PREVIEW = WM_CAP_START 50
Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START 6;
Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START 2;
Const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START 3;
Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START 5;
Const WM_CAP_SET_SCALE = WM_CAP_START 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START 52
Function CapcreateCaptureWindowa (LPSZWindownAme: Pchar; DWStyle: longint; x: integer;
Y: Integer; NWIDTH: Integer; NHEIGHT: Integer; Parentwin: hw
NID: Integer: hw; stdcall external 'avicap32.dll';
The above code is the definition of a function and constant of our main use.
Ok, open your Delphi, create a new project, add the definition above.
Create a window, put a panel, add a button, and the CAPTION is set to "start"
Here you need to define a global variable, VAR HWNDC: THANDLE;
The start button code is as follows:
Begin
HWndc: = CapcreateCaptureWindowa ('My OWN Capture Window ", WS_CHILD OR WS_VISIBLE, Panel1.Wid, Panel1.Height, Form1.handle, 0);
HWndc: = CapcreateCaptureWindowa ('My OWN Capture Window ", WS_CHILD OR WS_VISible, Panel1.Width, Panel1.Height, Form1.Handle, 0); if HWNDC <> 0 THEN
Begin
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage (HWNDC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_SCALE, 1, 0);
SendMessage (HWNDC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage (HWNDC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage (HWNDC, WM_CAP_SET_PREVIEW, 1, 0);
END;
Press F9 to run, how can I see the camera of the camera?
How can I stop? Add a button CAPTION is set to "stop"
code show as below:
IF HWNDC <> 0 THEN Begin
SendMessage (HWNDC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
HWNDC: = 0;
END;
The view is intercepted, how can I save it?
The following is saved in two ways, one is a BMP static diagram, one is an AVI animation.
Put three buttons to the form, CAPTION is set to "save BMP", "Start Recording", "Stop Recording"
The code of the three buttons is as follows:
/ / Save BMP
IF HWNDC <> 0 THEN Begin
SendMessage (HWNDC, WM_CAP_SAVEDIB, 0, Longint (Pchar ('c: /test.bmp'))))
END;
// Start video
IF hwndc <> 0 THEN
Begin
SendMessage (HWNDC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, LONINT (Pchar ('c: /test.avi'))));
SendMessage (HWNDC, WM_CAP_SEQUENCE, 0, 0);
END;
// Stop recording
IF HWNDC <> 0 THEN Begin
SendMessage (HWNDC, WM_CAP_STOP, 0, 0);
END;
Run it again. . You can save a few pictures, you can also enjoy it after recording AVI.
Program operation:
Screen.width-430) this.width = Screen.width-430 "align = center border = 0>
The complete program code is as follows:
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, ExtCtrls;
Type
TFORM1 = Class (TFORM)
Panel1: TPANEL;
Button1: tbutton; button2: tbutton;
Button3: tbutton;
Button4: tbutton;
Button5: tbutton;
Procedure Button1Click (Sender: TOBJECT);
Procedure Button2Click (Sender: TOBJECT);
Procedure Button3Click (Sender: TOBJECT);
Procedure Button4Click (Sender: TOBJECT);
Procedure Button5Click (Sender: TOBJECT);
Procedure formclose (Sender: Tobject; VAR Action: Tclosection);
Private
HWNDC: THANDLE;
public
{Public declarations}
END;
VAR
FORM1: TFORM1;
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;
Const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START 63
Const WM_CAP_SET_OVERLAY = WM_CAP_START 51
Const WM_CAP_SET_PREVIEW = WM_CAP_START 50
Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START 6;
Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START 2;
Const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START 3;
Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START 5;
Const WM_CAP_SET_SCALE = WM_CAP_START 53
Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START 52
Function CapcreateCaptureWindowa (LPSZWindowname: Pchar;
le;:: integer; nwidth: integer;
NHEIGHT: Integer; Parentwin: hwnd; nid: integer: hw;
Stdcall External 'Avicap32.dll';
IMPLEMentation
{$ R * .dfm}
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); 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
Begin
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage (HWNDC, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage (HWNDC, WM_CAP_SET_SCALE, 1, 0);
SendMessage (HWNDC, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage (HWNDC, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage (HWNDC, WM_CAP_SET_PREVIEW, 1, 0);
END;
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 Begin
SendMessage (HWNDC, WM_CAP_SAVEDIB, 0, Longint (Pchar ('c: /test.bmp'))))
END;
END;
Procedure TFORM1.BUTTON4CLICK (Sender: TOBJECT);
Begin
IF hwndc <> 0 THEN
Begin
SendMessage (HWNDC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, LONINT (Pchar ('c: /test.avi'))));
SendMessage (HWNDC, WM_CAP_SEQUENCE, 0, 0);
END;
END;
Procedure TFORM1.BUTTON5CLICK (Sender: TOBJECT);
Begin
IF HWNDC <> 0 THEN Begin
SendMessage (HWNDC, WM_CAP_STOP, 0, 0);
END;
END;
Procedure TFORM1.FORMCLOSE (Sender: TpoBject; VAR Action: Tclosection);
Begin
IF HWNDC <> 0 THEN Begin
SendMessage (HWNDC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
END;
END;
End.
If the computer does not have a camera, you want to see the effect of the program, can you?
Oh, of course, you can find a virtual camera, you can try SoftCam software. It is a veritable software camera that can simulate "real" camera, remind you, don't use this Dongdong Deceive MM or GG on chat software such as QQ, MSN.
With regard to camera programming, you can also look at this group of VCL components: DSPACK, DSPack is a set of class and components that use Microsoft Direct Show and DirectX technology, design work in DirectX 9, support system Win9x, ME, 2000, and Windows XP.
Ok, let's introduce these, as for how to achieve the video screen, look at you, nothing more than the data compression is transferred to each other, showing it, but said that it seems simple, it is still a bit difficult.
Source code download:
[ click to download ]