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

利用Delphi編程控制攝像頭(一)

2013-11-23 17:34:52  來源: Delphi編程 
    你的電腦有沒有攝像頭?看到別人用QQ玩視屏你會不會去想怎麼實現的?這裡介紹使用DELPHI使用MS的AVICAPDLL就可輕松的實現對攝像頭編程如果再加上你的網絡編程水平實現一個視屏聊天就不成什麼問題了

    看看下面代碼的代碼

    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編程控制攝像頭(二)


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