CnCameraEye.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2006 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnCameraEye;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:外接设备组件包
  24. * 单元名称:实现摄像头控制单元
  25. * 单元作者:rarnu(rarnu@cnpack.org)
  26. * 备 注:暂未进行无摄像头的检测
  27. * 开发平台:Windows2003 Server + Delphi2007 up2
  28. * 兼容测试:Windows2000/XP/2003/Vista + Delphi 7/2006/2007/2009
  29. * 本 地 化:该单元中的字符串均符合本地化处理方式
  30. * 单元标识:$Id$
  31. * 修改记录:2008.08.14 V1.0
  32. * 创建单元
  33. ================================================================================
  34. |</PRE>}
  35. interface
  36. {$I CnPack.inc}
  37. uses
  38. SysUtils, Classes, Controls, Windows, Messages;
  39. type
  40. TCnCameraEye = class(TComponent)
  41. private
  42. FDisplay: TWinControl;
  43. FOnStart: TNotifyEvent;
  44. FOnStartRecord: TNotifyEvent;
  45. FOnStop: TNotifyEvent;
  46. FOnStopRecord: TNotifyEvent;
  47. public
  48. constructor Create(AOwner: TComponent); override;
  49. destructor Destroy; override;
  50. procedure Start;
  51. {* 开始摄像 }
  52. procedure Stop;
  53. {* 停止摄像 }
  54. procedure SaveToBmp(const FileName: string);
  55. {* 截图并保存到bmp }
  56. procedure RecordToAVI(const FileName: string);
  57. {* 录制AVI }
  58. procedure StopRecord;
  59. {* 停止录制 }
  60. published
  61. property Display: TWinControl read FDisplay write FDisplay;
  62. {* 图像显示容器 }
  63. property OnStart: TNotifyEvent read FOnStart write FOnStart;
  64. {* 开始摄像事件 }
  65. property OnStop: TNotifyEvent read FOnStop write FOnStop;
  66. {* 停止摄像事件 }
  67. property OnStartRecord: TNotifyEvent read FOnStartRecord write FOnStartRecord;
  68. {* 开始录像事件 }
  69. property OnStopRecord: TNotifyEvent read FOnStopRecord write FOnStopRecord;
  70. {* 停止录像事件 }
  71. end;
  72. implementation
  73. {* 消息常量声明 }
  74. const
  75. WM_CAP_START = WM_USER;
  76. WM_CAP_STOP = WM_CAP_START + 68;
  77. WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
  78. WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
  79. WM_CAP_SAVEDIB = WM_CAP_START + 25;
  80. WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
  81. WM_CAP_SEQUENCE = WM_CAP_START + 62;
  82. WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
  83. WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
  84. WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
  85. WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
  86. WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
  87. WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
  88. WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
  89. WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
  90. WM_CAP_SET_SCALE = WM_CAP_START + 53;
  91. WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
  92. {* 声明动态函数,此函数从DLL中调入,动态判断是否可用 }
  93. type
  94. TFunCap = function(
  95. lpszWindowName: PCHAR;
  96. dwStyle: longint;
  97. x: integer;
  98. y: integer;
  99. nWidth: integer;
  100. nHeight: integer;
  101. ParentWin: HWND;
  102. nId: integer): HWND; stdcall;
  103. var
  104. hWndC: THandle;
  105. FunCap: TFunCap;
  106. DllHandle: THandle;
  107. { TCnCameraEye }
  108. constructor TCnCameraEye.Create(AOwner: TComponent);
  109. var
  110. FPointer: Pointer;
  111. begin
  112. inherited Create(AOwner);
  113. FDisplay := nil;
  114. {* 通过DLL调入,如果DLL不存在,表示没有驱动 }
  115. DllHandle := LoadLibrary('AVICAP32.DLL');
  116. if DllHandle <= 0 then
  117. raise Exception.Create('Camera driver not installed or invalid.');
  118. FPointer := GetProcAddress(DllHandle, 'capCreateCaptureWindowA');
  119. FunCap := TFunCap(FPointer);
  120. end;
  121. destructor TCnCameraEye.Destroy;
  122. begin
  123. StopRecord;
  124. Stop;
  125. FDisplay := nil;
  126. {* 如果已加载DLL,则释放掉 }
  127. if DllHandle > 0 then
  128. FreeLibrary(DllHandle);
  129. inherited;
  130. end;
  131. procedure TCnCameraEye.RecordToAVI(const FileName: string);
  132. begin
  133. if hWndC <> 0 then
  134. begin
  135. SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, LongInt(PAnsiChar(AnsiString(FileName))));
  136. SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
  137. if Assigned(FOnStartRecord) then
  138. FOnStartRecord(Self);
  139. end;
  140. end;
  141. procedure TCnCameraEye.SaveToBmp(const FileName: string);
  142. begin
  143. if hWndC <> 0 then
  144. SendMessage(hWndC, WM_CAP_SAVEDIB, 0, LongInt(PAnsiChar(AnsiString(FileName))));
  145. end;
  146. procedure TCnCameraEye.Start;
  147. var
  148. OHandle: THandle;
  149. begin
  150. if FDisplay = nil then Exit;
  151. OHandle := TWinControl(Owner).Handle;
  152. hWndC := FunCap(
  153. 'My Own Capture Window',
  154. WS_CHILD or WS_VISIBLE,
  155. FDisplay.Left, FDisplay.Top, FDisplay.Width, FDisplay.Height,
  156. OHandle, 0);
  157. if hWndC <> 0 then
  158. begin
  159. {* 发送指令 }
  160. SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
  161. SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
  162. SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
  163. SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
  164. SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
  165. SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);
  166. SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
  167. SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
  168. end;
  169. if Assigned(OnStart) then
  170. FOnStart(Self);
  171. end;
  172. procedure TCnCameraEye.Stop;
  173. begin
  174. if hWndC <> 0 then
  175. begin
  176. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
  177. hWndC := 0;
  178. if Assigned(FOnStop) then
  179. FOnStop(Self);
  180. end;
  181. end;
  182. procedure TCnCameraEye.StopRecord;
  183. begin
  184. if hWndC <> 0 then
  185. begin
  186. SendMessage(hWndC, WM_CAP_STOP, 0, 0);
  187. if Assigned(FOnStopRecord) then
  188. FOnStopRecord(Self);
  189. end;
  190. end;
  191. end.