你的电脑有没有摄像头?看到别人用QQ玩视屏你会不会去想怎么实现的?这里介绍使用DELPHI使用MS的AVICAP
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 AVICAPDLL;
上面的代码就是我们主要用到的一个函数和常量的定义
好了打开你的Delphi新建一个工程将上面的定义加上吧
新建一个窗口放个Panel上去添加一个按钮Caption设置为开始这里需要定义一个全局变量var hWndC : THandle; 开始按钮代码如下
begin
hWndC := capCreateCaptureWindowA(My Own Capture WindowWS_CHILD or WS_VISIBLE PanelLeftPanelTopPanelWidthPanelHeightFormHandle);
hWndC := capCreateCaptureWindowA(My Own Capture WindowWS_CHILD or WS_VISIBLE PanelLeftPanelTopPanelWidthPanelHeightFormHandle);
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;
按F运行一下怎么样是不是可以看到摄像头的视屏了?那怎么停下来?再加个按钮caption设置成停止 代码如下
if hWndC <> then begin
SendMessage(hWndC WM_CAP_DRIVER_DISCONNECT );
hWndC := ;
end;
视屏截到了怎么把它给保存下来呢?下面按两种方式保存一个是BMP静态图一个是AVI动画
再放三个按钮到窗体上去caption分别设置成保存BMP开始录像停止录像三个按钮的代码分别如下
//保存BMP
if hWndC <> then begin
SendMessage(hWndCWM_CAP_SAVEDIBlongint(pchar(c:\testbmp)));
end;
//开始录像
if hWndC <> then
begin
SendMessage(hWndCWM_CAP_FILE_SET_CAPTURE_FILEA Longint(pchar(c:\testavi)));
SendMessage(hWndC WM_CAP_SEQUENCE );
end;
//停止录像
if hWndC <> then begin
SendMessage(hWndC WM_CAP_STOP );
end;
再运行看看吧可以保存几张图看看也可以录成AVI以后慢慢欣赏
程序运行效果
利用Delphi编程控制摄像头(二)