Mega Code Archive

 
Categories / Delphi / System
 

A simple way of p2p camera viewing (Via Windows API)

Title: A simple way of p2p camera viewing (Via Windows API) Question: Here is a way to connect to the webcam and share the movie ;). The capture is firstly saved as a bitmap,then converted to jpeg and last sended to the client. Answer: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdTCPConnection, IdTCPClient, IdMappedPortTCP, IdUDPBase,jpeg, IdUDPServer, IdUDPClient,IdSocketHandle, IdAntiFreezeBase, IdAntiFreeze,vfw, ExtCtrls, Buttons; type TForm1 = class(TForm) srv: TIdTCPServer; IdAntiFreeze1: TIdAntiFreeze; Timer1: TTimer; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; procedure FormShow(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure srvExecute(AThread: TIdPeerThread); procedure Timer1Timer(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); private { Private declarations } public h:hwnd; resim_verisi:array[0..50000] of char; dosya:file of char; boyut:longint; kamera_kapat:boolean; kamera_kapali:boolean; kapat:boolean; bilgi:BITMAPINFO; procedure boskalinca(Sender: TObject; var Done: Boolean); { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormShow(Sender: TObject); begin kapat:=false; kamera_kapat:=falsE; if (FileExists('temp.avi')) then DeleteFile('temp.avi'); {Grnt yakalma penceresini olutur} h := capCreateCaptureWindow ('Kamera nizleme', WS_CHILD or WS_VISIBLE or WS_THICKFRAME , 100,100, 160, 120, Handle, 11011); {h penceresini (oluturduumuz pencere) kameraya balayan fonksiyon} capDriverConnect (h, 0); // 0 -means default driver. {grntnn tutulaca yakalama animasyonu} capFileSetCaptureFile(h, 'temp.avi') ; {grntnn ayarlarn renen fonksiyon . Bit ilem resmi formatnda } capGetVideoFormat(h, @bilgi,sizeof(bilgi)); {bilgi ye gre h penceresinin konumunu ve boyutunu ayarlayan fonksiyon} SetWindowPos(h,HWND_TOP,0,0,bilgi.bmiHeader.biWidth,bilgi.bmiHeader.biHeight, SWP_SHOWWINDOW ); //capSetCallbackOnFrame(h,resimgonder); {Tekli grnt yakalamay balatan fonksiyon} capCaptureSingleFrameOpen(h); // capSetCallbackOnYield(h,hazir); assignfile(dosya,'temp.jpg'); {uygulamann bota kalnca altraca fonksiyon} Application.OnIdle:=boskalinca; end; procedure TForm1.boskalinca(Sender: TObject; var Done: Boolean); begin end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin if(not kamera_kapat) then capdriverdisconnect(h); end; procedure TForm1.srvExecute(AThread: TIdPeerThread); var i:integer; begin i:=(AThread.Connection.ReadInteger(false)); ///////////////////// if i=0 then begin AThread.Connection.WriteBuffer(boyut,sizeof(longint),false); AThread.Connection.WriteBuffer(resim_verisi,boyut,false); end; /////////////////// ///////////////////// if i=1 then begin kapat:=true; end; /////////////////// ///////////////////// if i=2 then begin kamera_kapali:=true; capdriverdisconnect(h); caption:='Kamera kapal'; end; /////////////////// ///////////////////// if i=3 then begin capdriverconnect(h,0); kamera_kapali:=false; caption:='Kamera ak'; end; /////////////////// end; procedure TForm1.Timer1Timer(Sender: TObject); var dib:tpicture; jpg:tjpegimage; begin if not kamera_kapali then begin capcapturesingleframe(h); capcapturesingleframeclose(h); capfilesavedib(h,'temp.bmp'); capcapturesingleframeopen(h); dib:=tpicture.Create; jpg:=tjpegimage.Create; dib.LoadFromFile('temp.bmp'); jpg.Assign(dib.Bitmap); jpg.SaveToFile('temp.jpg'); reset(dosya); Boyut:=FileSize(Dosya); blockread(dosya,resim_verisi,sizeof(resim_verisi),boyut); closefile(dosya); jpg.Free; dib.Free; end; if kapat then close; end; procedure TForm1.SpeedButton1Click(Sender: TObject); begin capcapturesingleframeclose(h); capdlgvideoformat(h); capGetVideoFormat(h, @bilgi,sizeof(bilgi)); {bilgi ye gre h penceresinin konumunu ve boyutunu ayarlayan fonksiyon} SetWindowPos(h,HWND_TOP,0,0,bilgi.bmiHeader.biWidth,bilgi.bmiHeader.biHeight, SWP_SHOWWINDOW ); capcapturesingleframeopen(h); end; procedure TForm1.SpeedButton2Click(Sender: TObject); begin capcapturesingleframeclose(h); capdlgvideosource(h); capcapturesingleframeopen(h); end; end.