各位前輩大家好,小弟參照版上的許多作品,欲利用IdTCPServer發送由WebCam所擷取的TMemoryStream至Client端做處理,但是不知道哪裡出了錯,接收影像時會出現Otu of memory while expending memory stream的訊息,小弟的程式如下,煩請前輩們多多指教。謝謝。
Server端: //--------------------------------------------------------------------------- #include
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdTCPServer"
#pragma link "IdIPWatch"
#pragma resource "*.dfm"
TForm1 *Form1; HWND GetCapWnd;
LPVIDEOHDR VideoStr;
LRESULT CALLBACK GetVideo( HWND hwnd, Longint lpvideo );
TMemoryStream *stream = new TMemoryStream; //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
IdTCPServer1->DefaultPort=4000; //IdTCPServer1元件
IdTCPServer1->Active=true;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK GetVideo( HWND hwnd, Longint lpvideo )
{
static BITMAPINFO Bitmap_Info;
static BITMAPINFOHEADER Bitmap_Info_Head;
static BITMAPFILEHEADER Bitmap_File_Head;
CAPSTATUS status;
int BIHsize;
Byte *ptr,*ptr1; //Graphics::TBitmap *Bmp = new Graphics::TBitmap();
VideoStr=LPVIDEOHDR(lpvideo);
capGetStatus( GetCapWnd, &status, sizeof(status) ) ;
BIHsize=capGetVideoFormatSize( GetCapWnd );
capGetVideoFormat( GetCapWnd, &Bitmap_Info_Head, BIHsize);
Bitmap_Info.bmiHeader=Bitmap_Info_Head;
stream->Size=sizeof(Bitmap_File_Head) sizeof(Bitmap_Info_Head) Bitmap_Info_Head.biSizeImage;
Bitmap_File_Head.bfType=0x4D42;
Bitmap_File_Head.bfSize=stream->Size;
Bitmap_File_Head.bfOffBits=sizeof(Bitmap_File_Head) sizeof(Bitmap_Info_Head);
//stream->Clear();
stream->Position=0;
stream->WriteBuffer( &Bitmap_File_Head, sizeof(Bitmap_File_Head) );
stream->WriteBuffer( &Bitmap_Info, sizeof(Bitmap_Info) );
(void*)ptr=stream->Memory;
ptr =ptr Bitmap_File_Head.bfOffBits;
Move( VideoStr->lpData, ptr, Bitmap_Info_Head.biSizeImage);
//stream->Position=0; //歸0
//Bmp->LoadFromStream(stream);
//Form1->Image1->Picture->Bitmap=Bmp;
return(0);
} //---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
GetCapWnd = capCreateCaptureWindow ( "",WS_CHILD | WS_VISIBLE ,0,0,Panel1->Width,Panel1->Height,Panel1->Handle, 0);
bool ccd_ready = false;
ccd_ready=capDriverConnect(GetCapWnd,0);
if( !ccd_ready )
{
ShowMessage("攝影機連接失敗..");
return;
}
capPreviewRate(GetCapWnd,50);
capPreview(GetCapWnd,true);
capSetCallbackOnFrame (GetCapWnd, &GetVideo); }
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
capDlgVideoSource(GetCapWnd);
capDlgVideoFormat(GetCapWnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N4Click(TObject *Sender)
{
capDriverDisconnect(GetCapWnd);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
AnsiString szCommand=AThread->Connection->ReadLn(EOL,300,1000);
if(szCommand=="ServerToClient")
{
AThread->Connection->WriteStream(stream, true, true, 0);
//stream->Size=0;
}
else if(szCommand=="Stop")
{
capDriverDisconnect(GetCapWnd);
IdTCPServer1->Active=false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="IP:" IdIPWatch1->LocalIP() ; //IdIPWatch1元件
StatusBar1->Panels->Items[1]->Text="Port:" IntToStr(IdTCPServer1->DefaultPort);
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
IdTCPServer1->Active=false;
}
//--------------------------------------------------------------------------- Client 端: //--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma resource "*.dfm"
TForm1 *Form1;
TMemoryStream *ms1=new TMemoryStream; //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
IdTCPClient1->Host=Edit1->Text;
IdTCPClient1->Port=StrToInt(Edit2->Text);
IdTCPClient1->Connect(3000);
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(IdTCPClient1->Connected())
{ Graphics::TBitmap *bmp1=new Graphics::TBitmap; IdTCPClient1->WriteLn("ServerToClient");
IdTCPClient1->ReadStream(ms1, -1, false);
ms1->Position=0;
bmp1->LoadFromStream(ms1);
Image1->Picture->Bitmap=bmp1;
delete bmp1;
ms1->Clear();
} }
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
Timer1->Enabled=false;
IdTCPClient1->WriteLn("Stop");
IdTCPClient1->Disconnect();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Image1->Canvas->Brush->Style=bsSolid;
Image1->Canvas->Brush->Color=clBlack;
Image1->Canvas->Rectangle(0,0,160,120);
}
//---------------------------------------------------------------------------