熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

用Delphi開發視頻聊天軟件(二)

2013-11-23 17:35:43  來源: Delphi編程 
    )定義全局變量

    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


From:http://tw.wingwit.com/Article/program/Delphi/201311/24744.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.