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

(DirectShow) 如何擷取Camera影像到Image元件

答題得分者是:danny
psp67101
一般會員


發表:5
回覆:10
積分:3
註冊:2005-05-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-09 09:18:52 IP:220.128.xxx.xxx 訂閱

[code cpp]
// Step 1: 首先要先 initial COM
CoInitialize(NULL);

// Step 2: 呼叫初始化函式幫我們建立 Graph Manager 與 Build help Ojbect
//CaptureGraphBuilder2 pBuild 來幫我們管理 Graph Filter
//IGraphBuilder **ppGraph
InitCaptureGraphBuilder(&pGraph, &pBuild);

// Step 3: 取得 系統中的 Video Capture Device
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap,L"Capture Filter");

// Step 4: 要求 Graph Builder 幫我們建立適當的 graph filter 圖形
HRESULT hr;
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW,
NULL,
pCap,
NULL,
VideoFilter);


hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pCap,
NULL,
CaptureFilter);
hr = pBuild->RenderStream(&PIN_CATEGORY_VIDEOPORT,
&MEDIATYPE_Video,
pCap,
NULL,
VideoFilter);

// Step 5: 建立控制器用來執行我們的 graph filter
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);

pControl->Run();
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
問題2.若想要變動其解析度 要怎麼設定呢

[/code]
上面這段程式,可以自動產生一個視窗看到Camera的影像
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-09 10:05:10 IP:203.79.xxx.xxx 訂閱
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
看你的 code 應該是使用 DSPACK 吧 ?
DSPACK 的 Demos 中的 SampleGrabber 就是你要的

問題2.若想要變動其解析度 要怎麼設定呢
請參考這篇
http://delphi.ktop.com.tw/board.php?cid=31&fid=77&tid=34955

===================引 用 psp67101 文 章===================

[code cpp]
// Step 1: 首先要先 initial COM
CoInitialize(NULL);

// Step 2: 呼叫初始化函式幫我們建立 Graph Manager 與 Build help Ojbect
//CaptureGraphBuilder2 pBuild 來幫我們管理 Graph Filter
//IGraphBuilder **ppGraph
InitCaptureGraphBuilder(&pGraph, &pBuild);

// Step 3: 取得 系統中的 Video Capture Device
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap,L"Capture Filter");

// Step 4: 要求 Graph Builder 幫我們建立適當的 graph filter 圖形
HRESULT hr;
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW,
NULL,
pCap,
NULL,
VideoFilter);


hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pCap,
NULL,
CaptureFilter);
hr = pBuild->RenderStream(&PIN_CATEGORY_VIDEOPORT,
&MEDIATYPE_Video,
pCap,
NULL,
VideoFilter);

// Step 5: 建立控制器用來執行我們的 graph filter
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);

pControl->Run();
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
問題2.若想要變動其解析度 要怎麼設定呢

[/code]
上面這段程式,可以自動產生一個視窗看到Camera的影像
------
將問題盡快結案也是一種禮貌!
psp67101
一般會員


發表:5
回覆:10
積分:3
註冊:2005-05-06

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-11-09 10:27:15 IP:220.128.xxx.xxx 訂閱

===================引 用 danny 文 章===================
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
看你的 code 應該是使用 DSPACK 吧 ?
DSPACK 的 Demos 中的 SampleGrabber 就是你要的

我是用DSPACK,但是Demos的SampleGrabber不是用BCB寫的,似乎是用安裝的元件抓取的,
我不想用安裝元件來抓(也就是不用
SampleGrabber1->GetBitmap(Bitmap) 來抓);
而是用ISampleGrabber 宣告pSampleGrabber來抓影像放到Bitmap的話,要如何做呢?
有簡單的範例給我參考嗎?


問題2.若想要變動其解析度 要怎麼設定呢
請參考這篇
http://delphi.ktop.com.tw/board.php?cid=31&fid=77&tid=34955

===================引 用 psp67101 文 章===================

[code cpp]
// Step 1: 首先要先 initial COM
CoInitialize(NULL);

// Step 2: 呼叫初始化函式幫我們建立 Graph Manager 與 Build help Ojbect
//CaptureGraphBuilder2 pBuild 來幫我們管理 Graph Filter
//IGraphBuilder **ppGraph
InitCaptureGraphBuilder(&pGraph, &pBuild);

// Step 3: 取得 系統中的 Video Capture Device
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap,L"Capture Filter");

// Step 4: 要求 Graph Builder 幫我們建立適當的 graph filter 圖形
HRESULT hr;
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW,
NULL,
pCap,
NULL,
VideoFilter);


hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pCap,
NULL,
CaptureFilter);
hr = pBuild->RenderStream(&PIN_CATEGORY_VIDEOPORT,
&MEDIATYPE_Video,
pCap,
NULL,
VideoFilter);

// Step 5: 建立控制器用來執行我們的 graph filter
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);

