TVideoCapture类的源码,继承TCustomPanel,用于视频捕获(用到了SendMessage和SetWindowPos等API)good

[cpp] view plain copy

print?

  1. unit VideoCapture;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  5. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Imaging.jpeg;
  6. type
  7. TVideoCapture = class(TCustomPanel)
  8. private
  9. hWndC: THandle;
  10. CapturingAVI: bool;
  11. procedure WMSize(var Message: TWMSize); message WM_SIZE;
  12. protected
  13. { Protected declarations }
  14. public
  15. constructor Create(AOwner: TComponent); override;
  16. destructor Destroy; override;
  17. procedure OpenVideo(handle: THandle);
  18. procedure CloseVideo;
  19. procedure GrabFrame;
  20. procedure StartVideo;
  21. procedure StopVideo;
  22. procedure SaveBitMap(filename: TFileName);
  23. procedure SaveJpeg(filename: TFileName; compressibility: Integer);
  24. procedure SavetoJpegStream(var JpegStream: TMemoryStream; compressibility: Integer);
  25. function StartAvi(filename: TFileName): Boolean;
  26. procedure StopAvi;
  27. procedure SetVideoFormat;
  28. procedure SetSource;
  29. procedure SetStretch(TrueorFalse: Boolean = true);
  30. procedure SetCompression;
  31. published
  32. property Align;
  33. end;
  34. procedure Register;
  35. implementation
  36. const
  37. WM_CAP_START = WM_USER;
  38. WM_CAP_GET_CAPSTREAMPTR = (WM_CAP_START + 1);
  39. WM_CAP_SET_CALLBACK_ERROR = (WM_CAP_START + 2);
  40. WM_CAP_SET_CALLBACK_STATUS = (WM_CAP_START + 3);
  41. WM_CAP_SET_CALLBACK_YIELD = (WM_CAP_START + 4);
  42. WM_CAP_SET_CALLBACK_FRAME = (WM_CAP_START + 5);
  43. WM_CAP_SET_CALLBACK_VIDEOSTREAM = (WM_CAP_START + 6);
  44. WM_CAP_SET_CALLBACK_WAVESTREAM = (WM_CAP_START + 7);
  45. WM_CAP_GET_USER_DATA = (WM_CAP_START + 8);
  46. WM_CAP_SET_USER_DATA = (WM_CAP_START + 9);
  47. WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10);
  48. WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11);
  49. WM_CAP_DRIVER_GET_NAME = (WM_CAP_START + 12);
  50. WM_CAP_DRIVER_GET_VERSION = (WM_CAP_START + 13);
  51. WM_CAP_DRIVER_GET_CAPS = (WM_CAP_START + 14);
  52. WM_CAP_FILE_SET_CAPTURE_FILE = (WM_CAP_START + 20);
  53. WM_CAP_FILE_GET_CAPTURE_FILE = (WM_CAP_START + 21);
  54. WM_CAP_FILE_ALLOCATE = (WM_CAP_START + 22);
  55. WM_CAP_FILE_SAVEAS = (WM_CAP_START + 23);
  56. WM_CAP_FILE_SET_INFOCHUNK = (WM_CAP_START + 24);
  57. WM_CAP_FILE_SAVEDIB = (WM_CAP_START + 25);
  58. WM_CAP_EDIT_COPY = (WM_CAP_START + 30);
  59. WM_CAP_SET_AUDIOFORMAT = (WM_CAP_START + 35);
  60. WM_CAP_GET_AUDIOFORMAT = (WM_CAP_START + 36);
  61. WM_CAP_DLG_VIDEOFORMAT = (WM_CAP_START + 41);
  62. WM_CAP_DLG_VIDEOSOURCE = (WM_CAP_START + 42);
  63. WM_CAP_DLG_VIDEODISPLAY = (WM_CAP_START + 43);
  64. WM_CAP_GET_VIDEOFORMAT = (WM_CAP_START + 44);
  65. WM_CAP_SET_VIDEOFORMAT = (WM_CAP_START + 45);
  66. WM_CAP_DLG_VIDEOCOMPRESSION = (WM_CAP_START + 46);
  67. WM_CAP_SET_PREVIEW = (WM_CAP_START + 50);
  68. WM_CAP_SET_OVERLAY = (WM_CAP_START + 51);
  69. WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52);
  70. WM_CAP_SET_SCALE = (WM_CAP_START + 53);
  71. WM_CAP_GET_STATUS = (WM_CAP_START + 54);
  72. WM_CAP_SET_SCROLL = (WM_CAP_START + 55);
  73. WM_CAP_GRAB_FRAME = (WM_CAP_START + 60);
  74. WM_CAP_GRAB_FRAME_NOSTOP = (WM_CAP_START + 61);
  75. WM_CAP_SEQUENCE = (WM_CAP_START + 62);
  76. WM_CAP_SEQUENCE_NOFILE = (WM_CAP_START + 63);
  77. WM_CAP_SET_SEQUENCE_SETUP = (WM_CAP_START + 64);
  78. WM_CAP_GET_SEQUENCE_SETUP = (WM_CAP_START + 65);
  79. WM_CAP_SET_MCI_DEVICE = (WM_CAP_START + 66);
  80. WM_CAP_GET_MCI_DEVICE = (WM_CAP_START + 67);
  81. WM_CAP_STOP = (WM_CAP_START + 68);
  82. WM_CAP_ABORT = (WM_CAP_START + 69);
  83. WM_CAP_SINGLE_FRAME_OPEN = (WM_CAP_START + 70);
  84. WM_CAP_SINGLE_FRAME_CLOSE = (WM_CAP_START + 71);
  85. WM_CAP_SINGLE_FRAME = (WM_CAP_START + 72);
  86. WM_CAP_PAL_OPEN = (WM_CAP_START + 80);
  87. WM_CAP_PAL_SAVE = (WM_CAP_START + 81);
  88. WM_CAP_PAL_PASTE = (WM_CAP_START + 82);
  89. WM_CAP_PAL_AUTOCREATE = (WM_CAP_START + 83);
  90. WM_CAP_PAL_MANUALCREATE = (WM_CAP_START + 84);
  91. function capCreateCaptureWindowA(lpszWindowName: PCHAR;
  92. dwStyle: longint;
  93. x: integer;
  94. y: integer;
  95. nWidth: integer;
  96. nHeight: integer;
  97. ParentWin: HWND;
  98. nId: integer): HWND; stdcall; external ‘avicap32.dll‘;
  99. procedure Register;
  100. begin
  101. RegisterComponents(‘FstiCtl‘, [TVideoCapture]);
  102. end;
  103. { TVideoCapture }
  104. constructor TVideoCapture.Create(AOwner: TComponent);
  105. begin
  106. inherited Create(AOwner);
  107. CapturingAVI := false;
  108. Color := clBlack;
  109. BevelOuter := bvNone;
  110. Width := 320;
  111. Height := 240;
  112. hWndC := 0;
  113. end;
  114. destructor TVideoCapture.Destroy;
  115. begin
  116. if CapturingAVI then StopAvi;
  117. if hWndC <> 0 then CloseVideo;
  118. hWndC := 0;
  119. inherited;
  120. end;
  121. procedure TVideoCapture.OpenVideo(handle: THandle);
  122. begin
  123. hWndC := capCreateCaptureWindowA(‘Video Capture Window‘,
  124. WS_CHILD or WS_VISIBLE,
  125. Left,
  126. Top,
  127. Width,
  128. Height,
  129. Handle,
  130. 0);
  131. if hWndC <> 0 then
  132. SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
  133. end;
  134. procedure TVideoCapture.CloseVideo;
  135. begin
  136. if hWndC <> 0 then begin
  137. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
  138. SendMessage(hWndC, WM_CLOSE, 0, 0);
  139. hWndC := 0;
  140. end;
  141. end;
  142. procedure TVideoCapture.GrabFrame;
  143. begin
  144. if hWndC <> 0 then
  145. SendMessage(hWndC, WM_CAP_GRAB_FRAME, 0, 0);
  146. end;
  147. procedure TVideoCapture.SaveBitMap(filename: TFileName);
  148. begin
  149. SendMessage(hWndC, WM_CAP_FILE_SAVEDIB, 0, longint(pchar(FileName)));
  150. end;
  151. function TVideoCapture.StartAvi(filename: TFileName): Boolean;
  152. begin
  153. if hWndC <> 0 then begin
  154. CapturingAVI := true;
  155. SendMessage(hWndC,
  156. WM_CAP_FILE_SET_CAPTURE_FILE,
  157. 0,
  158. Longint(pchar(FileName)));
  159. SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
  160. end;
  161. end;
  162. procedure TVideoCapture.StopAvi;
  163. begin
  164. if hWndC <> 0 then begin
  165. SendMessage(hWndC, WM_CAP_STOP, 0, 0);
  166. CapturingAVI := false;
  167. end;
  168. end;
  169. procedure TVideoCapture.SaveJpeg(filename: TFileName;
  170. compressibility: Integer);
  171. var
  172. bmp: TBitMap;
  173. jpg: TJpegimage;
  174. begin
  175. try
  176. SaveBitMap(‘tmp.bmp‘);
  177. bmp := TBitmap.Create;
  178. jpg := TJpegImage.Create;
  179. bmp.LoadFromFile(‘tmp.bmp‘);
  180. jpg.Assign(bmp);
  181. jpg.CompressionQuality := compressibility;
  182. jpg.Compress;
  183. jpg.SaveToFile(filename);
  184. DeleteFile(‘tmp.bmp‘);
  185. except
  186. end;
  187. bmp.free;
  188. jpg.free;
  189. end;
  190. procedure TVideoCapture.SetVideoFormat;
  191. begin
  192. SendMessage(hWndC, WM_CAP_DLG_VIDEOFORMAT, 0, 0);
  193. end;
  194. procedure TVideoCapture.SetSource;
  195. begin
  196. SendMessage(hWndC, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
  197. end;
  198. procedure TVideoCapture.StartVideo;
  199. begin
  200. SendMessage(hWndC, WM_CAP_SET_PREVIEW, -1, 0);
  201. SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 100, 0);
  202. SendMessage(hWndC, WM_CAP_SET_SCALE, -1, 0);
  203. end;
  204. procedure TVideoCapture.StopVideo;
  205. begin
  206. SendMessage(hWndC, WM_CAP_SET_PREVIEW, 0, 0);
  207. end;
  208. procedure TVideoCapture.WMSize(var Message: TWMSize);
  209. begin
  210. SetWindowPos(hWndC, HWND_BOTTOM, 0, 0, Width, Height, SWP_NOMOVE or SWP_NOACTIVATE);
  211. end;
  212. procedure TVideoCapture.SetStretch(TrueorFalse: Boolean);
  213. begin
  214. end;
  215. procedure TVideoCapture.SetCompression;
  216. begin
  217. SendMessage(hWndC, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0)
  218. end;
  219. procedure TVideoCapture.SavetoJpegStream(var JpegStream: TMemoryStream; compressibility: Integer);
  220. var
  221. bmp: TBitMap;
  222. jpg: TJpegimage;
  223. begin
  224. try
  225. SaveBitMap(‘tmp.bmp‘);
  226. bmp := TBitmap.Create;
  227. jpg := TJpegImage.Create;
  228. bmp.LoadFromFile(‘tmp.bmp‘);
  229. jpg.Assign(bmp);
  230. jpg.CompressionQuality := compressibility;
  231. jpg.Compress;
  232. jpg.SaveToStream(JpegStream);
  233. DeleteFile(‘tmp.bmp‘);
  234. except
  235. end;
  236. bmp.free;
  237. jpg.free;
  238. end;
  239. end.

