Video for Windows capture component |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
VCL Components - TCapture資料來源: http://www.hobbypages.net/vcl_tcapture.asp Clicky here to begin download of TCapture.ZIP Video for Windows capture component (BETA) That means it's in testing stages. It does not mean it is ready for real-world programming. You wanna test it - go for it. You wanna use it for real, use it at your own risk. I'll remove all this scary text when I deem it fully functional and bug free. That's not to say it isnt powerful and easy to use. Someone asked me to write a demo flaunting it's usefulness. So I did. This is not a finished product - please either report bugs to me, or fix them before even thinking about using this component in real-world products. Demo Available: Clicky here to begin download of DEMO (hpcam.zip) ScreenShot and source code: The reason I don't include a zipped project is simple .... too many builder versions. main header (main.h)//--------------------------------------------------------------------------- #ifndef mainH #define mainH //--------------------------------------------------------------------------- #include < Classes.hpp > #include < Controls.hpp > #include < StdCtrls.hpp > #include < Forms.hpp > #include < ComCtrls.hpp > #include < ExtCtrls.hpp > #include "Capture.h" #include < MPlayer.hpp > #include < Buttons.hpp > #include < Menus.hpp > //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TPanel *Panel1; TPanel *Panel2; TPanel *Panel4; TStatusBar *StatusBar1; TCapture *Capture1; TMediaPlayer *MediaPlayer1; TLabel *Label1; TLabel *Label2; TBitBtn *BitBtn1; TBitBtn *BitBtn2; TMemo *Memo1; TLabel *Label3; TLabel *Label4; TLabel *Label5; TLabel *Label6; TTimer *Timer1; TLabel *Label7; TButton *Button1; TButton *Button2; TButton *Button3; TCheckBox *CheckBox1; TButton *Button4; TCheckBox *CheckBox2; TCheckBox *CheckBox3; TGroupBox *GroupBox1; TRadioButton *RadioButton1; TRadioButton *RadioButton2; TEdit *Edit1; TBitBtn *BitBtn4; TCheckBox *CheckBox4; TProgressBar *ProgressBar1; TTrackBar *TrackBar1; TLabel *Label20; TLabel *Label21; TLabel *Label23; TComboBox *ComboBox1; TBevel *Bevel1; TCheckBox *CheckBox5; TLabel *Label10; TLabel *Label24; TButton *Button5; TButton *Button6; TCheckBox *CheckBox6; TButton *Button7; TBitBtn *BitBtn3; TCheckBox *CheckBox7; TButton *Button8; TButton *Button9; TMainMenu *MainMenu1; TMenuItem *Hardware; TMenuItem *DriverSelect1; TMenuItem *Format1; TMenuItem *Compression1; TMenuItem *Display1; TMenuItem *Capture2; TMenuItem *Start1; TMenuItem *Stop1; TMenuItem *Save1; TMenuItem *Connect1; TMenuItem *Disconnect1; TMenuItem *N1; TMenuItem *N2; TMenuItem *Exit1; TMenuItem *N3; TMenuItem *Snapshot1; TMenuItem *CopytoClipboard1; TMenuItem *N4; TMenuItem *ReloadPlayer1; TMenuItem *Help1; TMenuItem *Help2; TMenuItem *About1; TPaintBox *PaintBox1; TPanel *Panel3; TBitBtn *BitBtn5; void __fastcall FormCreate(TObject *Sender); void __fastcall BitBtn1Click(TObject *Sender); void __fastcall BitBtn2Click(TObject *Sender); void __fastcall Capture1Status(TObject *Sender, int iErrorID, AnsiString sErrorText); void __fastcall Capture1Yield(TObject *Sender); void __fastcall BitBtn3Click(TObject *Sender); void __fastcall Capture1Error(TObject *Sender, int iErrorID, AnsiString sErrorText); void __fastcall MediaPlayer1PostClick(TObject *Sender, TMPBtnType Button); void __fastcall MediaPlayer1Click(TObject *Sender, TMPBtnType Button, bool &DoDefault); void __fastcall Timer1Timer(TObject *Sender); void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Capture1VideoStream(TObject *Sender, LPVIDEOHDR lpVHdr); void __fastcall Capture1AudioStream(TObject *Sender, LPWAVEHDR lpWHdr); void __fastcall BitBtn4Click(TObject *Sender); void __fastcall Button4Click(TObject *Sender); void __fastcall TrackBar1Change(TObject *Sender); void __fastcall FormResize(TObject *Sender); void __fastcall ComboBox1Change(TObject *Sender); void __fastcall Button3Click(TObject *Sender); void __fastcall Button5Click(TObject *Sender); void __fastcall Button6Click(TObject *Sender); void __fastcall Button7Click(TObject *Sender); void __fastcall Edit1KeyPress(TObject *Sender, char &Key); void __fastcall Button8Click(TObject *Sender); void __fastcall Button9Click(TObject *Sender); void __fastcall BitBtn5Click(TObject *Sender); void __fastcall Exit1Click(TObject *Sender); private: // User declarations bool bUseTimestamp; //AVIFILEINFO AviFileInfo; public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif main form (main.cpp) //--------------------------------------------------------------------------- #include < vcl.h > #pragma hdrstop #include "main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "Capture" #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { bUseTimestamp = false; if(FileExists(Capture1->VideoFileName)) { BitBtn3->Enabled = true; ReloadPlayer1->Enabled = true; } MediaPlayer1->TimeFormat = tfMSF; MediaPlayer1->FileName = Capture1->VideoFileName; Panel3->Width = Capture1->CaptureStatus.uiImageWidth; Panel3->Height = Capture1->CaptureStatus.uiImageHeight; PaintBox1->Width = Panel3->Width; PaintBox1->Height = Panel3->Height; Label6->Caption = "1"; Capture1->EnumCapDrv(); for(int i=0; i < Capture1->pStringCapDrivers->Count; i ) { ComboBox1->Items->Add(Capture1->pStringCapDrivers->Strings[i]); } Button4Click(Sender);//update CaptureParams } //--------------------------------------------------------------------------- /*Start capturing video*/ void __fastcall TForm1::BitBtn1Click(TObject *Sender) { MediaPlayer1->Close(); BitBtn3->Enabled = false; BitBtn5->Enabled = false; ReloadPlayer1->Enabled = false; Capture1->SetCaptureParamDefaults(); Capture1->UpdateParms(); Capture1->UseCallbackOnWaveStream = true; Capture1->UseCallbackOnVideoStream = true; Capture1->UseCallbackOnError = true; Capture1->UseCallbackOnStatus = true; Capture1->UseCallbackOnYield = true; Capture1->StartCapture(true, (TObject*)Sender); } //--------------------------------------------------------------------------- /*Stop capturing video*/ void __fastcall TForm1::BitBtn2Click(TObject *Sender) { BitBtn3->Enabled = false; ReloadPlayer1->Enabled = false; if(!Capture1->StopCapture()) { Memo1->Lines->Add("STOPPING"); } } //--------------------------------------------------------------------------- /*TNotifyEvent OnStatus*/ void __fastcall TForm1::Capture1Status(TObject *Sender, int iErrorID, AnsiString sErrorText) { StatusBar1->Panels->Items[1]->Text = sErrorText; Memo1->Lines->Add(sErrorText); if(StatusBar1->Panels->Items[1]->Text == "Capture End") { BitBtn4->Click(); } } //--------------------------------------------------------------------------- /*TNotifyEvent OnYield*/ void __fastcall TForm1::Capture1Yield(TObject *Sender) { Application->ProcessMessages(); } //--------------------------------------------------------------------------- /*Reload Player Button*/ void __fastcall TForm1::BitBtn3Click(TObject *Sender) { HMMIO hFile;/*some stuff for Label 10 and 23*/ PAVIFILE anAVIfile; AVIFileOpen(&anAVIfile,Capture1->VideoFileName.c_str(), OF_SHARE_DENY_NONE, NULL); if ((hFile = mmioOpen(Capture1->VideoFileName.c_str(), NULL, MMIO_READ)) != NULL) { Label10->Caption = "Opened";// File opened successfully. } else { Label10->Caption = " Not Opened";// File cannot be opened. } AVIFILEINFO AviFileInfo; AVIFileInfo(anAVIfile, &AviFileInfo, sizeof(AVIFILEINFO)); if(FileExists(Capture1->VideoFileName)) { MediaPlayer1->Close(); MediaPlayer1->FileName = Capture1->VideoFileName; MediaPlayer1->Open(); Label21->Caption = Capture1->VideoFileName; Label23->Caption = AviFileInfo.szFileType; MediaPlayer1->Frames = 1; ProgressBar1->Max = MediaPlayer1->Length; TrackBar1->Max = MediaPlayer1->Length; TrackBar1->Enabled = true; } BitBtn5->Enabled = true; } //--------------------------------------------------------------------------- /*TNotifyEvent OnError*/ void __fastcall TForm1::Capture1Error(TObject *Sender, int iErrorID, AnsiString sErrorText) { StatusBar1->Panels->Items[1]->Text = sErrorText; Memo1->Lines->Add(sErrorText); } //--------------------------------------------------------------------------- /*Adjust Label6 to show current position of player*/ void __fastcall TForm1::MediaPlayer1PostClick(TObject *Sender, TMPBtnType Button) { Label6->Caption = IntToStr(MediaPlayer1->Position); } //--------------------------------------------------------------------------- /*In case the TrackBar had focus - MediaPlayer doenst take focus OnClick*/ void __fastcall TForm1::MediaPlayer1Click(TObject *Sender, TMPBtnType Button, bool &DoDefault) { MediaPlayer1->SetFocus(); } //--------------------------------------------------------------------------- /*Update AVI position label (Label6)*/ void __fastcall TForm1::Timer1Timer(TObject *Sender) { Label6->Caption = IntToStr(MediaPlayer1->Position); ProgressBar1->Position = StrToInt(Label6->Caption); TrackBar1->Position = StrToInt(Label6->Caption); } //--------------------------------------------------------------------------- /*Driver Format Dialog*/ void __fastcall TForm1::Button1Click(TObject *Sender) { Capture1->Format(); Panel3->Width = Capture1->CaptureStatus.uiImageWidth; Panel3->Height = Capture1->CaptureStatus.uiImageHeight; PaintBox1->Width = Panel3->Width; PaintBox1->Height = Panel3->Height; Capture1->MoveCaptureWindow(); //Repaint(); } //--------------------------------------------------------------------------- /*Driver Source Dialog*/ void __fastcall TForm1::Button2Click(TObject *Sender) { Capture1->Source(); Panel3->Width = Capture1->CaptureStatus.uiImageWidth; Panel3->Height = Capture1->CaptureStatus.uiImageHeight; PaintBox1->Width = Panel3->Width; PaintBox1->Height = Panel3->Height; Capture1->MoveCaptureWindow(); } //--------------------------------------------------------------------------- /*TNotifyEvent OnVideoStream*/ void __fastcall TForm1::Capture1VideoStream(TObject *Sender, LPVIDEOHDR lpVHdr) { int a = lpVHdr->dwTimeCaptured; Label5->Caption = IntToStr(a);//the one next to "Recording Time" } //--------------------------------------------------------------------------- /*Capture1 Callback OnAudioStream*/ void __fastcall TForm1::Capture1AudioStream(TObject *Sender, LPWAVEHDR lpWHdr) { int a = lpWHdr->dwBytesRecorded; //buffer size Label4->Caption = IntToStr(a);//"Audio Buffer Size =" } //--------------------------------------------------------------------------- /*TNotifyEvent OnFormShow*/ /*Save Button*/ void __fastcall TForm1::BitBtn4Click(TObject *Sender) { BitBtn3->Enabled = true; ReloadPlayer1->Enabled = true; Label21->Caption = Capture1->VideoFileName; int i = Capture1->SaveCapture(); if(i == 0) { StatusBar1->Panels->Items[0]->Text = "Save Failed"; } else { StatusBar1->Panels->Items[0]->Text = "Save Succeeded"; } } //--------------------------------------------------------------------------- /*Update Capture1->CaptureParams (CAPTUREPARAMS)*/ void __fastcall TForm1::Button4Click(TObject *Sender) { if(CheckBox1->Checked) { Capture1->CaptureParams.fLimitEnabled = true; Capture1->CaptureParams.wTimeLimit = StrToInt(Edit1->Text); } else { Capture1->CaptureParams.fLimitEnabled = false; } if(CheckBox2->Checked) { Capture1->LeftMouseAbort = true; } else { Capture1->LeftMouseAbort = false; } if(CheckBox3->Checked) { Capture1->RightMouseAbort = true; } else { Capture1->RightMouseAbort = false; } if(RadioButton1->Checked) { Capture1->MicroSecPerFrame = Capture1->Fast_fps; } if(RadioButton2->Checked) { Capture1->MicroSecPerFrame = Capture1->Slow_fps; } if(CheckBox4->Checked) { Capture1->PromptToSave = true; } else { Capture1->PromptToSave = false; } if(CheckBox5->Checked) { Capture1->CaptureAudio = true; } else { Capture1->CaptureAudio = false; } if(CheckBox6->Checked) { Capture1->MustYield = true; } else { Capture1->MustYield = false; } if(CheckBox7->Checked) { bUseTimestamp = true; } else { bUseTimestamp = false; } Capture1->SetCaptureParamDefaults(); } //--------------------------------------------------------------------------- /*Moves the position in the MediaPlayer*/ void __fastcall TForm1::TrackBar1Change(TObject *Sender) { if(MediaPlayer1->Position != -1) { if(TrackBar1->Focused() == true) { MediaPlayer1->Position = TrackBar1->Position; } } } //--------------------------------------------------------------------------- /*having problems with the MediaPlayer output panel*/ void __fastcall TForm1::FormResize(TObject *Sender) { Panel3->Width = Capture1->CaptureStatus.uiImageWidth; Panel3->Height = Capture1->CaptureStatus.uiImageHeight; PaintBox1->Width = Panel3->Width; PaintBox1->Height = Panel3->Height; } //--------------------------------------------------------------------------- /*Changing drivers? lets reset it*/ void __fastcall TForm1::ComboBox1Change(TObject *Sender) { Capture1->Disconnect(); Capture1->Connect(Capture1->pStringCapDrivers->IndexOf(ComboBox1->Text)); } //--------------------------------------------------------------------------- /*Open Compression Dialog*/ void __fastcall TForm1::Button3Click(TObject *Sender) { Capture1->SetCompression(); } //--------------------------------------------------------------------------- /*Open Display Dialog*/ void __fastcall TForm1::Button5Click(TObject *Sender) { Capture1->GetDisplay(); } //--------------------------------------------------------------------------- /*Grab a still image*/ void __fastcall TForm1::Button6Click(TObject *Sender) { if(bUseTimestamp) { TDateTime Time(Now()); String s = ".bmp"; String fname = Time.FormatString("mmddhnnss") s; Capture1->Snapshot(fname.c_str()); } else { Capture1->Snapshot("SnapShot.bmp"); } } //--------------------------------------------------------------------------- /*copy still image to clipboard - Snapshot not required*/ void __fastcall TForm1::Button7Click(TObject *Sender) { Capture1->CopyToClipboard(); } //--------------------------------------------------------------------------- /*keeps it numbers*/ void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key) { if(!isdigit(Key)) { Key = NULL; } } //--------------------------------------------------------------------------- /*Connect button*/ void __fastcall TForm1::Button8Click(TObject *Sender) { Capture1->Connect(Capture1->pStringCapDrivers->IndexOf(ComboBox1->Text)); } //--------------------------------------------------------------------------- /*Disconnect button*/ void __fastcall TForm1::Button9Click(TObject *Sender) { Capture1->Disconnect(); } //--------------------------------------------------------------------------- /* take a snapshot of a playback window AVIframe(saves as snapshot.dib) this is not a quality image, since this is a demo of the TCapture component - not a source for a high end AVI editor :-) */ void __fastcall TForm1::BitBtn5Click(TObject *Sender) { // TCanvas *cCanvas = new TCanvas; cCanvas->Handle = GetDC(MediaPlayer1->Display->Handle); Graphics::TBitmap *bitmap = new Graphics::TBitmap; bitmap->Width = Panel3->Width; bitmap->Height = Panel3->Height; TRect src = MediaPlayer1->DisplayRect; TRect dst = PaintBox1->ClientRect; bitmap->Canvas->CopyRect(dst, cCanvas, src); bitmap->SaveToFile(Capture1->StillImageFileName); delete bitmap; delete cCanvas; } //--------------------------------------------------------------------------- /*Closes program*/ void __fastcall TForm1::Exit1Click(TObject *Sender) { Close(); } //---------------------------------------------------------------------------As always - software made available here at the HobbyPages is always free - and royalty-free. For me, it's a learning experience, and a joy to be of service. TCapture VCL元件原始碼如下: /*==================== Capture.cpp ================================ VERSION .07 beta - November 22,2002 The windows you create by using the TCapture VCL Component can perform the following tasks: ?Capture audio and video streams to an audo-video interleaved (AVI) file. ?Connect and disconnect video and audio input devices dynamically. ?View a live incoming video signal by using the overlay or preview methods. ?Specify a file to use when capturing and copy the contents of the capture file to another file. ?Set the capture rate. ?Display dialog boxes that control the video source and format. ?Create, save, and load palettes. ?Copy images and palettes to the clipboard. ?Capture and save a single image as a device-independent bitmap (DIB). The API documentations for AVI Capture can be found in the MS Help Files folder, MMEDIA.HLP, topic "Video Capture" Format = Video for Windows VERSION CONTROL: www.hobbypages.net ver .01 - 04 beta - unreleased to the public Malcolm Smith was the only one to see it in this state. ver .05 beta - released October 16, 2002 original formula - no sugar added ver .06 beta - released November 20, 2002 fixed user intervention for resizing events removed FrameData event - duplicated Satus event anyway ver .07 beta - released November 22, 2002 StartCapture(...) would stop after about 15 frames. This was caused by not reading the existing CAPTUREPARAMS before writing them. This was corrected. Changed a few other things - I forget now. */ //--------------------------------------------------------------------------- #include < vcl.h > #pragma hdrstop #include < stdio.h > #include "Capture.h" #pragma package(smart_init) //--------------------------------------------------------------------------- static inline void ValidCtrCheck(TCapture *) { new TCapture(NULL); } //--------------------------------------------------------------------------- namespace Capture { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TCapture)}; RegisterComponents("HobbyPages", classes, 0); } } //--------------------------------------------------------------------------- /*CALLBACK Functions*/ //--------------------------------------------------------------------------- LRESULT CALLBACK ::OnErrorCallback(HWND hWnd, int nErrID, LPCSTR lpErrorText) { //TCapture Control; TCapture *Control = reinterpret_cast |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |