Videocap.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. unit Videocap;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls,stdctrls,
  5. ExtCtrls, vfw, mmsystem,syncobjs;
  6. ///////////////////////////////////////////////////////////////////////////////
  7. // Video Capturing
  8. type
  9. // Types for audio-settings
  10. TChannel = (Stereo, Mono);
  11. TFrequency = (f8000Hz, f11025Hz, f22050Hz, f44100Hz);
  12. TResolution = (r8Bit, r16Bit);
  13. // Types for event-procedures
  14. type
  15. TCapStatusProc = procedure(Sender: TObject) of object;
  16. TCapStatusCallback = procedure (Sender:TObject;nID:integer;status:string) of object;
  17. TVideoStream = procedure (sender:TObject;lpVhdr:PVIDEOHDR) of object;
  18. TAudioStream = procedure (sender:TObject;lpWHdr:PWAVEHDR) of object;
  19. TError = procedure (sender:TObject;nID:integer; errorstr:string) of object;
  20. // Exceptions
  21. type ENoDriverException = class(Exception);
  22. type ENoCapWindowException = class(Exception);
  23. type ENotConnectException = class(Exception);
  24. type ENoOverlayException = class(Exception);
  25. type EFalseFormat = class(Exception);
  26. type ENotOpen = class(Exception);
  27. type EBufferFileError = class(Exception);
  28. type
  29. TAudioFormat = class (TPersistent)
  30. private
  31. FChannels :TChannel;
  32. FFrequency:TFrequency;
  33. FRes :TResolution;
  34. private
  35. procedure SetAudio(handle:Thandle); // Setting Audio Data to Capture Window
  36. public
  37. constructor create;
  38. published
  39. property Channels: TChannel read FChannels write Fchannels default Mono;
  40. property Frequency: TFrequency read FFrequency write fFrequency default f8000Hz;
  41. property Resolution : TResolution read FRes write FRes default r8Bit;
  42. end;
  43. type
  44. TVideoCap = class(TCustomControl)
  45. private
  46. fdriverIndex:integer; // Videodriver index
  47. fVideoDriverName : string; // name of videodriver
  48. fhCapWnd : THandle; // handle for CAP-Window
  49. fpDrivercaps : PCapDriverCaps; // propertys of videodriver
  50. fpDriverStatus : pCapStatus; // status of capdriver
  51. fscale : boolean; // window scaling
  52. fprop : boolean; // proportional scaling
  53. fpreviewrate : word; // Frames p. sec during preview
  54. fmicrosecpframe : cardinal; // framerate as microsconds
  55. fCapVideoFileName : string; // name of the capture file
  56. fTempFileName : String; // Name of temporary avi-file
  57. fTempFileSize : word; // size of Tmp- File in MB
  58. fCapSingleImageFileName : string; // name of the file for a single image
  59. fcapAudio :boolean; // Capture also audio stream
  60. fcapTimeLimit :word; // Time limit for captureing
  61. fIndexSize :cardinal; // size of the index in the capture file
  62. fcapToFile : boolean; // Write frames to file druing capturing
  63. FAudioFormat : TAudioFormat;// Audio Format
  64. fCapStatusProcedure : TCapStatusProc; // Event procedure for internal component status
  65. fcapStatusCallBack : TCapStatusCallback; // Event procedure for status of then driver
  66. fcapVideoStream : TVideoStream; // Event procedure for each Video frame during capturing
  67. fcapAudioStream : TAudiostream; // Event procedure for each Audio buffer
  68. fcapFrameCallback : TVideoStream; // Event procedure for each Video frame during preview
  69. fcapError : TError; // Event procedure for Error
  70. procedure setsize(var msg:TMessage); message WM_SIZE; // Changing size of cap window
  71. function GetDriverCaps:boolean; // get driver capitiyties
  72. procedure DeleteDriverProps; // delete driver capitilyites
  73. procedure CreateTmpFile(drvopn:boolean); // Create or delete a temp avi?file
  74. function GetDriverStatus(callback:boolean):boolean; // Getting state of driver
  75. Procedure SetDriverOpen(value:boolean) ; // Open and Close the driver
  76. function GetDriverOpen:boolean; // is Driver open ?
  77. function GetPreview:boolean; // previwe mode
  78. function GetOverlay:Boolean; // overlay eode;
  79. procedure SizeCap; // calc size of the Capture Window
  80. procedure Setprop(value:Boolean); // Stretch Picture proportional to Window Size
  81. procedure SetMicroSecPerFrame(value:cardinal); // micro seconds between two frames
  82. procedure setFrameRate(value:word); // Setting Frames p. second
  83. function GetFrameRate:word; // Getting Frames p. second.
  84. // Handlers for Propertys
  85. procedure SetDriverIndex(value:integer);// Select Driver by setting driver index
  86. function CreateCapWindow:boolean; // Opening driver, create capture window
  87. procedure DestroyCapwindow; // Closing Driver, destrying capture window
  88. function GetCapWidth:word; // Width and Heigth of Video-Frame
  89. function GetCapHeight:word;
  90. function GetHasDlgVFormat : Boolean; // Driver has a format dialog
  91. function GetHasDlgVDisplay : Boolean; // Driver has a display dialog
  92. function GetHasDlgVSource : Boolean; // Driver has a source dialog
  93. function GetHasVideoOverlay: Boolean; // Driver has overlay mode
  94. procedure Setoverlay(value:boolean); // Driver will use overlay mode
  95. procedure SetPreview(value:boolean); // Driver will use preview mode
  96. procedure SetScale(value:Boolean); // Stretching Frame to component size
  97. procedure SetpreviewRate(value:word); // Setting preview frame rate
  98. function GetCapInProgress:boolean; // Capturing in progress
  99. procedure SetIndexSize(value:cardinal); // Setting index size in capture file
  100. function GetBitMapInfoNP:TBITMAPINFO; // Bitmapinfo Without Palette
  101. function GetBitmapHeader:TBitmapInfoHeader; //Get only Header;
  102. procedure SetBitmapHeader(Header:TBitmapInfoHeader); // Set only Header
  103. procedure SetBufferFileSize(value:word); // Setting of Tmp-File
  104. // Setting callbacks as events
  105. procedure SetStatCallBack(value:TCapStatusCallback);
  106. procedure SetCapVideoStream(value:TVideoStream);
  107. procedure SetCapAudioStream(value:TAudioStream);
  108. procedure SetCapFrameCallback(value:TVideoStream);
  109. procedure SetCapError(value:TError);
  110. public
  111. procedure SetDriverName(value:String); // Select Driver by setting driver name
  112. constructor Create(AOwner: TComponent); override;
  113. destructor Destroy; override;
  114. property HasDlgFormat:Boolean read GetHasDlgVFormat; // Driver has a format dialog
  115. property HasDlgDisplay:Boolean read GetHasDlgVDisplay; // Driver has a display dialog
  116. property HasDlgSource:Boolean read GetHasDlgVSource; // Driver has a sourve dialog
  117. property HasVideoOverlay:boolean read GetHasVideoOverlay; // Driver has overlay mode
  118. property CapWidth: word read GetCapWidth; // Width of the captured frames
  119. property CapHeight: word read GetCapHeight; // Hight of the captured frames
  120. property CapInProgess: boolean read getCapinProgress; /// capturing is progress
  121. property BitMapInfo:TBitmapinfo read GetBitmapInfoNP; // Get the Bitmapinfo of the frames wiht no legal palette
  122. //Header of the Bitmapinfo
  123. function DlgVFormat:Boolean; // Shows VideoFormat dialog of the Driver
  124. function DlgVDisplay:boolean; // Shows VideoDisplay dialog of the Driver
  125. function DlgVSource:boolean; // Shows VideoSource dialog of the Driver
  126. function DlgVCompression:Boolean; // Shows VideoCompression dialog from VfW
  127. function GrabFrame:boolean; // Capture one Frame and stops overlay or preview mode
  128. function GrabFrameNoStop:boolean; // Capture one frame without stoppin overlay or preview
  129. function SaveAsDIB:Boolean; // saves actual frame as DIB
  130. function SaveToClipboard:Boolean; // Puts actual fasme to then Clipboard
  131. function StartCapture:Boolean; // Starts Capturing
  132. function StopCapture:Boolean; // Stops capturing
  133. function GetBitmapInfo(var p:Pointer):integer; // The whole Bitmap-Info with complete palette
  134. procedure SetBitmapInfo(p:Pointer;size:integer); // Setting whole Bitmap-Info with complete palette
  135. property BitMapInfoHeader:TBitmapInfoHeader read GetBitmapHeader write SetBitmapHeader;
  136. function SaveCap:boolean; // Saves Avi-File if Bufferfile is used
  137. function CapSingleFramesOpen:boolean; // Opens AVI-File for Singe Image Capturing
  138. function CapSingleFramesClose:boolean; // Close AVI-File after Singe Image Capturing
  139. function CapSingleFrame:boolean; // Captures a Single frame to File
  140. published
  141. property align;
  142. property color;
  143. property visible;
  144. property DriverOpen: boolean read getDriveropen write setDriverOpen; // Opens the Driver / or is Driver open
  145. property DriverIndex:integer read fdriverindex write SetDriverIndex; // Index of driver
  146. property DriverName: string read fVideoDriverName write SetDrivername; // Name of the Driver
  147. property VideoOverlay:boolean read GetOverlay write SetOverlay; // Overlay - Mode
  148. property VideoPreview:boolean read GetPreview write SetPreview; // Preview - Mode
  149. property PreviewScaleToWindow:boolean read fscale write Setscale; // Stretching Frame to component size
  150. property PreviewScaleProportional:boolean read fprop write Setprop; // Stretching Frame poportional to original size
  151. property PreviewRate:word read fpreviewrate write SetpreviewRate; //Preview frame rate
  152. property MicroSecPerFrame:cardinal read fmicrosecpframe write SetMicroSecPerFrame; //micro seconds between two frames
  153. property FrameRate:word read getFramerate write setFrameRate; //Frames p. second
  154. Property CapAudio:Boolean read fcapAudio write fcapAudio; // Captue audio stream to
  155. property VideoFileName:string read fCapVideoFileName write fCapVideoFileName ; // Name of capture file
  156. property SingleImageFile:string read FCapSingleImageFileName write FCapSingleImageFileName; // Name of file for single image
  157. property CapTimeLimit:word read fCapTimeLimit write fCapTimeLimit; // time limit for Capturing
  158. property CapIndexSize:cardinal read findexSize write setIndexSize; // Size of the index for capture file
  159. property CapToFile:boolean read fcaptoFile write fcapToFile; // Write Frames to capture file
  160. property CapAudioFormat:TAudioformat read FAudioformat write FAudioFormat; // Format of captuing Audiodata
  161. property BufferFileSize:word read ftempfilesize write SetBufferFileSize; // Size of Bufferfile in MB
  162. // Internal Events and Callbacks as Events
  163. property OnStatus:TCapStatusProc read fCapStatusProcedure write FCapStatusProcedure;
  164. property OnStatusCallback:TCapStatusCallback read fcapStatuscallback write SetStatCallback;
  165. property OnVideoStream:TVideoStream read fcapVideoStream write SetCapVideoStream;
  166. property OnFrameCallback:TVideoStream read FcapFramecallback write SetCapFrameCallback;
  167. property OnAudioStream:TAudioStream read fcapAudioStream write SetCapAudioStream;
  168. property OnError:TError read fcapError write SetCapError;
  169. property OnMouseMove;
  170. property OnMouseUp;
  171. property OnMouseDown;
  172. property OnClick;
  173. Property OnDblClick;
  174. end;
  175. Function GetDriverList:TStringList; // Fill stringlist with names and versioninfo of all installed capture drivers
  176. procedure FrameToBitmap(Bitmap:TBitmap;FrameBuffer:pointer; BitmapInfo:TBitmapInfo); // Make a TBitmap from a Frame
  177. procedure BitmapToFrame(Bitmap:TBitmap; FrameBuffer:pointer; BitmapInfo:TBitmapInfo); // Make a Frame form a Bitmap
  178. function FrametobgrFrame(FrameBuffer:pointer; bgrFrameBuffer:pointer; BitmapInfo:TBitmapInfo): boolean;
  179. procedure Register;
  180. implementation
  181. // Callback for status of video captures
  182. function StatusCallbackProc(hWnd : HWND; nID : Integer; lpsz : Pchar): DWord; stdcall;
  183. var Control:TVideoCap;
  184. begin
  185. control:=TVideoCap(capGetUserData(hwnd));
  186. if assigned(control) then
  187. begin
  188. if assigned(control.fcapStatusCallBack) then
  189. control.fcapStatusCallBack(control,nId,strPas(lpsz));
  190. end;
  191. result:= 1;
  192. end;
  193. // Callback for video stream
  194. function VideoStreamCallbackProc(hWnd:Hwnd; lpVHdr:PVIDEOHDR):DWORD; stdcall;
  195. var Control:TVideoCap;
  196. begin
  197. control:= TVideoCap(capGetUserData(hwnd));
  198. if assigned(control) then
  199. begin
  200. if assigned(control.fcapVideoStream ) then
  201. control.fcapVideoStream(control,lpvHdr);
  202. end;
  203. result:= 1;
  204. end;
  205. //Callback for Frames during Preview
  206. function FrameCallbackProc(hwnd:Hwnd; lpvhdr:PVideoHdr):DWord;stdcall;
  207. var Control:TVideoCap;
  208. begin
  209. control:= TVideoCap(capGetUserData(hwnd));
  210. if assigned(control) then
  211. begin
  212. if assigned(control.fcapFrameCallback ) then
  213. control.fcapFrameCallback(control,lpvHdr);
  214. end;
  215. result:= 1;
  216. end;
  217. // Callback for audio stream
  218. function AudioStreamCallbackProc(hwnd:HWND;lpWHdr:PWaveHdr):DWORD; stdcall;
  219. var control:TVideoCap;
  220. begin
  221. control:= TVideoCap(capGetUserData(hwnd));
  222. if assigned(control) then
  223. if assigned(control.fcapAudioStream) then
  224. begin
  225. control.fcapAudioStream(control,lpwhdr);
  226. end;
  227. result:= 1;
  228. end;
  229. // Callback for Error
  230. function ErrorCallbackProc(hwnd:HWND;nId:integer;lzError:Pchar):DWord;stdcall;
  231. var Control:TVideoCap;
  232. begin
  233. control:= TVideoCap(capGetUserData(hwnd));
  234. if assigned(control) then
  235. if assigned(control.fcaperror) then
  236. begin
  237. control.fcapError(control,nId,StrPas(lzError));
  238. end;
  239. result:= 1;
  240. end;
  241. // New Window-Procedure for CaputreWindow to post messages like WM_MouseMove to Component
  242. function WCapproc(hw:THandle;messa:DWord; w:wParam; l:lParam):integer;stdcall;
  243. var oldwndProc:Pointer;
  244. parentWnd:Thandle;
  245. begin
  246. oldwndproc:=Pointer(GetWindowLong(hw,GWL_USERDATA));
  247. case Messa of
  248. WM_MOUSEMOVE,
  249. WM_LBUTTONDBLCLK,
  250. WM_LBUTTONDOWN,WM_RBUTTONDOWN,WM_MBUTTONDOWN ,
  251. WM_LBUTTONUP,WM_RBUTTONUP,WM_MBUTTONUP:
  252. begin
  253. ParentWnd:=Thandle(GetWindowLong(hw,GWL_HWNDPARENT));
  254. sendMessage(ParentWnd,messa,w,l);
  255. result := integer(true);
  256. end
  257. else
  258. result:= callWindowProc(oldwndproc,hw,messa,w,l);
  259. end;
  260. end;
  261. (*---------------------------------------------------------------*)
  262. // constructor and Destructor
  263. constructor TVideoCap.Create(aowner:TComponent);
  264. begin
  265. inherited create(aowner);
  266. height := 100;
  267. width := 100;
  268. Color :=clblack;
  269. fVideoDriverName := '';
  270. fdriverindex := -1 ;
  271. fhCapWnd := 0;
  272. fCapVideoFileName := 'Video.avi';
  273. fCapSingleImageFileName := 'Capture.bmp';
  274. fscale := false;
  275. fprop := false;
  276. fpreviewrate := 30;
  277. fmicrosecpframe := 66667;
  278. fpDrivercaps := nil;
  279. fpDriverStatus := nil;
  280. fcapToFile := true;
  281. findexSize := 0;
  282. ftempFileSize := 0;
  283. fCapStatusProcedure := nil;
  284. fcapStatusCallBack := nil;
  285. fcapVideoStream := nil;
  286. fcapAudioStream := nil;
  287. FAudioformat:=TAudioFormat.Create;
  288. end;
  289. destructor TVideoCap.destroy;
  290. begin
  291. DestroyCapWindow;
  292. deleteDriverProps;
  293. fAudioformat.free;
  294. inherited destroy;
  295. end;
  296. (*---------------------------------------------------------------*)
  297. // Messagehandler for sizing the capture window
  298. procedure TVideoCap.SetSize(var msg:TMessage);
  299. begin
  300. if (fhCapWnd <> 0) and (Fscale) then
  301. begin
  302. if msg.msg = WM_SIZE then SizeCap;
  303. end;
  304. end;
  305. // Sizing capture window
  306. procedure TVideoCap.SizeCap;
  307. var h,w:integer;
  308. f,cf:single;
  309. begin
  310. if not fscale then
  311. MoveWindow(fhcapWnd,0,0,Capwidth,capheight,true)
  312. else
  313. begin
  314. if fprop then
  315. begin
  316. f:= Width/height;
  317. cf:= CapWidth/CapHeight;
  318. if f > cf then
  319. begin
  320. h:= height;
  321. w:= round(h*cf);
  322. end
  323. else
  324. begin
  325. w:= width;
  326. h:= round(w*1/cf);
  327. end
  328. end
  329. else
  330. begin
  331. h:= height;
  332. w:= Width;
  333. end;
  334. MoveWindow(fhcapWnd,0,0,w, h,true);
  335. end;
  336. end;
  337. (*---------------------------------------------------------------*)
  338. // Delete driver infos
  339. procedure TVideoCap.DeleteDriverProps;
  340. begin
  341. if assigned(fpDrivercaps) then
  342. begin
  343. dispose(fpDrivercaps);
  344. fpDriverCaps:= nil;
  345. end;
  346. if assigned(fpDriverStatus) then
  347. begin
  348. dispose(fpDriverStatus);
  349. fpDriverStatus:= nil;
  350. end;
  351. end;
  352. (*---------------------------------------------------------------*)
  353. // Buffer File
  354. procedure TVideoCap.CreateTmpFile(drvOpn:boolean);
  355. var s,f:array [0..MAX_PATH] of char;
  356. size:word;
  357. ok:boolean;
  358. e:Exception;
  359. begin
  360. if (ftempFileName ='') and (ftempFileSize = 0) then exit;
  361. if drvOpn then Size := ftempFileSize else size:=0;
  362. if fTempFileName = '' then
  363. begin
  364. GetTempPath(sizeof(s),@s);
  365. GetTempFileName(s,'cap',0,f);
  366. ftempfilename := f;
  367. end;
  368. if size <> 0 then
  369. begin
  370. capFileSetCaptureFile(fhCapWnd,strpCopy(f,ftempfilename));
  371. ok:=capFileAlloc(fhcapWnd,1024*1024* ftempFileSize);
  372. if not ok then
  373. begin
  374. e:= EBufferFileError.Create('Could not create tmp file');
  375. raise e;
  376. end;
  377. end
  378. else
  379. begin
  380. capFileSetCaptureFile(fhCapWnd,strpCopy(f, fCapVideoFileName));
  381. DeleteFile(fTempfileName);
  382. fTempFileName:= '';
  383. end;
  384. end;
  385. procedure TVideoCap.SetBufferFileSize(Value:word);
  386. begin
  387. if value = fTempFilesize then exit;
  388. ftempFileSize:=value;
  389. if DriverOpen Then CreateTmpFile(true);
  390. end;
  391. (*---------------------------------------------------------------*)
  392. // Capitilies of the Driver
  393. function TVideoCap.GetDriverCaps:boolean;
  394. var savestat : integer;
  395. begin
  396. result:= false;
  397. if assigned(fpDrivercaps) then
  398. begin
  399. result:= true;
  400. exit;
  401. end;
  402. if fdriverIndex = -1 then exit;
  403. savestat := fhCapwnd; // save state of the window
  404. if fhCapWnd = 0 then CreateCapWindow;
  405. if fhCapWnd = 0 then exit;
  406. new(fpDrivercaps);
  407. if capDriverGetCaps(fhCapWnd, fpDriverCaps, sizeof(TCapDriverCaps)) then
  408. begin
  409. result:= true;
  410. if savestat = 0 then destroyCapWindow;
  411. exit;
  412. end;
  413. dispose(fpDriverCaps); // Error can't open then Driver
  414. fpDriverCaps := nil;
  415. if savestat = 0 then destroyCapWindow;
  416. end;
  417. (*---------------------------------------------------------------*)
  418. // BitmapInfo without a Palette
  419. function TVideoCap.GetBitMapInfoNp:TBitmapinfo;
  420. var e:Exception;
  421. begin
  422. if driveropen then
  423. begin
  424. capGetVideoFormat(fhcapWnd, @result,sizeof(TBitmapInfo));
  425. exit;
  426. end ;
  427. fillchar(result,sizeof(TBitmapInfo),0);
  428. e:= ENotOpen.Create('Driver not Open');
  429. raise e;
  430. end;
  431. // Whole BitmapInfo
  432. function TVideoCap.GetBitMapInfo(var p:Pointer):integer;
  433. var size:integer;
  434. e:Exception;
  435. begin
  436. p:=nil;
  437. if driverOpen then
  438. begin
  439. size:= capGetVideoFormat(fhcapWnd,p,0);
  440. getmem(p,size);
  441. capGetVideoFormat(fhcapwnd,p,size);
  442. result:=size;
  443. exit;
  444. end;
  445. e:= ENotOpen.Create('Driver not Open');
  446. raise e;
  447. end;
  448. // Setting whole BitmapInfo
  449. procedure TVideoCap.SetBitmapInfo(p:Pointer;size:integer);
  450. var e:Exception;
  451. supported:boolean;
  452. begin
  453. if driverOpen then
  454. begin
  455. supported:=capSetVideoFormat(fhcapWnd,p,size);
  456. if not supported then
  457. begin
  458. e:=EFalseFormat.Create('Not supported Frame Format' );
  459. raise e;
  460. end;
  461. exit;
  462. end;
  463. e:= ENotOpen.Create('Driver not Open');
  464. raise e;
  465. end;
  466. // Only Header of BitmapInfo
  467. function TVideoCap.GetBitMapHeader:TBitmapinfoHeader;
  468. var e:Exception;
  469. begin
  470. if driveropen then
  471. begin
  472. capGetVideoFormat(fhcapWnd, @result,sizeof(TBitmapInfoHeader));
  473. exit;
  474. end ;
  475. fillchar(result,sizeof(TBitmapInfoHeader),0);
  476. e:= ENotOpen.Create('Driver not Open');
  477. raise e;
  478. end;
  479. procedure TVideoCap.SetBitMapHeader(header:TBitmapInfoHeader);
  480. var e:exception;
  481. begin
  482. if driveropen then
  483. begin
  484. if not capSetVideoFormat(fhcapWnd,@header,sizeof(TBitmapInfoHeader)) then
  485. begin
  486. e:= EFalseFormat.Create('Not supported Frame Format');
  487. raise e;
  488. end;
  489. exit;
  490. end
  491. else
  492. begin
  493. e:= ENotOpen.Create('Driver not Open');
  494. raise e;
  495. end;
  496. end;
  497. (*---------------------------------------------------------------*)
  498. function TVideoCap.getDriverStatus(callback:boolean):boolean;
  499. begin
  500. result := false;
  501. if fhCapWnd <> 0 then
  502. begin
  503. if not assigned(fpDriverstatus) then new(fpDriverStatus);
  504. if capGetStatus(fhCapWnd,fpdriverstatus, sizeof(TCapStatus)) then
  505. begin
  506. result:= true;
  507. end;
  508. end;
  509. if assigned(fCapStatusProcedure)and callback then fcapStatusProcedure ( self);
  510. end;
  511. (*---------------------------------------------------------------*)
  512. // Setting name of driver
  513. procedure TVideoCap.SetDrivername(value:string);
  514. var i:integer;
  515. name:array[0..80] of char;
  516. ver :array[0..80] of char;
  517. begin
  518. if fVideoDrivername = value then exit;
  519. for i:= 0 to 9 do
  520. if capGetDriverDescription( i,name,80,ver,80) then
  521. if strpas(name) = value then
  522. begin
  523. fVideoDriverName := value;
  524. Driverindex:= i;
  525. exit;
  526. end;
  527. fVideoDrivername:= '';
  528. DriverIndex:= -1;
  529. end;
  530. (*---------------------------------------------------------------*)
  531. procedure TVideoCap.SetDriverIndex(value:integer);
  532. var name:array[0..80] of char;
  533. ver :array[0..80] of char;
  534. begin
  535. if value = fdriverindex then exit;
  536. destroyCapWindow;
  537. deleteDriverProps; // Alte Treiberfähigkeiten Löschen
  538. if value > -1 then
  539. begin
  540. if capGetDriverDescription(value,name,80,ver,80) then
  541. fVideoDriverName:= StrPas(name)
  542. else
  543. value:= -1;
  544. end;
  545. if value = -1 then fvideoDriverName:= '';
  546. fdriverindex:= value;
  547. end;
  548. (*---------------------------------------------------------------*)
  549. function TVideoCap.CreateCapWindow;
  550. var Ex:Exception;
  551. savewndproc:integer;
  552. begin
  553. if fhCapWnd <> 0 then
  554. begin
  555. result:= true;
  556. exit;
  557. end;
  558. if fdriverIndex = -1 then
  559. begin
  560. Ex := ENoDriverException.Create('No capture driver selected');
  561. GetDriverStatus(true);
  562. raise ex;
  563. exit;
  564. end;
  565. fhCapWnd := capCreateCaptureWindow( PChar(Name),
  566. WS_CHILD or WS_VISIBLE , 0, 0,
  567. Width, Height,
  568. Handle, 5001);
  569. if fhCapWnd =0 then
  570. begin
  571. Ex:= ENoCapWindowException.Create('Can not create capture window');
  572. GetDriverStatus(true);
  573. raise ex;
  574. exit;
  575. end;
  576. // Set our own Adress to the CapWindow
  577. capSetUserData(fhCapwnd,integer(self));
  578. // Set our own window procedure to Capture-Window
  579. savewndproc:=SetWindowLong(fhcapWnd,GWL_WNDPROC,integer(@WCapProc));
  580. // User Data for old WndProc adress
  581. SetWindowLong(fhcapWnd,GWL_USERDATA,savewndProc);
  582. // Setting callbacks as events
  583. if assigned(fcapStatusCallBack ) then
  584. capSetCallbackOnStatus(fhcapWnd ,StatusCallbackProc);
  585. if assigned(fcapFrameCallback) then
  586. capSetCallbackOnFrame(fhcapWnd,FrameCallbackProc);
  587. if assigned(fcapError) then
  588. capSetCallbackOnError(fhcapWnd,ErrorCallBackProc);
  589. if assigned(fcapVideoStream) then
  590. capSetCallbackOnVideoStream(fhcapwnd,VideoStreamCallbackProc);
  591. if assigned(fcapAudioStream) then
  592. capSetCallbackOnWaveStream(fhcapWnd,AudioStreamCallbackProc);
  593. if not capDriverConnect(fhCapWnd, fdriverIndex) then
  594. begin
  595. Ex:= ENotConnectException.Create('Can not connect capture driver with capture window');
  596. Destroycapwindow;
  597. GetDriverStatus(true);
  598. raise ex;
  599. exit;
  600. end;
  601. CreateTmpFile(True);
  602. capPreviewScale(fhCapWnd, fscale);
  603. capPreviewRate(fhCapWnd, round( 1/fpreviewrate*1000));
  604. GetDriverStatus(true);
  605. Sizecap;
  606. result:= true;
  607. end;
  608. (*------------------------------------------------------------------------*)
  609. // Setting callbacks as events
  610. procedure TVideoCap.SetStatCallBack(value:TCapStatusCallback);
  611. begin
  612. fcapStatusCallBack := value;
  613. if DriverOpen then
  614. if assigned(fcapStatusCallBack) then
  615. capSetCallbackOnStatus(fhcapWnd ,StatusCallbackProc)
  616. else
  617. capSetCallbackOnStatus(fhcapWnd ,nil);
  618. end;
  619. procedure TVideoCap.SetCapVideoStream(value:TVideoStream);
  620. begin
  621. fcapVideoStream:= value;
  622. if DriverOpen then
  623. if assigned(fcapVideoStream) then
  624. capSetCallbackOnVideoStream(fhcapwnd,VideoStreamCallbackProc)
  625. else
  626. capSetCallbackOnVideoStream(fhcapwnd, nil);
  627. end;
  628. procedure TVideoCap.SetCapFrameCallback(value:TVideoStream);
  629. begin
  630. fcapframeCallback:= value;
  631. if DriverOpen then
  632. if assigned(fcapFrameCallback) then
  633. capSetCallbackOnFrame(fhcapwnd,FrameCallBackProc)
  634. else
  635. capSetCallbackOnFrame(fhcapwnd, nil);
  636. end;
  637. procedure TVideoCap.SetCapAudioStream(value:TAudioStream);
  638. begin
  639. fcapAudioStream:= value;
  640. if DriverOpen then
  641. if assigned(fcapAudioStream) then
  642. capSetCallbackOnWaveStream(fhcapWnd,AudioStreamCallbackProc)
  643. else
  644. capSetCallbackOnWaveStream(fhcapWnd,nil);
  645. end;
  646. procedure TVideoCap.SetCapError(value:TError);
  647. begin
  648. fcapError:= value;
  649. if DriverOpen then
  650. if assigned(fcapError) then
  651. capSetCallbackOnError(fhcapWnd,ErrorCallbackProc)
  652. else
  653. capSetCallbackOnError(fhcapWnd,nil);
  654. end;
  655. (*---------------------------------------------------------------*)
  656. procedure TVideoCap.DestroyCapWindow;
  657. begin
  658. if fhCapWnd = 0 then exit;
  659. CreateTmpFile(False);
  660. CapDriverDisconnect(fhCapWnd);
  661. SetWindowLong(fhcapWnd,GWL_WNDPROC,GetWindowLong(fhcapwnd,GWL_USERDATA)); // Old windowproc
  662. DestroyWindow( fhCapWnd ) ;
  663. fhCapWnd := 0;
  664. end;
  665. (*---------------------------------------------------------------*)
  666. function TVideoCap.GetHasVideoOverlay:Boolean;
  667. begin
  668. if getDriverCaps then
  669. Result := fpDriverCaps^.fHasOverlay
  670. else
  671. result:= false;
  672. end;
  673. (*---------------------------------------------------------------*)
  674. function TVideoCap.GetHasDlgVFormat:Boolean;
  675. begin
  676. if getDriverCaps then
  677. Result := fpDriverCaps^.fHasDlgVideoFormat
  678. else
  679. result:= false;
  680. end;
  681. (*---------------------------------------------------------------*)
  682. function TVideoCap.GetHasDlgVDisplay : Boolean;
  683. begin
  684. if getDriverCaps then
  685. Result := fpDriverCaps^.fHasDlgVideoDisplay
  686. else
  687. result:= false;
  688. end;
  689. (*---------------------------------------------------------------*)
  690. function TVideoCap.GetHasDlgVSource : Boolean;
  691. begin
  692. if getDriverCaps then
  693. Result := fpDriverCaps^.fHasDlgVideoSource
  694. else
  695. result:= false;
  696. end;
  697. (*---------------------------------------------------------------*)
  698. function TVideoCap.DlgVFormat:boolean;
  699. var savestat : integer;
  700. begin
  701. result:= false;
  702. if fdriverIndex = -1 then exit;
  703. savestat := fhCapwnd;
  704. if fhCapWnd = 0 then
  705. if not CreateCapWindow then exit;
  706. result :=capDlgVideoFormat(fhCapWnd);
  707. if result then GetDriverStatus(true);
  708. if savestat = 0 then destroyCapWindow;
  709. if result then
  710. begin
  711. Sizecap;
  712. Repaint;
  713. end;
  714. end;
  715. (*---------------------------------------------------------------*)
  716. function TVideoCap.DlgVDisplay:boolean;
  717. var savestat : integer;
  718. begin
  719. result:= false;
  720. if fdriverIndex = -1 then exit;
  721. savestat := fhCapwnd;
  722. if fhCapWnd = 0 then
  723. if not CreateCapWindow then exit;
  724. result:=capDlgVideoDisplay(fhCapWnd) ;
  725. if result then GetDriverStatus(true);
  726. if savestat = 0 then destroyCapWindow;
  727. if result then
  728. begin
  729. SizeCap;
  730. Repaint;
  731. end;
  732. end;
  733. (*---------------------------------------------------------------*)
  734. function TVideoCap.DlgVSource:boolean;
  735. var savestat : integer;
  736. begin
  737. result:= false;
  738. if fdriverIndex = -1 then exit;
  739. savestat := fhCapwnd;
  740. if fhCapWnd = 0 then
  741. if not createCapWindow then exit;
  742. result:= capDlgVideoSource(fhCapWnd);
  743. if result then GetDriverStatus(true);
  744. if savestat = 0 then destroyCapWindow;
  745. if result then
  746. begin
  747. SizeCap;
  748. Repaint;
  749. end;
  750. end;
  751. (*---------------------------------------------------------------*)
  752. function TVideoCap.DlgVCompression;
  753. var savestat : integer;
  754. begin
  755. result:= false;
  756. if fdriverIndex = -1 then exit;
  757. savestat := fhCapwnd;
  758. if fhCapWnd = 0 then
  759. if not createCapWindow then exit;
  760. result:=capDlgVideoCompression(fhCapWnd);
  761. if savestat = 0 then destroyCapWindow;
  762. end;
  763. (*---------------------------------------------------------------*)
  764. // Single Frame Grabbling
  765. function TVideoCap.GrabFrame:boolean;
  766. begin
  767. result:= false;
  768. if not DriverOpen then exit;
  769. Result:= capGrabFrame(fhcapwnd);
  770. if result then GetDriverStatus(true);
  771. end;
  772. function TVideoCap.GrabFrameNoStop:boolean;
  773. begin
  774. result:= false;
  775. if not DriverOpen then exit;
  776. Result:= capGrabFrameNoStop(fhcapwnd);
  777. if result then GetDriverStatus(true);
  778. end;
  779. (*---------------------------------------------------------------*)
  780. // save frame as DIP
  781. function TVideoCap.SaveAsDIB:Boolean;
  782. var s:array[0..MAX_PATH] of char;
  783. begin
  784. result:= false;
  785. if not DriverOpen then exit;
  786. result := capFileSaveDIB(fhcapwnd,strpCopy(s,fCapSingleImageFileName));
  787. end;
  788. function TVideoCap.SaveToClipboard:boolean;
  789. begin
  790. result:= false;
  791. if not Driveropen then exit;
  792. result:= capeditCopy(fhcapwnd);
  793. end;
  794. (*---------------------------------------------------------------*)
  795. procedure TVideoCap.Setoverlay(value:boolean);
  796. var ex:Exception;
  797. begin
  798. if value = GetOverlay then exit;
  799. if gethasVideoOverlay = false then
  800. begin
  801. Ex:= ENoOverlayException.Create('Driver has no overlay mode');
  802. raise ex;
  803. exit;
  804. end;
  805. if value = true then
  806. begin
  807. if fhcapWnd = 0 then CreateCapWindow;
  808. GrabFrame;
  809. end;
  810. capOverlay(fhCapWnd,value);
  811. GetDriverStatus(true);
  812. invalidate;
  813. end;
  814. function TVideoCap.GetOverlay:boolean;
  815. begin
  816. if fhcapWnd = 0 then result := false
  817. else
  818. result:= fpDriverStatus^.fOverlayWindow;
  819. end;
  820. (*---------------------------------------------------------------*)
  821. procedure TVideoCap.SetPreview(value:boolean);
  822. begin
  823. if value = GetPreview then exit;
  824. if value = true then
  825. if fhcapWnd = 0 then CreateCapWindow;
  826. capPreview(fhCapWnd,value);
  827. GetDriverStatus(true);
  828. invalidate;
  829. end;
  830. function TVideoCap.GetPreview:boolean;
  831. begin
  832. if fhcapWnd = 0 then result := false
  833. else
  834. result:= fpDriverStatus^.fLiveWindow;
  835. end;
  836. procedure TVideoCap.SetPreviewRate(value:word);
  837. begin
  838. if value = fpreviewrate then exit;
  839. if value < 1 then value := 1;
  840. if value > 30 then value := 30;
  841. fpreviewrate:= value;
  842. if DriverOpen then capPreviewRate(fhCapWnd, round( 1/fpreviewrate*1000));
  843. end;
  844. (*---------------------------------------------------------------*)
  845. procedure TVideoCap.SetMicroSecPerFrame(value:cardinal);
  846. begin
  847. if value = fmicrosecpframe then exit;
  848. if value < 33333 then value := 33333;
  849. fmicrosecpframe := value;
  850. end;
  851. procedure TVideoCap.setFrameRate(value:word);
  852. begin
  853. if value <> 0 then fmicrosecpframe:= round(1.0/value*1000000.0);
  854. end;
  855. function TVideoCap.GetFrameRate:word;
  856. begin
  857. if fmicrosecpFrame > 0 then
  858. result:= round(1./ fmicrosecpframe * 1000000.0)
  859. else
  860. result:= 0;
  861. end;
  862. function TVideoCap.StartCapture;
  863. var CapParms:TCAPTUREPARMS;
  864. name:array[0..MAX_PATH] of char;
  865. begin
  866. result := false;
  867. if not DriverOpen then exit;
  868. capCaptureGetSetup(fhCapWnd, @CapParms, sizeof(TCAPTUREPARMS));
  869. if ftempfilename='' then
  870. capFileSetCaptureFile(fhCapWnd,strpCopy(name, fCapVideoFileName));
  871. CapParms.dwRequestMicroSecPerFrame := fmicrosecpframe;
  872. CapParms.fLimitEnabled := BOOL(FCapTimeLimit);
  873. CapParms.wTimeLimit := fCapTimeLimit;
  874. CapParms.fCaptureAudio := fCapAudio;
  875. CapParms.fMCIControl := FALSE;
  876. CapParms.fYield := TRUE;
  877. //CapParms.vKeyAbort := VK_ESCAPE;
  878. CapParms.vKeyAbort := 0;
  879. CapParms.fAbortLeftMouse := FALSE;
  880. CapParms.fAbortRightMouse := FALSE;
  881. if CapParms.fLimitEnabled then // Calculate Indexsize
  882. begin
  883. CapParms.dwIndexSize:= frameRate*FCapTimeLimit; // For Video Frames
  884. If fCapAudio then
  885. CapParms.dwIndexSize := CapParms.dwIndexSize + 5*FCapTimeLimit; // Additional Buffer for Audio
  886. end
  887. else
  888. begin
  889. If CapParms.dwIndexSize = 0 then // Default Value
  890. CapParms.DwIndexSize := 100000 // Value bigger then default for larger Videos
  891. else
  892. CapParms.dwIndexSize := findexSize; // IndexSize by user
  893. end;
  894. if CapParms.dwIndexSize < 1800 then CapParms.dwIndexSize:= 1800; // Limit Control
  895. If CapParms.dwIndexSize > 324000 then CapParms.dwIndexSize:= 324000;
  896. capCaptureSetSetup(fhCapWnd, @CapParms, sizeof(TCAPTUREPARMS));
  897. if fCapAudio then FAudioformat.SetAudio(fhcapWnd);
  898. if CapToFile then
  899. result:= capCaptureSequence(fhCapWnd)
  900. else
  901. result := capCaptureSequenceNoFile(fhCapWnd);
  902. GetDriverStatus(true);
  903. end;
  904. function TVideoCap.StopCapture;
  905. begin
  906. result:=false;
  907. if not DriverOpen then exit;
  908. result:=CapCaptureStop(fhcapwnd);
  909. GetDriverStatus(true);
  910. end;
  911. function TVideoCap.SaveCap:Boolean;
  912. var name:array[0..MAX_PATH] of char;
  913. begin
  914. result := capFileSaveAs(fhcapwnd,strPCopy(name,fCapVideoFileName)); // strpCopy(name, fCapVideoFileName));
  915. end;
  916. procedure TVideoCap.SetIndexSize(value:cardinal);
  917. begin
  918. if value = 0 then
  919. begin
  920. findexSize:= 0;
  921. exit;
  922. end;
  923. if value < 1800 then value := 1800;
  924. if value > 324000 then value := 324000;
  925. findexsize:= value;
  926. end;
  927. function TVideoCap.GetCapInProgress:boolean;
  928. begin
  929. result:= false;
  930. if not DriverOpen then exit;
  931. GetDriverStatus(false);
  932. result:= fpDriverStatus^.fCapturingNow ;
  933. end;
  934. (*---------------------------------------------------------------*)
  935. Procedure TVideoCap.SetScale(value:boolean);
  936. begin
  937. if value = fscale then exit;
  938. fscale:= value;
  939. if DriverOpen then
  940. begin
  941. capPreviewScale(fhCapWnd, fscale);
  942. SizeCap;
  943. end;
  944. Repaint;
  945. end;
  946. Procedure TVideoCap.Setprop(value:Boolean);
  947. begin
  948. if value = fprop then exit;
  949. fprop:=value;
  950. if DriverOpen then Sizecap;
  951. Repaint;
  952. end;
  953. (*---------------------------------------------------------------*)
  954. function TVideoCap.GetCapWidth;
  955. begin
  956. if assigned(fpDriverStatus) then
  957. result:= fpDriverStatus^.uiImageWidth
  958. else
  959. result:= 0;
  960. end;
  961. function TVideoCap.GetCapHeight;
  962. begin
  963. if assigned(fpDriverStatus) then
  964. result:= fpDriverStatus^.uiImageHeight
  965. else
  966. result:= 0;
  967. end;
  968. (*---------------------------------------------------------------*)
  969. Procedure TVideoCap.SetDriverOpen(value:boolean);
  970. begin
  971. if value = GetDriverOpen then exit;
  972. if value = false then DestroyCapWindow;
  973. if value = true then CreateCapWindow;
  974. end;
  975. function TVideoCap.GetDriverOpen:boolean;
  976. begin
  977. result := fhcapWnd <> 0;
  978. end;
  979. (*---------------------------------------------------------------*)
  980. // Singele frame Capturing
  981. function TVideoCap.CapSingleFramesOpen:boolean;
  982. var name :array [0..MAX_PATH] of char;
  983. CapParms:TCAPTUREPARMS;
  984. begin
  985. result := false;
  986. if not DriverOpen then exit;
  987. capCaptureGetSetup(fhCapWnd, @CapParms, sizeof(TCAPTUREPARMS));
  988. if ftempfilename='' then
  989. capFileSetCaptureFile(fhCapWnd,strpCopy(name, fCapVideoFileName));
  990. CapParms.dwRequestMicroSecPerFrame := fmicrosecpframe;
  991. CapParms.fLimitEnabled := BOOL(0);
  992. CapParms.fCaptureAudio := false;
  993. CapParms.fMCIControl := FALSE;
  994. CapParms.fYield := TRUE;
  995. CapParms.vKeyAbort := VK_ESCAPE;
  996. CapParms.dwIndexSize := findexSize; // IndexSize by user
  997. if CapParms.dwIndexSize < 1800 then CapParms.dwIndexSize:= 1800; // Limit Control
  998. If CapParms.dwIndexSize > 324000 then CapParms.dwIndexSize:= 324000;
  999. capCaptureSetSetup(fhCapWnd, @CapParms, sizeof(TCAPTUREPARMS));
  1000. result:= capCaptureSingleFrameOpen(fhcapWnd);
  1001. end;
  1002. function TVideoCap.CapSingleFramesClose:boolean;
  1003. var E:Exception;
  1004. begin
  1005. if not driverOpen then
  1006. begin
  1007. e:= ENotOpen.Create('Driver not Open');
  1008. raise e;
  1009. exit;
  1010. end;
  1011. result:= CapCaptureSingleFrameClose(fhcapWnd);
  1012. end;
  1013. function TVideoCap.CapSingleFrame:boolean;
  1014. var E:Exception;
  1015. begin
  1016. if not driverOpen then
  1017. begin
  1018. e:= ENotOpen.Create('Driver not Open');
  1019. raise e;
  1020. exit;
  1021. end;
  1022. result:= CapCaptureSingleFrame(fhcapWnd);
  1023. end;
  1024. ///////////////////////////////////////////////////////////////////////////
  1025. constructor TAudioFormat.create;
  1026. begin
  1027. inherited create;
  1028. FChannels:=Mono;
  1029. FFrequency:=f8000Hz;
  1030. Fres:=r8Bit;
  1031. end;
  1032. procedure TAudioFormat.SetAudio(handle:Thandle);
  1033. Var WAVEFORMATEX:TWAVEFORMATEX;
  1034. begin
  1035. if handle= 0 then exit; // No CapWindow
  1036. capGetAudioFormat(handle,@WAVEFORMATEX, SizeOf(TWAVEFORMATEX));
  1037. case FFrequency of
  1038. f8000hz :WAVEFORMATEX.nSamplesPerSec:=8000;
  1039. f11025Hz:WAVEFORMATEX.nSamplesPerSec:=11025;
  1040. f22050Hz:WAVEFORMATEX.nSamplesPerSec:=22050;
  1041. f44100Hz:WAVEFORMATEX.nSamplesPerSec:=44100;
  1042. end;
  1043. WAVEFORMATEX.nAvgBytesPerSec:= WAVEFORMATEX.nSamplesPerSec;
  1044. if FChannels=Mono then
  1045. WAVEFORMATEX.nChannels:=1
  1046. else
  1047. WAVEFORMATEX.nChannels:=2;
  1048. if FRes=r8Bit then
  1049. WAVEFORMATEX.wBitsPerSample:=8
  1050. else
  1051. WAVEFORMATEX.wBitsPerSample:=16;
  1052. capSetAudioFormat(handle,@WAVEFORMATEX, SizeOf(TWAVEFORMATEX));
  1053. end;
  1054. ///////////////////////////////////////////////////////////////////////////
  1055. // Creating a list with capture drivers
  1056. Function GetDriverList:TStringList;
  1057. var i:integer;
  1058. name:array[0..80] of char;
  1059. ver :array[0..80] of char;
  1060. begin
  1061. result:= TStringList.Create;
  1062. result.Capacity:= 10;
  1063. result.Sorted:= false;
  1064. for i:= 0 to 9 do
  1065. if capGetDriverDescription( i,name,80,ver,80) then
  1066. result.Add(StrPas(name)+ ' '+strpas(ver))
  1067. else
  1068. break;
  1069. end;
  1070. procedure FrameToBitmap(Bitmap:TBitmap;FrameBuffer:pointer; BitmapInfo:TBitmapInfo);
  1071. var hdd:Thandle;
  1072. begin
  1073. with Bitmap do
  1074. begin
  1075. Width:= BitmapInfo.bmiHeader.biWidth; // New size of Bitmap
  1076. Height:=Bitmapinfo.bmiHeader.biHeight;
  1077. hdd:= DrawDibOpen;
  1078. DrawDibDraw(hdd,canvas.handle,0,0,BitmapInfo.BmiHeader.biwidth,BitmapInfo.bmiheader.biheight,@BitmapInfo.bmiHeader,
  1079. frameBuffer,0,0,bitmapInfo.bmiHeader.biWidth,bitmapInfo.bmiHeader.biheight,0);
  1080. DrawDibClose(hdd);
  1081. end;
  1082. end;
  1083. procedure BitmapToFrame(Bitmap:TBitmap; FrameBuffer:pointer; BitmapInfo:TBitmapInfo);
  1084. var ex:Exception;
  1085. begin
  1086. if bitmapInfo.bmiHeader.BiCompression <> bi_RGB then
  1087. begin
  1088. ex:= EFalseFormat.Create('Not Supported DIB format');
  1089. raise ex ;
  1090. end;
  1091. with Bitmap do
  1092. GetDiBits(canvas.handle,handle,0,BitmapInfo.bmiHeader.biheight,FrameBuffer,BitmapInfo,DIB_RGB_COLORS);
  1093. end;
  1094. function FrametobgrFrame(FrameBuffer:pointer; bgrFrameBuffer:pointer; BitmapInfo:TBitmapInfo): boolean;
  1095. //const
  1096. // init: boolean=false;
  1097. var
  1098. ic: HIC;
  1099. info: TBitmapInfoHeader;
  1100. r: Integer;
  1101. hdd:Thandle;
  1102. ex:Exception;
  1103. begin
  1104. // if not init then
  1105. info := BitmapInfo.bmiHeader;
  1106. info.biCompression := BI_RGB;
  1107. info.biBitCount := 24;
  1108. info.biSizeImage := info.biWidth*info.biHeight*3;
  1109. ic := ICLocate(ICTYPE_VIDEO, 0, @BitmapInfo.bmiHeader, @info,ICMODE_FASTDECOMPRESS);
  1110. ICDecompressBegin(ic, @BitmapInfo.bmiHeader, @info);
  1111. R := ICDecompress(ic, ICDECOMPRESS_HURRYUP{}, @BitmapInfo.bmiHeader, FrameBuffer, @info,bgrFrameBuffer);
  1112. ICDecompressEnd(ic);
  1113. ICClose(ic);
  1114. Result := true; //R=ICERR_OK;
  1115. // ICClose(ic);{}{
  1116. with Tbitmap.Create do
  1117. try
  1118. Width := BitmapInfo.bmiHeader.biWidth; // New size of Bitmap
  1119. Height := BitmapInfo.bmiHeader.biheight;
  1120. PixelFormat := pf24bit;
  1121. hdd:= DrawDibOpen;
  1122. Result := DrawDibDraw(hdd,canvas.handle,0,0,BitmapInfo.bmiHeader.biWidth,BitmapInfo.bmiHeader.biheight, @BitmapInfo.bmiHeader,
  1123. frameBuffer,0,0,BitmapInfo.bmiHeader.biWidth,BitmapInfo.bmiHeader.biheight,0);
  1124. DrawDibClose(hdd);
  1125. if Result then
  1126. begin
  1127. BitmapInfo.bmiHeader.biCompression := BI_RGB;
  1128. BitmapInfo.bmiHeader.biBitCount := 24;
  1129. BitmapInfo.bmiHeader.biSizeImage := BitmapInfo.bmiHeader.biWidth*BitmapInfo.bmiHeader.biHeight*3;
  1130. GetDiBits(canvas.handle,handle,0,BitmapInfo.bmiHeader.biheight,bgrFrameBuffer,BitmapInfo,DIB_RGB_COLORS);
  1131. end;
  1132. finally
  1133. Free;
  1134. end; {}
  1135. end;
  1136. procedure Register;
  1137. begin
  1138. RegisterComponents( 'Video', [TVideoCap]);
  1139. end;
  1140. end.