pControl->Run();
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
問題2.若想要變動其解析度 要怎麼設定呢

[/code]
上面這段程式,可以自動產生一個視窗看到Camera的影像
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-11-09 11:10:13 IP:203.79.xxx.xxx 訂閱
你是用DSPACK但不想用安裝元件來抓 ? DSPACK不就是要安裝的元件嗎 ?
請參考這篇 使用DirectShow 將影片內容轉成圖檔 應該是你要的

===================引 用 psp67101 文 章===================

===================引 用 danny 文 章===================
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
看你的 code 應該是使用 DSPACK 吧 ?
DSPACK 的 Demos 中的 SampleGrabber 就是你要的

我是用DSPACK,但是Demos的SampleGrabber不是用BCB寫的,似乎是用安裝的元件抓取的,
我不想用安裝元件來抓(也就是不用
SampleGrabber1->GetBitmap(Bitmap) 來抓);
而是用ISampleGrabber 宣告pSampleGrabber來抓影像放到Bitmap的話,要如何做呢?
有簡單的範例給我參考嗎?


問題2.若想要變動其解析度 要怎麼設定呢
請參考這篇
http://delphi.ktop.com.tw/board.php?cid=31&fid=77&tid=34955
------
將問題盡快結案也是一種禮貌!
編輯記錄
danny 重新編輯於 2007-11-09 11:13:11, 註解 無‧
psp67101
一般會員


發表:5
回覆:10
積分:3
註冊:2005-05-06

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-11-09 14:52:22 IP:220.128.xxx.xxx 訂閱

[code cpp]
// Step 1: 首先要先 initial COM
CoInitialize(NULL);

// Step 2: 呼叫初始化函式幫我們建立 Graph Manager 與 Build help Ojbect
//CaptureGraphBuilder2 pBuild 來幫我們管理 Graph Filter
InitCaptureGraphBuilder(&pGraph, &pBuild);

// Step 3: 取得 系統中的 Video Capture Device
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap,L"Capture Filter");

// Step 4: 要求 Graph Builder 幫我們建立適當的 graph filter 圖形
IBaseFilter *pMux;
hr = pBuild->SetOutputFileName(&MEDIASUBTYPE_Avi, // Specifies AVI for the target file.
L"D:\\2.avi", //File name.
&pMux, // Receives a pointer to the mux.
NULL); // (Optional) Receives a pointer to the file sink.

hr = pBuild->RenderStream( &PIN_CATEGORY_CAPTURE, // Pin category.
&MEDIATYPE_Video, // Media type.
pCap, // Capture filter.
NULL, // Intermediate filter (optional).
pMux); // Mux or file sink filter.
pMux->Release();

IConfigAviMux *pConfigMux = NULL;
hr = pCap->QueryInterface(IID_IConfigAviMux, (void**)&pConfigMux);
if (SUCCEEDED(hr)) {
pConfigMux->SetMasterStream(1);
pConfigMux->Release();
}

IBaseFilter *pEncoder; /* Create the encoder filter (not shown). */
pGraph->AddFilter(pEncoder, L"Encoder");
hr = pBuild->RenderStream( &PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pCap,
pEncoder,
pMux);
pEncoder->Release();

[/code]

很像還是不行,請問要改什麼嗎
hr = pBuild->SetOutputFileName(&MEDIASUBTYPE_Avi, // Specifies AVI for the target file.
L"D:\\2.avi", //File name.
&pMux, // Receives a pointer to the mux.
NULL); // (Optional) Receives a pointer to the file sink.
這個hr回傳是0,是那裡有問題嗎
編輯記錄
psp67101 重新編輯於 2007-11-09 15:02:50, 註解 無‧
yy680511
一般會員


發表:5
回覆:10
積分:13
註冊:2007-08-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-11-09 18:37:29 IP:61.64.xxx.xxx 訂閱
danny 大大
我的程度不好
若想要變動其解析度 <-----?

顯示放大縮小控制
只需改變Edit1的大小,然後使用IvideoWindow介面方法即可
VideoWindow.SetWindowPosition(0, 0, Edit1.Width, Edit1.Height);

這是你指的改變解析度嗎...? 好像只是顯示
請問如果是輸入的實際影像解析度要變動要怎樣改?


===================引 用 danny 文 章===================
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
看你的 code 應該是使用 DSPACK 吧 ?
DSPACK 的 Demos 中的 SampleGrabber 就是你要的

問題2.若想要變動其解析度 要怎麼設定呢
請參考這篇
http://delphi.ktop.com.tw/board.php?cid=31&fid=77&tid=34955
psp67101
一般會員


發表:5
回覆:10
積分:3
註冊:2005-05-06

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-11-13 17:41:50 IP:220.128.xxx.xxx 訂閱

[code cpp]

