你的电脑有没有摄像头?看到别人用QQ玩视屏你会不会去想怎么实现的?这里介绍使用DELPHI使用MS的 AVICAP32.DLL就可轻松的实现对摄像头编程,如果再加上你的网络编程水平,实现一个视屏聊天就不成什么问题了。
看看下面代 码的代码:
const WM_CAP_START = WM_USER;
const WM_CAP_STOP = WM_CAP_START + ;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + ;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + ;
const WM_CAP_SAVEDIB = WM_CAP_START + ;
const WM_CAP_GRAB_FRAME = WM_CAP_START + ;
const WM_CAP_SEQUENCE = WM_CAP_START + ;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + ;
const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+
const WM_CAP_SET_OVERLAY =WM_CAP_START+
const WM_CAP_SET_PREVIEW =WM_CAP_START+
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +;
const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +;
const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +;
const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +;
const WM_CAP_SET_SCALE=WM_CAP_START+
const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+
function capCreateCaptureWindowA(lpszWindowName : PCHAR; dwStyle : longint; x : integer;
y : integer;nWidth : integer;nHeight : integer;ParentWin : HWND;
nId : integer): HWND;STDCALL EXTERNAL 'AVICAP32.DLL';
上面的代码就是我们主要用到的一个函数和常量的定义。
好了,打开你的Delphi,新建一个工程,将上面的定义加上吧。
新建一个窗口,放个Panel上去,添加一个按钮,Caption设置为"开始"这里需要定义一个全局变量,var hWndC : THandle; 开始按钮代码如下:
begin
hWndC := capCreateCaptureWindowA('My Own Capture Window',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,Panel1.Width,Panel1.Height,Form1.Handle,);
hWndC := capCreateCaptureWindowA('My Own Capture Window',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,Panel1.Width,Panel1.Height,Form1.Handle,);
if hWndC <> then
begin
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, , );
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, , );
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, , );
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, , );
SendMessage(hWndC, WM_CAP_SET_SCALE, , );
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, , );
SendMessage(hWndC, WM_CAP_SET_OVERLAY, , );
SendMessage(hWndC, WM_CAP_SET_PREVIEW, , );
end;
按F9运行一下,怎么样,是不是可以看到摄像头的视屏了?那怎么停下来?再加个按钮caption设置成" 停止" 。代码如下:
if hWndC <> then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, , );
hWndC := ;
end;
视屏截到了,怎么把它给保存下来呢?下面按两种方式保存,一个是BMP静态图,一个是AVI动画。
再放三个按钮到窗体上 去,caption分别设置成"保存BMP"、"开始录像"、"停止录像",三个按钮的代码分别如下:
//保存BMP
if hWndC <> then begin
SendMessage(hWndC,WM_CAP_SAVEDIB,,longint(pchar('c:/test.bmp')));
end;
// 开始录像
if hWndC <> then
begin
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,, Longint(pchar('c:/test.avi')));
SendMessage(hWndC, WM_CAP_SEQUENCE, , );
end;
//停止录像
if hWndC <> then begin
SendMessage(hWndC, WM_CAP_STOP, , );
end;
再运行看看吧。。 可以保存几张图看看,也可以录成AVI以后慢慢欣赏。
程序运行效果:
完整的程序代码如下:
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 + ;
const WM_CAP_DRIVER_CONNECT = WM_CAP_START + ;
const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + ;
const WM_CAP_SAVEDIB = WM_CAP_START + ;
const WM_CAP_GRAB_FRAME = WM_CAP_START + ;
const WM_CAP_SEQUENCE = WM_CAP_START + ;
const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + ;
const WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+
const WM_CAP_SET_OVERLAY =WM_CAP_START+
const WM_CAP_SET_PREVIEW =WM_CAP_START+
const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +;
const WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +;
const WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +;
const WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +;
const WM_CAP_SET_SCALE=WM_CAP_START+
const WM_CAP_SET_PREVIEWRATE=WM_CAP_START+
function capCreateCaptureWindowA(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 := capCreateCaptureWindowA('My Own Capture Window',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,Panel1.Width,Panel1.Height,Form1.Handle,);
hWndC := capCreateCaptureWindowA('My Own Capture Window',WS_CHILD or WS_VISIBLE ,Panel1.Left,Panel1.Top,Panel1.Width,Panel1.Height,Form1.Handle,);
if hWndC <> then
begin
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, , );
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, , );
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, , );
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, , );
SendMessage(hWndC, WM_CAP_SET_SCALE, , );
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, , );
SendMessage(hWndC, WM_CAP_SET_OVERLAY, , );
SendMessage(hWndC, WM_CAP_SET_PREVIEW, , );
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if hWndC <> then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, , );
hWndC := ;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if hWndC <> then begin
SendMessage(hWndC,WM_CAP_SAVEDIB,,longint(pchar('c:/test.bmp')));
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if hWndC <> then
begin
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,, Longint(pchar('c:/test.avi')));
SendMessage(hWndC, WM_CAP_SEQUENCE, , );
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if hWndC <> then begin
SendMessage(hWndC, WM_CAP_STOP, , );
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if hWndC <> then begin
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, , );
end;
end;
end.
如果电脑没有摄像头,但又想看看程序的效果,可以么?
当然可以,找个虚拟摄像头不就搞定,大家可以试试SoftCam这个软件,它 是一个名副其实的软件摄像机,能模拟成为“真实的”摄像机,提醒一下各位,大家可不要用这个东东用在QQ,MSN等聊天软件上欺骗MM或GG啊。
关于摄像头编程,大家也可以看看这组VCL组件:DSPack,DSPack是一套使用微软Direct Show和DirectX技术的类和组件,设计工作于DirectX ,支持系统Win9X, ME, 和Windows XP。
好了,就介绍这些了,至于视屏聊天怎么实现,就看你的了,无非是把数据压缩传输给对方,显示出来,不过话又说回来,看似简单,实现起来还有些难度的。