線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2560
推到 Plurk!
推到 Facebook!

如何把視訊影像存入buff

答題得分者是:taishyang
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-01-31 23:07:21 IP:218.169.xxx.xxx 訂閱
各位大大好:

這是我參考板上資源改出的簡易的 取得視訊的 程式

我看他也是用BitmapA->Assign(Image1->Picture->Bitmap);來存取

可是我一直沒有辦法正確的把影像存到 buff 裡面做一個簡單的灰階運算

請各位大大賜教了


[code cpp]
//---------------------------------------------------------------------------

#include
#pragma hdrstop
#include <math.h><br />#include "Unit1.h"
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
long color;
const int w=600,h=480;
int i,j;
Byte *ptra=new Byte[w];
RGBQUAD *PRGB=new RGBQUAD[w*h];
double buffg[w][h],R[w][h],G[w][h],B[w][h],r,g,b;
HWND vc1,hCapWnd;
LPVIDEOHDR VideoStr; //宣告圖片資料
LRESULT CALLBACK FrameCallBack(HWND,Longint); //宣告callbackonframe函式
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{


BitmapA = new Graphics::TBitmap();

BitmapB = new Graphics::TBitmap();

BitmapE = new Graphics::TBitmap();


}
//---------------------------------------------------------------------------
LRESULT CALLBACK FrameCallBack( HWND hwnd, Longint lpvhdr )
{
static BITMAPINFOHEADER BitmapHead;
static BITMAPINFO BitmapInfo;
static BITMAPFILEHEADER BitmapFileHead;

CAPSTATUS status;
int BIHsize, byte;
Byte *ptr;
TMemoryStream *stream = new TMemoryStream;
// 取得圖片資料
VideoStr=LPVIDEOHDR(lpvhdr);
// 取得CAP訊息
capGetStatus( hCapWnd, &status, sizeof(status) ) ;
// 取得圖片格式容量大小
BIHsize=capGetVideoFormatSize( hCapWnd );
// 取得圖片格式 代入 bitmapinfohead 內
capGetVideoFormat( hCapWnd, &BitmapHead, BIHsize);
// 設定 BITMAPINFO
BitmapInfo.bmiHeader=BitmapHead;

stream->Size=sizeof(BitmapFileHead) sizeof(BitmapHead)-2 BitmapHead.biSizeImage;//減2

// 設定 BITMAPFILEHEAD
BitmapFileHead.bfType=0x4D42; // 總是 BM
BitmapFileHead.bfSize=stream->Size; //該圖檔總大小
BitmapFileHead.bfReserved1=(WORD)0;
BitmapFileHead.bfReserved2=(WORD)0;
BitmapFileHead.bfOffBits=sizeof(BitmapFileHead)-2 sizeof(BitmapHead); // 偏移至imagedata大小//減2

// 將資料存入 memorystream
stream->Position=0;
stream->WriteBuffer( &BitmapFileHead.bfType, sizeof(BitmapFileHead.bfType) );
stream->WriteBuffer( &BitmapFileHead.bfSize, sizeof(BitmapFileHead.bfSize) );
stream->WriteBuffer( &BitmapFileHead.bfReserved1, sizeof(BitmapFileHead.bfReserved1) );
stream->WriteBuffer( &BitmapFileHead.bfReserved2, sizeof(BitmapFileHead.bfReserved2) );
stream->WriteBuffer( &BitmapFileHead.bfOffBits, sizeof(BitmapFileHead.bfOffBits) );
stream->WriteBuffer( &BitmapInfo, sizeof(BitmapInfo) );

(void*)ptr=stream->Memory;
ptr = BitmapFileHead.bfOffBits;
Move( VideoStr->lpData, ptr, BitmapHead.biSizeImage);
// 存入完成
stream->Position=0;

Form1->Image1->Picture->Bitmap->LoadFromStream( stream );

delete( stream );
capSetCallbackOnFrame (hCapWnd, NULL); // 停止callbackonframe
return(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer(TObject *Sender)
{
hCapWnd=capCreateCaptureWindow ( "Capture Window", WS_CHILD | WS_VISIBLE ,0,0,Panel1->Width,Panel1->Height,Panel1->Handle,0);//設定 CaptureWindows
capDriverConnect(hCapWnd,0);//呼叫視訊裝置 0 是編號
//capOverlay(hCapWnd,true);
capPreview(hCapWnd,true);
//capPreviewScale(hCapWnd,true);
capPreviewRate(hCapWnd,1000/30);//擷取影像延遲時間

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{

BitmapB->Assign(BitmapA);
BitmapA->Assign(Image1->Picture->Bitmap);


for(j=0;jHeight;j )
{
ptra = (Byte *)BitmapA->ScanLine[j];

for(i=0;iWidth;i )
{
(PRGB)[i j*w].rgbRed=GetRValue(ptra[i]);
(PRGB)[i j*w].rgbGreen=GetGValue(ptra[i]);
(PRGB)[i j*w].rgbBlue=GetBValue(ptra[i]);
buffg[i][j]=(double)(0.299*(PRGB)[i j*w].rgbRed 0.587*(PRGB)[i j*w].rgbGreen 0.114*(PRGB)[i j*w].rgbBlue 0.5);
}
Image1->Picture->Assign(BitmapA);
}

for(j=0;j {
for(i=0;i {
Form1->Image1->Canvas->Pixels[i][j]=TColor(RGB(buffg[i][j],buffg[i][j],buffg[i][j]));
}
}


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

Timer1->Interval=5000;
Timer1->Enabled=true;

}
//---------------------------------------------------------------------------




[/code]

站上資源

[code xml]
http://delphi.ktop.com.tw/board.php?cid=168&fid=921&tid=22263
[/code]
------
恩...
編輯記錄
istillloving 重新編輯於 2009-01-31 23:09:43, 註解 無‧
istillloving 重新編輯於 2009-02-18 00:01:06, 註解 無‧
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-02-05 19:49:44 IP:218.172.xxx.xxx 訂閱
我除錯了一下

發現是錯在57行

Form1->Image1->Picture->Bitmap->LoadFromStream( stream );

只要在Timer1使用capSetCallbackOnFrame (hCapWnd, &FrameCallBack); 來呼叫 FrameCallBack

就會出現 "Bitmap image is not valid" 的錯誤 是不是我格式設錯了呢??
------
恩...
編輯記錄
istillloving 重新編輯於 2009-02-05 19:52:36, 註解 無‧
istillloving 重新編輯於 2009-02-05 19:53:42, 註解 無‧
istillloving 重新編輯於 2009-02-05 19:54:48, 註解 無‧
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-02-05 20:41:11 IP:118.169.xxx.xxx 訂閱
有沒有用過[Bitmap image is not valid]當關鍵字搜尋?
istillloving
高階會員


發表:33
回覆:182
積分:183
註冊:2008-10-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-02-07 07:18:03 IP:220.129.xxx.xxx 訂閱
您好:

我想大概是格式出錯了

那我請問一下能不能直接用開(*.raw)檔的方式直接開串流阿??
------
恩...
系統時間:2024-04-17 3:58:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!