http://blog.csdn.net/huang_xw/article/details/8638309

时间: 2024-10-07 20:26:42

TVideoCapture类的源码,继承TCustomPanel,用于视频捕获(用到了SendMessage和SetWindowPos等API)good的相关文章

commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现

commoncrawl 源码库是用于 Hadoop 的自定义 InputFormat 配送实现. Common Crawl 提供一个示例程序 BasicArcFileReaderSample.java (位于 org.commoncrawl.samples) 用来配置 InputFormat. commoncrawl / commoncrawl Watch414 Fork86 CommonCrawl Project Repository — More... http://www.commoncr

ios版塔防类游戏源码

一款ios塔防类游戏源码 ,一款像素风格的防御类游戏源码.老鼠向目标进攻,点击老鼠就可以直接把老鼠拍死.或者像<水果忍着>那样滑动屏幕将目标弄死,大家可以看看吧. 项目源码下载:http://code.662p.com/view/6308.html <ignore_js_op> <ignore_js_op> <ignore_js_op> <ignore_js_op> 详细说明:http://ios.662p.com/thread-1449-1-2

关于java中ReentrantLock类的源码分析以及总结与例子

一,官方描述 关于ReentrantLock的官方描述,英文的就不贴出来了,这里我只贴出我自己翻译的描述: reentrant是一个跟synchronized具有相同行为和语义的持有锁来访问方法和语句的互斥锁,但是reentrant还拥有被扩展的能力. ReentrantLock会被线程拥有并且持续锁定,不会解锁.线程调用lock()方法返回后,则成功持有锁,否则这个锁正在被另一个线程所持有,只能等待另一个线程释放锁,如果当前线程拥有了锁,则调用lock()方法会立即返回,这个状态可以通过isH