IGraphBuilder *pGraph;
ICaptureGraphBuilder2 *pBuild;
IBaseFilter *pCap;
IMediaControl *pMediaControl;
IBasicVideo *pBasicVideo;

CoInitialize(NULL);
InitCaptureGraphBuilder(&pGraph, &pBuild);
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap, L"Capture Filter");
pGraph->QueryInterface(IID_IMediaControl,(LPVOID *)&pMediaControl);

hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE, // Pin category.
&MEDIATYPE_Video, // Media type.
pCap, // Capture filter.
NULL, // Intermediate filter (optional).
NULL); // Mux or file sink filter.

pGraph->QueryInterface(IID_IBasicVideo,(LPVOID *)&pBasicVideo);
pMediaControl->Run();

// The image will be saved when OK is clicked
MessageBox( NULL, "Grab Image", "Grab", MB_OK);

pMediaControl->Pause();

// get width and height
long height, width;
pBasicVideo->get_VideoHeight(&height);
pBasicVideo->get_VideoWidth(&width);
long bufSize;
long *imgData;
HRESULT hr;
/*
The second value is NULL to resolve required buffer size.
The required buffer size will be returned in variable "bufSize".
*/
hr = pBasicVideo->GetCurrentImage(&bufSize, NULL);
if (FAILED(hr)) {
ShowMessage("GetCurrentImage failed\n");
}
if (bufSize < 1) {
ShowMessage("failed to get data size\n");
}
imgData = (long *)malloc(bufSize);
// The data will be in DIB format
pBasicVideo->GetCurrentImage(&bufSize, imgData);

HANDLE fh;
BITMAPFILEHEADER bmphdr;
BITMAPINFOHEADER bmpinfo;
DWORD nWritten;
memset(&bmphdr, 0, sizeof(bmphdr));
memset(&bmpinfo, 0, sizeof(bmpinfo));
bmphdr.bfType = ('M' << 8) | 'B';
bmphdr.bfSize = sizeof(bmphdr) sizeof(bmpinfo) bufSize;
bmphdr.bfOffBits = sizeof(bmphdr) sizeof(bmpinfo);
bmpinfo.biSize = sizeof(bmpinfo);
bmpinfo.biWidth = width;
bmpinfo.biHeight = height;
bmpinfo.biPlanes = 1;
bmpinfo.biBitCount = 32;
fh = CreateFile("result.bmp",
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
WriteFile(fh, &bmpinfo, sizeof(bmpinfo), &nWritten, NULL);
WriteFile(fh, imgData, bufSize, &nWritten, NULL);
CloseHandle(fh);
free(imgData);
// Release resource
pBasicVideo->Release();
pMediaControl->Release();
pGraph->Release(); // 對應 Graph: 對應整個 filter graph
pCap->Release(); // 對應 Capture Filter : 對應攝影機實體
pBuild->Release(); // 對應 Capture Graph Builder : helper 物件
CoUninitialize();
你好,對於擷取Camera影像,再存成影像或視訊,我還是試不出來,
我存的影像裡面還是沒東西,到底是那裡出錯,可以幫我改一下嗎

[/code]
chiachia
一般會員


發表:0
回覆:1
積分:0
註冊:2008-05-12

發送簡訊給我
#8 引用回覆 回覆 發表時間:2008-05-12 12:08:54 IP:140.127.xxx.xxx 訂閱

===================引 用 psp67101 文 章===================

[code cpp]
// Step 1: 首先要先 initial COM
CoInitialize(NULL);

// Step 2: 呼叫初始化函式幫我們建立 Graph Manager 與 Build help Ojbect
//CaptureGraphBuilder2 pBuild 來幫我們管理 Graph Filter
//IGraphBuilder **ppGraph
InitCaptureGraphBuilder(&pGraph, &pBuild);

// Step 3: 取得 系統中的 Video Capture Device
EnumerateVideoInputDevice(&pCap);
pGraph->AddFilter(pCap,L"Capture Filter");

// Step 4: 要求 Graph Builder 幫我們建立適當的 graph filter 圖形
HRESULT hr;
hr = pBuild->RenderStream(&PIN_CATEGORY_PREVIEW,
NULL,
pCap,
NULL,
VideoFilter);


hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video,
pCap,
NULL,
CaptureFilter);
hr = pBuild->RenderStream(&PIN_CATEGORY_VIDEOPORT,
&MEDIATYPE_Video,
pCap,
NULL,
VideoFilter);

// Step 5: 建立控制器用來執行我們的 graph filter
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);

pControl->Run();
問題1. 我想要將其影像讀入Image元件,要如何做?(用 ISampleGrabber宣告來做的話要如何使用)
問題2.若想要變動其解析度 要怎麼設定呢

[/code]
上面這段程式,可以自動產生一個視窗看到Camera的影像

不好意思,請問一下,您是有先include 什麼檔嗎?
最近開始初學,還請賜教,謝謝^_^
系統時間:2024-05-05 23:25:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!