Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 How can I capture an image from a video source?


How can I capture an image from a video source?   

    For a complete example, you need to get the Microsoft Video for Windows SDK. The following simple example shows how to open the default video capture device, grab a frame from the device, save a frame from the device to the disk as a .BMP (device independent bitmap) file, record a .AVI file (with sound, but without preview), and close the device. Note: You must have a video capture device installed for this example to work.

    Example:
    unit Unit ;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
       Dialogs, ExtCtrls, StdCtrls;
    type
      TForm  = class(TForm)
        Panel : TPanel;
        OpenVideo: TButton;
        CloseVideo: TButton;
        GrabFrame: TButton;
        SaveBMP: TButton;
        StartAVI: TButton;
        StopAVI: TButton;
        SaveDialog : TSaveDialog;
        procedure FormCreate(Sender: TObject);
        procedure OpenVideoClick(Sender: TObject);
        procedure CloseVideoClick(Sender: TObject);
        procedure GrabFrameClick(Sender: TObject);
        procedure SaveBMPClick(Sender: TObject);
        procedure StartAVIClick(Sender: TObject);
        procedure StopAVIClick(Sender: TObject);
      private
        { Private declarations }
        hWndC : THandle;
        CapturingAVI : bool;
      public
        { Public declarations }
      end;
    var
      Form : TForm ;
    implementation
    {$R *.DFM}
    const WM_CAP_START                  = WM_USER;
    const WM_CAP_STOP                   = WM_CAP_START + 68;
    const WM_CAP_DRIVER_CONNECT         = WM_CAP_START +  0;
    const WM_CAP_DRIVER_DISCONNECT      = WM_CAP_START +   ;
    const WM_CAP_SAVEDIB                = WM_CAP_START + 25;
    const WM_CAP_GRAB_FRAME             = WM_CAP_START + 60;
    const WM_CAP_SEQUENCE               = WM_CAP_START + 62;
    const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START +  20;
    function capCreateCaptureWindowA(lpszWindowName : PCHAR;
                                     dwStyle : longint;
                                     x : integer;
                                     y : integer;
                                     nWidth : integer;
                                     nHeight : integer;
                                     ParentWin  : HWND;
                                     nId : integer): HWND;
                                     STDCALL EXTERNAL 'AVICAP32.DLL';
    procedure TForm .FormCreate(Sender: TObject);
    begin
      CapturingAVI := false;
      hWndC := 0;
      SaveDialog .Options :=
        [ofHideReadOnly, ofNoChangeDir, ofPathMustExist]
    end;
    procedure TForm .OpenVideoClick(Sender: TObject);
    begin
      hWndC := capCreateCaptureWindowA('My Own Capture Window',
                                       WS_CHILD or WS_VISIBLE ,
                                       Panel .Left,
                                       Panel .Top,
                                       Panel .Width,
                                       Panel .Height,
                                       Form .Handle,
                                       0);
      if hWndC <> 0 then
        SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
    end;
    procedure TForm .CloseVideoClick(Sender: TObject);
    begin
      if hWndC <> 0 then begin
        SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
       hWndC := 0;
       end;
    end;
    procedure TForm .GrabFrameClick(Sender: TObject);
    begin
      if hWndC <> 0 then
        SendMessage(hWndC, WM_CAP_GRAB_FRAME, 0, 0);
    end;
    procedure TForm .SaveBMPClick(Sender: TObject);
    begin
      if hWndC <> 0 then begin
        SaveDialog .DefaultExt := 'bmp';
        SaveDialog .Filter := 'Bitmap files (*.bmp)|*.bmp';
        if SaveDialog .Execute then
          SendMessage(hWndC,
                      WM_CAP_SAVEDIB,
                      0,
                      longint(pchar(SaveDialog .FileName)));
      end;
    end;
    procedure TForm .StartAVIClick(Sender: TObject);
    begin
      if hWndC <> 0 then begin
        SaveDialog .DefaultExt := 'avi';
        SaveDialog .Filter := 'AVI files (*.avi)|*.avi';
        if SaveDialog .Execute then begin
           CapturingAVI := true;
           SendMessage(hWndC,
                       WM_CAP_FILE_SET_CAPTURE_FILEA,
                       0,
                       Longint(pchar(SaveDialog .FileName)));
           SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
        end;
      end;
    end;
    procedure TForm .StopAVIClick(Sender: TObject);
    begin
      if hWndC <> 0 then begin
        SendMessage(hWndC, WM_CAP_STOP, 0, 0);
        CapturingAVI := false;
      end;
    end;
    end.
    

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use