Delphi programming control camera

Source: Internet
Author: User
Youyou online (http://yousoft.hi.com.cn) XU Changyou

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 program the camera easily by using Microsoft's avicap32.dll in Delphi,
If you add your network programming level, it is no problem to implement a video-screen chat.
Take a look at 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 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;

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 to "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 the three buttons on the form, 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:
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: 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?
Well, of course, you can find a virtual camera. You can try SoftCam, which is a real software camera,
It can be simulated as a "real" camera, reminding you that you don't want to use this stuff 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 of classes and components that use Microsoft Direct Show and DirectX technology. It is designed to work on DirectX 9 and supports the 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 transfer it to the other party for display, but it seems simple to say it again, it is still difficult to implement.
 
Source code download:
[Click to download]

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.