JDK中String类的源码分析(二)

1.startsWith(String prefix, int toffset)方法 包括startsWith(*),endsWith(*)方法,都是调用上述一个方法 1 public boolean startsWith(String prefix, int toffset) { 2 char ta[] = value; 3 int to = toffset; 4 char pa[] = prefix.value; 5 int po = 0; 6 int pc = prefix.value.l

【小白的java成长系列】——顶级类Object源码分析

首先来说一下api文档使用,api这个词对有一定开发经验的java编程人员来说是很喜爱的~ java当然也提供了api开发文档,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 找到下面的: 下载自己喜爱的版本即可,解压,点击~/jdk-7u60-apidocs/api/index.html就可以查看其api了: 跟上网一样一样的,点击相应链接就可以查看其信息了. 进入正题,说说Object这个类: 先

java Thread 类的源码阅读(oracle jdk1.8)

java线程类的源码分析阅读技巧: 首先阅读thread类重点关注一下几个问题: 1.start() ,启动一个线程是如何实现的? 2.java线程状态机的变化过程以及如何实现的? 1.start方法的源码如下: new Thread(Runnable):代码内部实际代码如下: /** * Initializes a Thread. * * @param g the Thread group * @param target the object whose run() method gets c

Netty工具类HashedWheelTimer源码走读

1. 简单介绍. A Timer optimized for approximated I/O timeout scheduling. 关于Timer的介绍可以看看这篇文章, 写得不错 :  http://novoland.github.io/%E5%B9%B6%E5%8F%91/2014/07/26/%E5%AE%9A%E6%97%B6%E5%99%A8%EF%BC%88Timer%EF%BC%89%E7%9A%84%E5%AE%9E%E7%8E%B0.html 可以看到, HashedWhe

ThreadLocal 类的源码解析

1.原理图说明 首先看这一张图,我们可以看出,每一个Thread类中都存在一个属性 ThreadLocalMap 成员,该成员是一个map数据结构,map中是一个Entry的数组,存在entry实体,该实体包含了 key value hash (注意 此map结构不包含next引用 所以不是使用的链地址方法). 可以是用来存放 ThreadLocal对象以及对应的变量副本: 根据这个原理.我们可以知道在一个线程中可以存储多个 ThreadLocal 对象以及对应的value副本: 所以Threa

ThreadLocal 类 的源码解析以及使用原理

1.原理图说明 首先看这一张图,我们可以看出,每一个Thread类中都存在一个属性 ThreadLocalMap 成员,该成员是一个map数据结构,map中是一个Entry的数组,存在entry实体,该实体包含了 key value hash (注意 此map结构不包含next引用 所以不是使用的链地址方法). 可以是用来存放 ThreadLocal对象以及对应的变量副本: 根据这个原理.我们可以知道在一个线程中可以存储多个 ThreadLocal 对象以及对应的value副本: 所以Threa