抓取摄像头图片

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, VfW, Clipbrd, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    AviPanel: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure capbmp;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  m_hCapWnd:Hwnd;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
   dwSize:Integer;
   setBmp : BITMAPINFO ;
begin
   try
      m_hCapWnd := capCreateCaptureWindow('',
        WS_CHILD or WS_VISIBLE,0,0,AviPanel.width,AviPanel.height,AviPanel.Handle,0);
                              //avipanel 就是你要显示的视频窗口的;可以是form ,panel等
                   //AviPanel.width就是显示的宽度,AviPanel.height显示的高度;
      if(capDriverConnect(m_hCapWnd,0)) then
      begin
        // capOverlay(m_hCapWnd,true);   //普通的摄像头不能用overlay的方式 主意;
         capPreviewRate(m_hCapWnd,30);  //设置帧率为30
         capPreview(m_hCapWnd,true);    // preview方式显示
         dwSize:=capGetVideoFormatSize(m_hCapWnd);  
         capGetVideoFormat(m_hCapWnd,@setBmp, dwSize);
         setBmp.bmiHeader.biWidth:=352;    //这个就是设置你捕捉图片的大小了 宽度 :)
         setBmp.bmiHeader.biHeight:=288; //这个就是设置你捕捉图片的大小了 高度 :)
         capSetVideoFormat(m_hCapWnd,@setBmp,dwSize); 
      end;
   except
   end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin//定像
  capbmp;
end;

procedure TForm1.capbmp;
begin
  capEditCopy(m_hCapWnd);   //把图像拷到剪贴板
  image1.Picture.Bitmap.LoadFromClipboardFormat(CF_BITMAP,Clipboard.GetAsHandle(CF_BITMAP), 0);
  // 这时在image1里面的就是你需要的图片
end;

end.
 

你可能感兴趣的:(抓取摄像头图片)