Use Delphi programming to control the camera (figure)

Source: Internet
Author: User

Does your computer have a camera? Will you think about how to achieve it when you see someone playing the video screen with QQ? Here we will introduce how to use MS in Delphi
Avicap32.dll can easily program the camera. If you add your network programming level, it is no problem to implement a video screen chat.

Look at the generation below
Code:

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
Capcreatecapture0000wa (lpszwindowname: pchar; dwstyle: longint; X:
Integer;
Y: integer; nwidth: integer; nheight: integer; parentwin:
Hwnd;
NID: integer): hwnd; stdcall external 'avicap32. dll ';

 
The above code is the definition of a function and constant that we mainly use.

Okay. Open your Delphi, create a project, and add the above definition.

 
Create a new window, place a panel, and add a button. Set caption to "start". A global variable, VAR hwndc: thandle, must be defined here;
The code for the start button is as follows:

Begin
Hwndc: = capcreatecapturew.wa ('my own capture
Window ', ws_child or ws_visible
, Panel1.left, panel1.top, panel1.width, panel1.height, form1.handle, 0 );

Hwndc
: = Capcreatecapturew.wa ('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;

Run F9. How can I see the camera's video screen? So how to stop? Set the button caption"
Stop ". The Code is as follows:

If hwndc <> 0 then begin
Sendmessage (hwndc,
Wm_cap_driver_disconnect, 0, 0 );
Hwndc: = 0;
End;

 
When the video is captured, how can we save it? The following are saved in two ways: BMP static graph and AVI animation.

Put three buttons on the form.
Go and set caption to "save BMP", "Start video", and "Stop video" respectively. The code for 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 recording
If hwndc <> 0 then
Begin
Sendmessage (hwndc, wm_cap_file_set_capture_filea, 0,
Longint (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 several images to see, or you can record them as avi and then enjoy them slowly.

Program running effect:

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: tcloseaction );
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
Capcreatecapture0000wa (lpszwindowname: pchar;
Dwstyle: longint; X:
Integer; Y: integer; nwidth: integer;
Nheight: integer; parentwin:
Hwnd; NID: integer): hwnd;
Stdcall external 'avicap32. dll ';

Implementation

{$ R
*. DFM}

Procedure tform1.button1click (Sender: tobject );
Begin
Hwndc
: = Capcreatecapturew.wa ('my own capture Window', ws_child or
Ws_visible
, Panel1.left, panel1.top, panel1.width, panel1.height, form1.handle, 0 );

Hwndc
: = Capcreatecapturew.wa ('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,
Longint (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: tobject; var action: tcloseaction );
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, but you want to see the effect of the program, can you?

Of course, you can find a virtual camera. You can try softcam.
It is a real software camera that can be simulated as a "real" camera. I would like to remind you that you may not use this camera to cheat mm or Gg on chat software such as QQ or MSN.

 
For camera programming, you can also look at this set of VCL components: dspack, dspack is a set
Show and DirectX technology classes and components, designed for DirectX 9, support system Win9x, me, 2000 and Windows XP.

 
Now, let's talk about this. As for how to implement video-screen chat, it's up to you to compress the data and display it to the other party. However, it seems simple to come back, it is still difficult to implement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.