)定义全局变量
CapWnd:THandle; //定义捕捉窗句柄
CapParms:TcaptureParms; //用于设置设备属性的结构变量
BMPINFO:TBitmapInfo; //BMP图像信息
)编码事件代码
开始按钮代码
CapWnd := capCreateCaptureWindow(我的窗口 WS_VISIBLE or WS_CHILD//窗口样式 //X坐标 //Y坐标 panelWidth //窗口宽panelHeight //窗口高 panelhandle //窗口句柄 ); //通常为
if CapWnd = then exit; //定义帧捕捉回调函数
CapSetCallbackOnFrame(CapWndFrameCallBack);
CapParmsdwRequestMicroSecPerFrame:=;
CapParmsfLimitEnabled:=FALSE;
CapParmsfCaptureAudio:=FALSE;
CapParmsfMCIControl:=FALSE;
CapParmsfYield:=TRUE;
CapParmsvKeyAbort:=VK_ESCAPE;
CapParmsfAbortLeftMouse:=False;
CapParmsfAbortRightMouse:=FALSE; //让设置生效
CapCaptureSetSetup(capWnd@CapParmssizeof(TCAPTUREPARMS));
CapPreviewRate(capWnd); //设置预览视频的频率
CapCaptureSequenceNoFile(capWnd); //如果要捕捉视频流则要使用函数来指定不生成文件不然会自动生成AVI文件 CapDriverConnect(CapWnd); //连接摄像头设备第二个参数是个序号当系统中装有多个显示驱动程序时其值分别依次为到总个数如果有多个摄像头那么就是>>
capGetVideoFormat(capWnd @BMPINFOsizeof(TBitmapInfo)); //取得视频图像数据头
CapPreviewScale(capWndTRUE); //是否缩放
CapOverlay(capWndtrue); //指定是否使用叠加模式true为使用否则为falseCapPreview(capWndtrue);
回调函数代码
var hd:Thandle;
jpg:TJpegImage;
memStream :TMemoryStream;
Bitmap:TBitmap;
begin //将数据显在Image
Bitmap:=TBitmapCreate;
BitmapWidth :=BMPINFObmiHeaderbiWidth;
// New size of Bitmap
BitmapHeight:=BMPINFObmiHeaderbiHeight;
hd:= DrawDibOpen; DrawDibDraw(hdBitmapcanvashandle_
BMPINFOBmiHeaderbiwidthBMPINFObmiheaderbiheight_
@BMPINFObmiHeaderlpVHdr^lpDataBMPINFObmiHeaderbiWidth_
BMPINFObmiHeaderbiheight);
DrawDibClose(hd); //发送数据
memStream := TMemoryStreamCreate;
jpg := TJpegImageCreate;
jpgAssign(Bitmap);
jpgCompressionQuality := ; //jpg压缩质量
jpgJPEGNeeded;
jpgCompress;
jpgSaveToStream(memStream);
jpgFree; //因为UDP数据包有大小限制这里如果超出部分就没有传输完全可以发几次发出去
FormIdUDPClientBroadcastEnabled:=true;//用广播功能
if memStreamSize>FormIdUDPClientBufferSize then
//向X网段广播端口
FormIdUDPClientSendBuffer(memStreamMemory^FormIdUDPClientBufferSize) else FormIdUDPClientSendBuffer(memStreamMemory^memStreamSize);
memStreamFree;
BitmapFree;
End;
停止代码
capCaptureAbort(CapWnd); //停止捕捉 capDriverDisconnect(CapWnd); //将捕捉窗同驱动器断开
完整的视频聊天软件服务器代码
unit Unit;
interfaceuses Windows Messages SysUtils Variants Classes Graphics Controls Forms Dialogs StdCtrls ExtCtrlsVFW IdBaseComponent IdComponent IdUDPBase IdUDPClientjpeg;type TForm = class(TForm) Panel: TPanel;
Button: TButton;
Button: TButton;
IdUDPClient: TIdUDPClient;
procedure ButtonClick(Sender: TObject);
procedure ButtonClick(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var Form: TForm;
CapWnd:THandle; //定义捕捉窗句柄
CapParms:TcaptureParms; //用于设置设备属性的结构变量
BMPINFO:TBitmapInfo; //BMP图像信息
implementation{$R *dfm}
function FrameCallBack(hWnd: HWND; lpVHdr: PVIDEOHDR): LongInt;stdcall;
var hd:Thandle;
jpg:TJpegImage;
memStream :TMemoryStream;
Bitmap:TBitmap;
begin //将数据显在Image
Bitmap:=TBitmapCreate;
BitmapWidth :=BMPINFObmiHeaderbiWidth;
// New size of Bitmap
BitmapHeight:=BMPINFObmiHeaderbiHeight;
hd:= DrawDibOpen;
DrawDibDraw(hdBitmapcanvashandleBMPINFOBmiHeaderbiwidthBMPINFO_
bmiheaderbiheight@BMPINFObmiHeader_
lpVHdr^lpDataBMPINFObmiHeaderbiWidthBMPINFObmiHeaderbiheight);
DrawDibClose(hd);
//发送数据
memStream := TMemoryStreamCreate;
jpg := TJpegImageCreate; jpgAssign(Bitmap);
jpgCompressionQuality := ;
//jpg压缩质量
jpgJPEGNeeded;
jpgCompress;
jpgSaveToStream(memStream);
jpgFree;
//因为UDP数据包有大小限制这里如果超出部分就没有传输完全可以发几次发出去
FormIdUDPClientBroadcastEnabled:=true;//用广播功能
if memStreamSize>FormIdUDPClientBufferSize then
//向X网段广播端口
FormIdUDPClientSendBuffer(memStreamMemory^FormIdUDPClientBufferSize)
else
FormIdUDPClientSendBuffer(memStreamMemory^memStreamSize);
memStreamFree;
BitmapFree;
end;
procedure TFormButtonClick(Sender: TObject);
begin
CapWnd := capCreateCaptureWindow(我的窗口 WS_VISIBLE or WS_CHILD//窗口样式
//X坐标
//Y坐标
panelWidth //窗口宽
panelHeight //窗口高
panelhandle //窗口句柄
); //通常为
if CapWnd = then exit; //定义帧捕捉回调函数
CapSetCallbackOnFrame(CapWndFrameCallBack);
CapParmsdwRequestMicroSecPerFrame:=;
CapParmsfLimitEnabled:=FALSE;
CapParmsfCaptureAudio:=FALSE;
CapParmsfMCIControl:=FALSE;
CapParmsfYield:=TRUE;
CapParmsvKeyAbort:=VK_ESCAPE;
CapParmsfAbortLeftMouse:=False;
CapParmsfAbortRightMouse:=FALSE; //让设置生效
CapCaptureSetSetup(capWnd@CapParmssizeof(TCAPTUREPARMS));
CapPreviewRate(capWnd); //设置预览视频的频率
CapCaptureSequenceNoFile(capWnd); //如果要捕捉视频流则要使用函数来指定不生成文件不然会自动生成AVI文件
CapDriverConnect(CapWnd); //连接摄像头设备第二个参数是个序号当系统中装有多个显示驱动程序时其值分别依次为到总个数如果有多个摄像头那么就是>> capGetVideoFormat(capWnd @BMPINFOsizeof(TBitmapInfo)); //取得视频图像数据头
CapPreviewScale(capWndTRUE); //是否缩放
CapOverlay(capWndtrue); //指定是否使用叠加模式true为使用否则为false CapPreview(capWndtrue);end;procedure TFormButtonClick(Sender: TObject);
begin capCaptureAbort(CapWnd); //停止捕捉
capDriverDisconnect(CapWnd); //将捕捉窗同驱动器断开
end;
end