我寫的視訊程式傳送範圍有偏差 |
缺席
|
0083chu
一般會員 發表:6 回覆:7 積分:2 註冊:2009-08-03 發送簡訊給我 |
我參考站上前輩的作品寫完這個程式,經過測試後發覺有Web Cam傳送端影像是沒什麼問題,在遠端接收的程式顯示區塊有偏差。
我在想會不會是傳送端再送的時候發生問題? 還有就是能不能把Web Cam擷取到的影像串流直接用WriteStream出去? (步驟較少) 我影像是用320x240,傳送端Ctrl V是開Web Cam,Ctrl C是關Web Cam,Ctrl S是輸入IP,Ctrl X是關閉 傳送端 [code cpp] //--------------------------------------------------------------------------- #include #include #include "jpeg.hpp" #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; HWND hwndCapture; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Panel1->DoubleBuffered=true; Image1->Width=320; Image1->Height=240; Image1->Left=(Panel1->Width-Image1->Width)/2; Image1->Top=(Panel1->Height-Image1->Height)/2; Image1->Canvas->Brush->Color=clWhite; Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height)); Image1->Stretch=true; //Height = 240; //Width = 320; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('V') & 0x8000)) { //HWND hwndCapture; hwndCapture=capCreateCaptureWindow((LPSTR) "Camera",WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | WS_BORDER | MCIWNDF_RECORD | MCIWNDF_SHOWALL ,0,0,320,240,(HWND) Form1->Panel1->Handle,(int) 1); // Try to connect to every driver int i; for(i=0;i<5;i ) if(capDriverConnect(hwndCapture,i)) break; if(i==10) { // Unable to connect to the capture ShowMessage("找不到攝影機"); exit(EXIT_FAILURE) ; } // To set preview rate to 33.3 miliseconds, or 30 FPS capPreviewRate(hwndCapture,33); // To set up priview funtion capPreview(hwndCapture,true); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('C') & 0x8000)) { capDriverDisconnect(hwndCapture); DestroyWindow(hwndCapture); //Close(); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('X') & 0x8000)) { IdTCPClient1->Disconnect(); Close(); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('S') & 0x8000)) { AnsiString Server; if (InputQuery("Computer to connect to" , "Address Name:" , Server)) { if (Server.Length() > 0) { IdTCPClient1->Host = Server; IdTCPClient1->Connect(3000); } } Timer1->Enabled = true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { Graphics::TBitmap *bmp1 = new Graphics::TBitmap; TMemoryStream *ms1=new TMemoryStream; try { HDC dc; TCanvas *cs=new TCanvas; dc=GetDC(0); cs->Handle=dc; bmp1->Width =Width; bmp1->Height=Height; //bmp1->Width =Screen->Width; //bmp1->Height=Screen->Height; bmp1->Canvas->CopyRect(Rect(0, 0, bmp1->Width, bmp1->Height), Canvas, Rect(0, 0, Width, Height)); //bmp1->Canvas->CopyRect(Rect(0, 0, bmp1->Width, bmp1->Height), cs, //Rect(0, 0, Screen->Width, Screen->Height)); //POINT pt; //HCURSOR hCur=GetCursor(); //GetCursorPos(&pt); //DrawIcon(bmp1->Canvas->Handle, pt.x, pt.y, hCur); TJPEGImage *jpg=new TJPEGImage; jpg->Assign(bmp1); jpg->CompressionQuality=90; jpg->Compress(); jpg->SaveToStream(ms1); delete jpg; delete cs; ReleaseDC(NULL, dc); IdTCPClient1->WriteStream(ms1, true, true, 0); } __finally { delete ms1; delete bmp1; } } //--------------------------------------------------------------------------- [/code] 接收端 [code cpp] //--------------------------------------------------------------------------- #include #include "jpeg.hpp" #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Panel1->DoubleBuffered=true; Image1->Width=320; Image1->Height=240; Image1->Left=(Panel1->Width-Image1->Width)/2; Image1->Top=(Panel1->Height-Image1->Height)/2; Image1->Canvas->Brush->Color=clWhite; Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height)); Image1->Stretch=true; IdTCPServer1->Active=true; } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPServer1Disconnect(TIdPeerThread *AThread) { Image1->Picture=NULL; Image1->Canvas->Brush->Color=clWhite; Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height)); } //--------------------------------------------------------------------------- void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread) { TMemoryStream *ms1=new TMemoryStream; try { AThread->Connection->ReadStream(ms1, -1, false); ms1->Position=0; TJPEGImage *jpg=new TJPEGImage; jpg->LoadFromStream(ms1); jpg->CompressionQuality=50; Image1->Picture->Assign(jpg); delete jpg; } __finally { delete ms1; } } //--------------------------------------------------------------------------- [/code] 我忘了要終止問題了.......=.= 修正過後的傳送端 [code cpp] //--------------------------------------------------------------------------- #include #include #include "jpeg.hpp" #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; HWND hwndCapture; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Panel1->DoubleBuffered=true; Image1->Width=320; Image1->Height=240; Image1->Left=(Panel1->Width-Image1->Width)/2; Image1->Top=(Panel1->Height-Image1->Height)/2; Image1->Canvas->Brush->Color=clWhite; Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height)); Image1->Stretch=true; //Height = 240; //Width = 320; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('V') & 0x8000)) { //HWND hwndCapture; hwndCapture=capCreateCaptureWindow((LPSTR) "Camera",WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | WS_BORDER | MCIWNDF_RECORD | MCIWNDF_SHOWALL ,0,0,320,240,(HWND) Form1->Panel1->Handle,(int) 1); // Try to connect to every driver int i; for(i=0;i<5;i ) if(capDriverConnect(hwndCapture,i)) break; if(i==10) { // Unable to connect to the capture ShowMessage("找不到攝影機"); exit(EXIT_FAILURE) ; } // To set preview rate to 33.3 miliseconds, or 30 FPS capPreviewRate(hwndCapture,33); // To set up priview funtion capPreview(hwndCapture,true); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('C') & 0x8000)) { capDriverDisconnect(hwndCapture); DestroyWindow(hwndCapture); //Close(); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('X') & 0x8000)) { IdTCPClient1->Disconnect(); Close(); } if((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState('S') & 0x8000)) { AnsiString Server; if (InputQuery("Computer to connect to" , "Address Name:" , Server)) { if (Server.Length() > 0) { IdTCPClient1->Host = Server; IdTCPClient1->Connect(3000); } } Timer1->Enabled = true; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { Graphics::TBitmap *bmp1 = new Graphics::TBitmap; TMemoryStream *ms1=new TMemoryStream; try { HDC dc; TCanvas *cs=new TCanvas; dc=GetDC(0); cs->Handle=dc; bmp1->Width =Width; bmp1->Height=Height; //要把 Width, Height 改成 Panel1->Width, Panel1->Height //應該是我的起點標錯了,不管我怎麼加減多少都有偏差 bmp1->Canvas->CopyRect(Rect(0, 0, bmp1->Width, bmp1->Height), Canvas, Rect(0, 0, Panel1->Width, Panel1->Height)); TJPEGImage *jpg=new TJPEGImage; jpg->Assign(bmp1); jpg->CompressionQuality=90; jpg->Compress(); jpg->SaveToStream(ms1); delete jpg; delete cs; ReleaseDC(NULL, dc); IdTCPClient1->WriteStream(ms1, true, true, 0); } __finally { delete ms1; delete bmp1; } } //--------------------------------------------------------------------------- [/code]
------
CHU 編輯記錄
0083chu 重新編輯於 2010-04-13 17:53:56, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |