|
CapWnd:THandle; //定义捕捉窗句柄 CapParms:TcaptureParms; //用于设置设备属性的结构变量 BMPINFO:TBitmapInfo; //BMP图像信息 |
CapWnd := capCreateCaptureWindow('我的窗口', WS_VISIBLE or WS_CHILD,//窗口样式 0, //X坐标 0, //Y坐标 panel1.Width, //窗口宽 panel1.Height, //窗口高 panel1.handle, //窗口句柄 0); //通常为0 if CapWnd = 0 then exit; //定义帧捕捉回调函数 CapSetCallbackOnFrame(CapWnd,FrameCallBack); CapParms.dwRequestMicroSecPerFrame:=1; CapParms.fLimitEnabled:=FALSE; CapParms.fCaptureAudio:=FALSE; CapParms.fMCIControl:=FALSE; CapParms.fYield:=TRUE; CapParms.vKeyAbort:=VK_ESCAPE; CapParms.fAbortLeftMouse:=False; CapParms.fAbortRightMouse:=FALSE; //让设置生效 CapCaptureSetSetup(capWnd,@CapParms,sizeof(TCAPTUREPARMS)); CapPreviewRate(capWnd,33); //设置预览视频的频率 CapCaptureSequenceNoFile(capWnd); //如果要捕捉视频流,则要使用函数来指定不生成文件,不然会自动生成AVI文件 CapDriverConnect(CapWnd,0); //连接摄像头设备,第二个参数是个序号,当系统中装有多个显示驱动程序时,其值分别依次为0到总个数如果有多个摄像头,那么就是0->1->2 capGetVideoFormat(capWnd, @BMPINFO,sizeof(TBitmapInfo)); //取得视频图像数据头 CapPreviewScale(capWnd,TRUE); //是否缩放 CapOverlay(capWnd,true); //指定是否使用叠加模式,true为使用,否则为falseCapPreview(capWnd,true); |
var hd:Thandle; jpg:TJpegImage; memStream :TMemoryStream; Bitmap:TBitmap; begin //将数据显在Image, Bitmap:=TBitmap.Create; Bitmap.Width :=BMPINFO.bmiHeader.biWidth; // New size of Bitmap Bitmap.Height:=BMPINFO.bmiHeader.biHeight; hd:= DrawDibOpen; DrawDibDraw(hd,Bitmap.canvas.handle,0,0,_ BMPINFO.BmiHeader.biwidth,BMPINFO.bmiheader.biheight,_ @BMPINFO.bmiHeader,lpVHdr^.lpData,0,0,BMPINFO.bmiHeader.biWidth,_ BMPINFO.bmiHeader.biheight,0); DrawDibClose(hd); //发送数据 memStream := TMemoryStream.Create; jpg := TJpegImage.Create; jpg.Assign(Bitmap); jpg.CompressionQuality := 10; //jpg压缩质量 jpg.JPEGNeeded; jpg.Compress; jpg.SaveToStream(memStream); jpg.Free; //因为UDP数据包有大小限制,这里如果超出部分,就没有传输,完全可以发几次发出去 Form1.IdUDPClient1.BroadcastEnabled:=true;//用广播功能 if memStream.Size>Form1.IdUDPClient1.BufferSize then //向192.168.0.X网段广播,端口 9001 Form1.IdUDPClient1.SendBuffer('192.168.0.255',9001,memStream.Memory^,Form1.IdUDPClient1.BufferSize) else Form1.IdUDPClient1.SendBuffer('192.168.0.255',9001,memStream.Memory^,memStream.Size); memStream.Free; Bitmap.Free; End; |
capCaptureAbort(CapWnd); //停止捕捉 capDriverDisconnect(CapWnd); //将捕捉窗同驱动器断开 |
unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,VFW, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient,jpeg;type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; Button2: TButton; IdUDPClient1: TIdUDPClient; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; 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:=TBitmap.Create; Bitmap.Width :=BMPINFO.bmiHeader.biWidth; // New size of Bitmap Bitmap.Height:=BMPINFO.bmiHeader.biHeight; hd:= DrawDibOpen; DrawDibDraw(hd,Bitmap.canvas.handle,0,0,BMPINFO.BmiHeader.biwidth,BMPINFO._ bmiheader.biheight,@BMPINFO.bmiHeader,_ lpVHdr^.lpData,0,0,BMPINFO.bmiHeader.biWidth,BMPINFO.bmiHeader.biheight,0); DrawDibClose(hd); //发送数据 memStream := TMemoryStream.Create; jpg := TJpegImage.Create; jpg.Assign(Bitmap); jpg.CompressionQuality := 10; //jpg压缩质量 jpg.JPEGNeeded; jpg.Compress; jpg.SaveToStream(memStream); jpg.Free; //因为UDP数据包有大小限制,这里如果超出部分,就没有传输,完全可以发几次发出去 Form1.IdUDPClient1.BroadcastEnabled:=true;//用广播功能 if memStream.Size>Form1.IdUDPClient1.BufferSize then //向192.168.0.X网段广播,端口 9001 Form1.IdUDPClient1.SendBuffer('192.168.0.255',9001,memStream.Memory^,Form1.IdUDPClient1.BufferSize) else Form1.IdUDPClient1.SendBuffer('192.168.0.255',9001,memStream.Memory^,memStream.Size); memStream.Free; Bitmap.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin CapWnd := capCreateCaptureWindow('我的窗口', WS_VISIBLE or WS_CHILD,//窗口样式 0, //X坐标 0, //Y坐标 panel1.Width, //窗口宽 panel1.Height, //窗口高 panel1.handle, //窗口句柄 0); //通常为0 if CapWnd = 0 then exit; //定义帧捕捉回调函数 CapSetCallbackOnFrame(CapWnd,FrameCallBack); CapParms.dwRequestMicroSecPerFrame:=1; CapParms.fLimitEnabled:=FALSE; CapParms.fCaptureAudio:=FALSE; CapParms.fMCIControl:=FALSE; CapParms.fYield:=TRUE; CapParms.vKeyAbort:=VK_ESCAPE; CapParms.fAbortLeftMouse:=False; CapParms.fAbortRightMouse:=FALSE; //让设置生效 CapCaptureSetSetup(capWnd,@CapParms,sizeof(TCAPTUREPARMS)); CapPreviewRate(capWnd,33); //设置预览视频的频率 CapCaptureSequenceNoFile(capWnd); //如果要捕捉视频流,则要使用函数来指定不生成文件,不然会自动生成AVI文件 CapDriverConnect(CapWnd,0); //连接摄像头设备,第二个参数是个序号,当系统中装有多个显示驱动程序时,其值分别依次为0到总个数如果有多个摄像头,那么就是0->1->2 capGetVideoFormat(capWnd, @BMPINFO,sizeof(TBitmapInfo)); //取得视频图像数据头 CapPreviewScale(capWnd,TRUE); //是否缩放 CapOverlay(capWnd,true); //指定是否使用叠加模式,true为使用,否则为false CapPreview(capWnd,true);end;procedure TForm1.Button2Click(Sender: TObject); begin capCaptureAbort(CapWnd); //停止捕捉 capDriverDisconnect(CapWnd); //将捕捉窗同驱动器断开 end; end. |
3.2.1新建一个工程,命名为Project2.dpr
3.2.2在程序窗口Form2上放置一个image控件,该控件用于接收的图像内容,再放置一个Tbutton控件,caption为"接收",,放置一个UDPServer组件,这里用indy的IdUDPServer用来接收网络视频,如图示:
接收按钮代码:
IdUDPServer1.DefaultPort:=9001; //接收端口 IdUDPServer1.Active:=true; //启用 |
var jpg:TJpegImage;begin try jpg := TJpegImage.Create; jpg.LoadFromStream(Adata); Image1.Picture.Bitmap.Assign(jpg); jpg.Free; exceptend;end; |
unit Unit2; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, ExtCtrls,jpeg,IdSocketHandle; type TForm1 = class(TForm) Image1: TImage; IdUDPServer1: TIdUDPServer; Button1: TButton; procedure Button1Click(Sender: TObject); procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin IdUDPServer1.DefaultPort:=9001; //接收端口 IdUDPServer1.Active:=true; //启用 end; procedure TForm1.IdUDPServer1UDPRead(Sender: TObject;AData: TStream; ABinding: TIdSocketHandle); var jpg:TJpegImage; begin try jpg := TJpegImage.Create; jpg.LoadFromStream(Adata); Image1.Picture.Bitmap.Assign(jpg); jpg.Free; except end; end; end. |
视频聊天软件服务器 |
聊天软件客户端 |