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

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