Delphi控制摄像头

时间:2022-01-16 19:58:43

Delphi对摄像头的控制很简单,在System,windows和messages三个单元内已定义了所有的底层消息函数,我们只需要合理的调用它们就行了。我把摄像头的有关操作做成一个控件,这样就可以拖动窗体上直接使用了。

 

 

  1. {************************************
  2.  *    Camera Control for Delphi7    *
  3.  *          Made by Rarnu           *
  4.  *        Credit 2006.08.27         *
  5.  *       http://rarnu.ik8.com       *
  6.  ************************************}
  7. unit RaCameraEye;
  8. interface
  9. uses
  10.   SysUtils, Classes, Controls, Windows, Messages;
  11. {事件声明}
  12. type
  13.   {开始摄像事件}
  14.   TOnStart = procedure(Sender: TObject) of object;
  15.   {停止摄像事件}
  16.   TOnStop = procedure(Sender: TObject) of object;
  17.   {开始录像事件}
  18.   TOnStartRecord = procedure(Sender: TObject) of object;
  19.   {停止录像事件}
  20.   TOnStopRecord = procedure(Sender: TObject) of object;
  21. type
  22.   TRaCameraEye = class(TComponent)
  23.   private
  24.     {图像显示容器}
  25.     fDisplay: TWinControl;
  26.     {事件关联变量}
  27.     fOnStart: TOnStart;
  28.     fOnStartRecord: TOnStartRecord;
  29.     fOnStop: TOnStop;
  30.     fOnStopRecord: TOnStopRecord;
  31.   protected
  32.   public
  33.     {构造&析构,由TComponent类覆盖而来}
  34.     constructor Create(AOwner: TComponent); override;
  35.     destructor Destroy; override;
  36.     {开始摄像}
  37.     procedure Start;
  38.     {停止摄像}
  39.     procedure Stop;
  40.     {截图并保存到bmp}
  41.     procedure SaveToBmp(FileName: string);
  42.     {录制AVI}
  43.     procedure RecordToAVI(FileName: string);
  44.     {停止录制}
  45.     procedure StopRecord;
  46.   published
  47.     property Display: TWinControl read fDisplay write fDisplay;
  48.     property OnStart: TOnStart read fOnStart write fOnStart;
  49.     property OnStop: TOnStop read fOnStop write fOnStop;
  50.     property OnStartRecord: TOnStartRecord read fOnStartRecord write fOnStartRecord;
  51.     property OnStopRecord: TOnStopRecord read fOnStopRecord write fOnStopRecord;
  52.   end;
  53. {消息常量声明}
  54. const
  55.   WM_CAP_START = WM_USER;
  56.   WM_CAP_STOP = WM_CAP_START + 68;
  57.   WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
  58.   WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
  59.   WM_CAP_SAVEDIB = WM_CAP_START + 25;
  60.   WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
  61.   WM_CAP_SEQUENCE = WM_CAP_START + 62;
  62.   WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
  63.   WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
  64.   WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
  65.   WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
  66.   WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
  67.   WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
  68.   WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
  69.   WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
  70.   WM_CAP_SET_SCALE = WM_CAP_START + 53;
  71.   WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
  72. {声明动态函数,此函数从DLL中调入,动态判断是否可用}
  73. type
  74.   TFunCap = function(
  75.     lpszWindowName: PCHAR;
  76.     dwStyle: longint;
  77.     x: integer;
  78.     y: integer;
  79.     nWidth: integer;
  80.     nHeight: integer;
  81.     ParentWin: HWND;
  82.     nId: integer): HWND; stdcall;
  83. {全局变量声明}
  84. var
  85.   hWndC: THandle;
  86.   FunCap: TFunCap;
  87.   DllHandle: THandle;
  88. procedure Register;
  89. implementation
  90. procedure Register;
  91. begin
  92.   RegisterComponents('Rarnu Components', [TRaCameraEye]);
  93. end;
  94. { TRaCameraEye }
  95. constructor TRaCameraEye.Create(AOwner: TComponent);
  96. var
  97.   FPointer: Pointer;{函数指针}
  98. begin
  99.   inherited Create(AOwner);
  100.   fDisplay := nil;
  101.   {通过DLL调入,如果DLL不存在,表示没有驱动}
  102.   DllHandle := LoadLibrary('AVICAP32.DLL');
  103.   if DllHandle <= 0 then
  104.   begin
  105.     MessageBox(TWinControl(Owner).Handle, '未安装摄像头驱动或驱动程序无效,不能使用此控件!''出错', MB_OK or MB_ICONERROR);
  106.     Destroy;{释放控件}
  107.     Exit;
  108.   end;
  109.   {函数指针指向指定API}
  110.   FPointer := GetProcAddress(DllHandle, 'capCreateCaptureWindowA');
  111.   {恢复函数指针到实体函数}
  112.   FunCap := TFunCap(FPointer);
  113. end;
  114. destructor TRaCameraEye.Destroy;
  115. begin
  116.   StopRecord;
  117.   Stop;
  118.   fDisplay := nil;
  119.   {如果已加载DLL,则释放掉}
  120.   if DllHandle > 0 then
  121.     FreeLibrary(DllHandle);
  122.   inherited Destroy;
  123. end;
  124. procedure TRaCameraEye.RecordToAVI(FileName: string);
  125. begin
  126.   if hWndC <> 0 then
  127.   begin
  128.     SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0longint(PCHAR(FileName)));
  129.     SendMessage(hWndC, WM_CAP_SEQUENCE, 00);
  130.     if Assigned(OnStartRecord) then
  131.       OnStartRecord(Self);
  132.   end;
  133. end;
  134. procedure TRaCameraEye.SaveToBmp(FileName: string);
  135. begin
  136.   if hWndC <> 0 then
  137.     SendMessage(hWndC, WM_CAP_SAVEDIB, 0longint(PCHAR(FileName)));
  138. end;
  139. procedure TRaCameraEye.Start;
  140. var
  141.   OHandle: THandle;
  142. begin
  143.   if fDisplay = nil then Exit;
  144.   OHandle := TWinControl(Owner).Handle;
  145.   {动态函数控制摄像头}
  146.   hWndC := FunCap(
  147.     'My Own Capture Window',
  148.     WS_CHILD or WS_VISIBLE,
  149.     {规定显示范围}
  150.     fDisplay.Left, fDisplay.Top, fDisplay.Width, fDisplay.Height,
  151.     OHandle, 0);
  152.   if hWndC <> 0 then
  153.   begin
  154.     {发送指令}
  155.     SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 00);
  156.     SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 00);
  157.     SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 00);
  158.     SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 00);
  159.     SendMessage(hWndC, WM_CAP_SET_SCALE, 10);
  160.     SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 660);
  161.     SendMessage(hWndC, WM_CAP_SET_OVERLAY, 10);
  162.     SendMessage(hWndC, WM_CAP_SET_PREVIEW, 10);
  163.   end;
  164.   if Assigned(OnStart) then
  165.     OnStart(Self);
  166. end;
  167. procedure TRaCameraEye.Stop;
  168. begin
  169.   if hWndC <> 0 then
  170.   begin
  171.     SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 00);
  172.     hWndC := 0;
  173.     if Assigned(OnStop) then
  174.       OnStop(Self);
  175.   end;
  176. end;
  177. procedure TRaCameraEye.StopRecord;
  178. begin
  179.   if hWndC <> 0 then
  180.   begin
  181.     SendMessage(hWndC, WM_CAP_STOP, 00);
  182.     if Assigned(OnStopRecord) then
  183.       OnStopRecord(Self);
  184.   end;
  185. end;
  186. end.