?? usb攝像頭編程.txt
字號:
注意包含頭文件qedit.h,dshow.h
幾個函數如下,其中有一些全局變量,程序寫的充忙,也就不整理了。參考了一些資料。
BOOL initVideo(void)
{
AM_MEDIA_TYPE amt;
CoInitialize(NULL); // COM的初期化
// ---- 輸入計算的準備 ----
// 搜索輸入裝置
IBaseFilter *pbf = NULL;
IMoniker * pMoniker = NULL;
ULONG cFetched;
// 制作裝置的各個部分
CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void ** ) &pDevEnum);
// 制作視頻輸入裝置的各個部分
IEnumMoniker * pClassEnum = NULL;
pDevEnum -> CreateClassEnumerator(
CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
if (pClassEnum == NULL){
AfxMessageBox("No video device, program exit!");
pDevEnum -> Release();
CoUninitialize();
//OnOK();
return FALSE ;
}
// 取得最初發現的視頻輸入裝置object的接口
pClassEnum -> Next(1, &pMoniker, &cFetched);
pMoniker->BindToObject( 0, 0, IID_IBaseFilter, (void**)&pbf );
// ---- 過慮圖像的準備 ----
// 制作過慮圖像,取得接口
CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IGraphBuilder, (void **) &pGraph);
pGraph -> QueryInterface( IID_IMediaControl, (LPVOID *) &pMC );
// 把輸入圖像追加到過慮圖像
pGraph -> AddFilter( pbf, L"Video Capture");
// 因為進行了追加所以解除參照的輸入圖像
pbf -> Release();
// ---- 過慮圖像的準備 ----
// 制作過慮圖像,取得接口
CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (LPVOID *)&pF);
pF -> QueryInterface( IID_ISampleGrabber, (void **)&pGrab );
// 把輸入圖像追加到過慮圖像
ZeroMemory(&amt, sizeof(AM_MEDIA_TYPE));
amt.majortype = MEDIATYPE_Video;
amt.subtype = MEDIASUBTYPE_RGB24;
amt.formattype = FORMAT_VideoInfo;
pGrab -> SetMediaType( &amt );
// 把grubber過慮追加到過慮圖像
pGraph -> AddFilter(pF, L"SamGra");
// ---- 輸入圖像的準備 ----
// 制作輸入圖像
CoCreateInstance( CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
IID_ICaptureGraphBuilder2, (void **) &pCapture );
// 把過慮圖像編入到輸入圖像
pCapture -> SetFiltergraph( pGraph );
// 輸入圖像的設定,設定grubber為rendering輸出
pCapture -> RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
pbf, NULL, pF);
// ---- 表示窗口的準備 ----
// 位圖信息的取得
pGrab -> GetConnectedMediaType( &amt );
// 獲得視頻頭部的信息
VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)amt.pbFormat;
// 在視頻的頭部包含位圖的信息
// 把位圖的信息復制到BITMAPINFO的結構體中
BITMAPINFO BitmapInfo;
ZeroMemory( &BitmapInfo, sizeof(BitmapInfo) );
CopyMemory( &BitmapInfo.bmiHeader, &(pVideoHeader->bmiHeader),
sizeof(BITMAPINFOHEADER));
img00.bih = BitmapInfo.bmiHeader;
long n = img00.bih.biSizeImage;
char s[100];
sprintf(s, "Video Width: %ld Video Height: %ld", img00.bih.biWidth, img00.bih.biHeight);
AfxMessageBox(s);
img00.lpBmpData = (BYTE *)malloc( n );
// ************* 追加 *****************
if (img00.lpBmpData == NULL) {
AfxMessageBox("Insufficient Memory!");
}
img00.hi = (HINSTANCE)GetWindowLong( HWND_DESKTOP, GWL_HINSTANCE );
return true;
}
void initGraphic(void)
{
// 把位圖的信息復制到BITMAPINFO的結構體中
BITMAPINFO BitmapInfo;
ZeroMemory( &BitmapInfo, sizeof(BitmapInfo) );
CopyMemory( &BitmapInfo.bmiHeader, &img00.bih,
sizeof(BITMAPINFOHEADER));
img01.bih = BitmapInfo.bmiHeader;
long n = img01.bih.biSizeImage;
img01.lpBmpData = (BYTE *)malloc( n );
// **************** 追加 ******************
if (img01.lpBmpData == NULL) {
printf("Insufficient memory available (img01.lpBmpData)\n");
}
img01.hi = (HINSTANCE)GetWindowLong( HWND_DESKTOP, GWL_HINSTANCE );
}
void closeVideo(void)
{
// 接口的解除
pMC -> Release();
pDevEnum -> Release();
pGraph -> Release();
pCapture -> Release();
CoUninitialize();
free( img00.lpBmpData );
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -