在图像处理以及人脸识别时经常会用到从摄像头读取图像。OPENCV有提供的方法来实现,非常简单,不用多说。而使用VC++则没有那么容易,下面介绍使用CImage和DirectShow读取摄像头图像,并显示的对话框中。
我用的开发工具是VS2010。
源代码下载,使用VS2010编译通过。
http://download.csdn.net/detail/sdlypyzq/4087013
一、创建一个MFC对话框程序,工程起名为CameraVCTest。
二、删除无用的控件和按钮。添加一个图片控件,ID为IDC_PICTURE,并为其添加CStatic
类型变量m_picture。添加四个按钮,名称分别为预览,拍照,保存,关闭。ID分别为IDC_VIEW,IDC_TAKEPHOTO,IDC_SAVE,IDC_CLOSE。分别为其添加变量m_view,m_takePhoto,m_save,m_close。双击四个按钮,生成四个响应函数。
三、将DirectShow文件夹和CameraDS类的头文件和源文件拷贝到项目源文件夹下。并在项目属性VC++ Directories 添加include Directories,“.\Directory”。在Solution Exlporer添加上CameraDS类的头文件和源文件。
四、在CCameraVCTestDlg 类的头文件中,添加
#include "CameraDS.h" #include <vector> using namespace std;
|
添加公共变量和方法
vector<CCameraDS> cameralist; CImage *m_pImage; void ShowPicture(CImage *img,CStatic *pic); void SaveCard(CImage *img); int m_cam_count; bool isBreak;
CString m_savePath; CString savePath; int m_saveIndex; int m_fileCount;
|
在源文件的初始化函数里添加:
m_cam_count = CCameraDS::CameraCount(); cameralist.resize(m_cam_count); if(! cameralist[m_cam_count-1].OpenCamera(m_cam_count-1, false, 640,480)) { return FALSE; } this->isBreak = false; this->isSave = false; this->m_view.EnableWindow(TRUE); this->m_takePhoto.EnableWindow(FALSE); this->m_save.EnableWindow(FALSE);
this->m_savePath = L".\\原-始º?照?片?\\"; this->m_saveIndex = 1001; this->m_fileCount = 0;
|
添加如下函数
void DoEvents1() { MSG msg; if (::PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } }
void CCameraVCTestDlg::OnBnClickedView() { // TODO: Add your control notification handler code here
this->isBreak = false; this->m_view.EnableWindow(FALSE); this->m_takePhoto.EnableWindow(TRUE); this->m_save.EnableWindow(FALSE); if(m_cam_count==0) { return ; } //获?取¨?一°?帧? //CImage imgShow; //imgShow.Create(this->MAX_WIDTH,this->MAX_HEIGHT,24); while(1) { DoEvents1(); if(isBreak) { break; }
this->m_pImage = cameralist[m_cam_count-1].QueryFrame2(); //光a斑ã?补1偿£¤平?衡a //this->oper.RepairFaculaRGBImage(*m_pImage,this->m_imgBk,imgShow); //this->ShowPicture(&imgShow,&this->m_picture); this->ShowPicture(m_pImage,&this->m_picture);
//if(m_fileCount == 100) //{ // this->isBreak = true; // this->m_bnInit.EnableWindow(TRUE); // this->m_bnTake.EnableWindow(FALSE); // this->m_bnSave.EnableWindow(TRUE); // break; //} if(isSave) { if(this->m_fileCount<100) { //this->m_ctrlProg.SetPos(m_fileCount); CString path; path.Format(L"%s\\%d.jpg",savePath,m_fileCount+m_saveIndex); this->m_pImage->Save(path); m_fileCount++; } else { isSave = false; this->m_view.EnableWindow(FALSE); this->m_takePhoto.EnableWindow(TRUE); //this->m_save.EnableWindow(TRUE); }
} Sleep(150); } }
|
添加视频帧显示函数
void CCameraVCTestDlg::ShowPicture(CImage *img,CStatic *pic) { if(img==NULL) { return ; } ///////???????????? int width = img->GetWidth(); int height = img->GetHeight(); CRect picRect; this->m_picture.GetClientRect(&picRect); CRect rt(picRect); CDC* dc = this->m_picture.GetDC();
// if(this->isSaveCard == 1) // { CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)); //dc->FillRect(rt, pBrush);
if(picRect.Height()*width > height*picRect.Width()) { CPoint p1(0,(picRect.Height()-(picRect.Width()*height/width))/2); CPoint p2(picRect.Width(),(picRect.Height() - p1.y)); rt.SetRect(p1,p2); } else { CPoint p1((picRect.Width()-(picRect.Height()*width/height))/2,0); CPoint p2(picRect.Width()-p1.x,picRect.Height()); rt.SetRect(p1,p2); } // }
// this->ShowMouseCursor(CursorTag); ::SetStretchBltMode(dc->m_hDC,HALFTONE); img->Draw(dc->m_hDC, rt); }
|
添加关闭程序
void CCameraVCTestDlg::OnBnClickedClose() { // TODO: Add your control notification handler code here //结¨¢束º?从䨮摄¦?像?头ª¡¤中D读¨¢取¨?视º¨®频¦Ì,ê?关?闭À?程¨¬序¨° this->isBreak = true; this->OnClose(); }
|
好了,现在对话框上,点击预览按钮,就可以打开摄像头了,若还需要什么功能自己添加吧,如有问题,可以一起交流。欢迎不吝赐教。
Camera.h
#ifndef POINTER_64 #if !defined(_MAC) && (defined(_M_MRX000) || defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_VER >= 1100) && !(defined(MIDL_PASS) || defined(RC_INVOKED)) #define POINTER_64 __ptr64 typedef unsigned __int64 POINTER_64_INT; #if defined(_WIN64) #define POINTER_32 __ptr32 #else #define POINTER_32 #endif #else #if defined(_MAC) && defined(_MAC_INT_64) #define POINTER_64 __ptr64 typedef unsigned __int64 POINTER_64_INT; #else #if (_MSC_VER >= 1300) && !(defined(MIDL_PASS) || defined(RC_INVOKED)) #define POINTER_64 __ptr64 #else #define POINTER_64 #endif typedef unsigned long POINTER_64_INT; #endif #define POINTER_32 #endif #endif #ifndef CCAMERA_H #define CCAMERA_H #define WIN32_LEAN_AND_MEAN #include <atlbase.h> #include "qedit.h" #include "dshow.h" #include <windows.h> //#include "cv.h" //#include <cxcore.h> #define MYFREEMEDIATYPE(mt) {if ((mt).cbFormat != 0) \ {CoTaskMemFree((PVOID)(mt).pbFormat); \ (mt).cbFormat =0; \ (mt).pbFormat = NULL; \ } \ if ((mt).pUnk != NULL) \ { \ (mt).pUnk->Release(); \ (mt).pUnk = NULL; \ }} class CCameraDS { private: // IplImage * m_pFrame; CImage m_image; bool m_bConnected; int m_nWidth; int m_nHeight; bool m_bLock; bool m_bChanged; long m_nBufferSize; CComPtr<IGraphBuilder> m_pGraph; CComPtr<IBaseFilter> m_pDeviceFilter; CComPtr<IMediaControl> m_pMediaControl; CComPtr<IBaseFilter> m_pSampleGrabberFilter; CComPtr<ISampleGrabber> m_pSampleGrabber; CComPtr<IPin> m_pGrabberInput; CComPtr<IPin> m_pGrabberOutput; CComPtr<IPin> m_pCameraOutput; CComPtr<IMediaEvent> m_pMediaEvent; CComPtr<IBaseFilter> m_pNullFilter; CComPtr<IPin> m_pNullInputPin; private: bool BindFilter(int nCamIDX, IBaseFilter **pFilter); void SetCrossBar(); public: CCameraDS(); virtual~CCameraDS(); //打开摄像头,nCamID指定打开哪个摄像头,取值可以为0,1,2,... //bDisplayProperties指示是否自动弹出摄像头属性页 //nWidth和nHeight设置的摄像头的宽和高,如果摄像头不支持所设定的宽度和高度,则返回false bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties=true, int nWidth =320, int nHeight =240); //关闭摄像头,析构函数会自动调用这个函数 void CloseCamera(); //返回摄像头的数目 //可以不用创建CCameraDS实例,采用int c=CCameraDS::CameraCount();得到结果。 staticint CameraCount(); //根据摄像头的编号返回摄像头的名字 //nCamID: 摄像头编号 //sName: 用于存放摄像头名字的数组 //nBufferSize: sName的大小 //可以不用创建CCameraDS实例,采用CCameraDS::CameraName();得到结果。 staticint CCameraDS::CameraName(int nCamID, char* sName, int nBufferSize); //返回图像宽度 int GetWidth(){return m_nWidth;} //返回图像高度 int GetHeight(){return m_nHeight;} //抓取一帧,返回的IplImage不可手动释放! //返回图像数据的为RGB模式的Top-down(第一个字节为左上角像素),即IplImage::origin=0(IPL_ORIGIN_TL) // IplImage * QueryFrame(); CImage* QueryFrame2(); }; #endif
#include "stdafx.h" #include "CameraDS.h" #pragma comment(lib,"Strmiids.lib") ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCameraDS::CCameraDS() { m_bConnected =false; m_nWidth =0; m_nHeight =0; m_bLock =false; m_bChanged =false; // m_pFrame = NULL; m_nBufferSize =0; m_pNullFilter = NULL; m_pMediaEvent = NULL; m_pSampleGrabberFilter = NULL; m_pGraph = NULL; CoInitialize(NULL); } CCameraDS::~CCameraDS() { CloseCamera(); CoUninitialize(); } void CCameraDS::CloseCamera() { if(m_bConnected) m_pMediaControl->Stop(); m_pGraph = NULL; m_pDeviceFilter = NULL; m_pMediaControl = NULL; m_pSampleGrabberFilter = NULL; m_pSampleGrabber = NULL; m_pGrabberInput = NULL; m_pGrabberOutput = NULL; m_pCameraOutput = NULL; m_pMediaEvent = NULL; m_pNullFilter = NULL; m_pNullInputPin = NULL; //if (m_pFrame) // cvReleaseImage(&m_pFrame); if(!this->m_image.IsNull()) { this->m_image.Destroy(); } m_bConnected =false; m_nWidth =0; m_nHeight =0; m_bLock =false; m_bChanged =false; m_nBufferSize =0; } bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties, int nWidth, int nHeight) { HRESULT hr = S_OK; CoInitialize(NULL); // Create the Filter Graph Manager. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph); hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&m_pSampleGrabberFilter); hr = m_pGraph->QueryInterface(IID_IMediaControl, (void**) &m_pMediaControl); hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void**) &m_pMediaEvent); hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID*) &m_pNullFilter); hr = m_pGraph->AddFilter(m_pNullFilter, L"NullRenderer"); hr = m_pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (void**)&m_pSampleGrabber); AM_MEDIA_TYPE mt; ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE)); mt.majortype = MEDIATYPE_Video; mt.subtype = MEDIASUBTYPE_RGB24; mt.formattype = FORMAT_VideoInfo; hr = m_pSampleGrabber->SetMediaType(&mt); MYFREEMEDIATYPE(mt); m_pGraph->AddFilter(m_pSampleGrabberFilter, L"Grabber"); // Bind Device Filter. We know the device because the id was passed in BindFilter(nCamID, &m_pDeviceFilter); m_pGraph->AddFilter(m_pDeviceFilter, NULL); CComPtr<IEnumPins> pEnum; m_pDeviceFilter->EnumPins(&pEnum); hr = pEnum->Reset(); hr = pEnum->Next(1, &m_pCameraOutput, NULL); pEnum = NULL; m_pSampleGrabberFilter->EnumPins(&pEnum); pEnum->Reset(); hr = pEnum->Next(1, &m_pGrabberInput, NULL); pEnum = NULL; m_pSampleGrabberFilter->EnumPins(&pEnum); pEnum->Reset(); pEnum->Skip(1); hr = pEnum->Next(1, &m_pGrabberOutput, NULL); pEnum = NULL; m_pNullFilter->EnumPins(&pEnum); pEnum->Reset(); hr = pEnum->Next(1, &m_pNullInputPin, NULL); //SetCrossBar(); if (bDisplayProperties) { CComPtr<ISpecifyPropertyPages> pPages; HRESULT hr = m_pCameraOutput->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pPages); if (SUCCEEDED(hr)) { PIN_INFO PinInfo; m_pCameraOutput->QueryPinInfo(&PinInfo); CAUUID caGUID; pPages->GetPages(&caGUID); OleCreatePropertyFrame(NULL, 0, 0, L"Property Sheet", 1, (IUnknown **)&(m_pCameraOutput.p), caGUID.cElems, caGUID.pElems, , 0, NULL); CoTaskMemFree(caGUID.pElems); PinInfo.pFilter->Release(); } pPages = NULL; } else { ////////////////////////////////////////////////////////////////////////////// // 加入由 lWidth和lHeight设置的摄像头的宽和高 的功能,默认320*240 // by flymanbox @2009-01-24 ////////////////////////////////////////////////////////////////////////////// int _Width = nWidth, _Height = nHeight; IAMStreamConfig* iconfig; iconfig = NULL; hr = m_pCameraOutput->QueryInterface(IID_IAMStreamConfig, (void**)&iconfig); AM_MEDIA_TYPE* pmt; if(iconfig->GetFormat(&pmt) !=S_OK) { //printf("GetFormat Failed ! \n"); returnfalse; } VIDEOINFOHEADER* phead; if ( pmt->formattype == FORMAT_VideoInfo) { phead=( VIDEOINFOHEADER*)pmt->pbFormat; phead->bmiHeader.biWidth = _Width; phead->bmiHeader.biHeight = _Height; if(( hr=iconfig->SetFormat(pmt)) != S_OK ) { returnfalse; } } iconfig->Release(); iconfig=NULL; MYFREEMEDIATYPE(*pmt); } hr = m_pGraph->Connect(m_pCameraOutput, m_pGrabberInput); hr = m_pGraph->Connect(m_pGrabberOutput, m_pNullInputPin); if (FAILED(hr)) { switch(hr) { case VFW_S_NOPREVIEWPIN : break; case E_FAIL : break; case E_INVALIDARG : break; case E_POINTER : break; } } m_pSampleGrabber->SetBufferSamples(TRUE); m_pSampleGrabber->SetOneShot(TRUE); hr = m_pSampleGrabber->GetConnectedMediaType(&mt); if(FAILED(hr)) returnfalse; VIDEOINFOHEADER *videoHeader; videoHeader = reinterpret_cast<VIDEOINFOHEADER*>(mt.pbFormat); m_nWidth = videoHeader->bmiHeader.biWidth; m_nHeight = videoHeader->bmiHeader.biHeight; m_bConnected =true; pEnum = NULL; returntrue; }
#include "stdafx.h" #include "CameraDS.h" #pragma comment(lib,"Strmiids.lib") ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CCameraDS::CCameraDS() { m_bConnected =false; m_nWidth =0; m_nHeight =0; m_bLock =false; m_bChanged =false; // m_pFrame = NULL; m_nBufferSize =0; m_pNullFilter = NULL; m_pMediaEvent = NULL; m_pSampleGrabberFilter = NULL; m_pGraph = NULL; CoInitialize(NULL); } CCameraDS::~CCameraDS() { CloseCamera(); CoUninitialize(); } void CCameraDS::CloseCamera() { if(m_bConnected) m_pMediaControl->Stop(); m_pGraph = NULL; m_pDeviceFilter = NULL; m_pMediaControl = NULL; m_pSampleGrabberFilter = NULL; m_pSampleGrabber = NULL; m_pGrabberInput = NULL; m_pGrabberOutput = NULL; m_pCameraOutput = NULL; m_pMediaEvent = NULL; m_pNullFilter = NULL; m_pNullInputPin = NULL; //if (m_pFrame) // cvReleaseImage(&m_pFrame); if(!this->m_image.IsNull()) { this->m_image.Destroy(); } m_bConnected =false; m_nWidth =0; m_nHeight =0; m_bLock =false; m_bChanged =false; m_nBufferSize =0; } bool CCameraDS::OpenCamera(int nCamID, bool bDisplayProperties, int nWidth, int nHeight) { HRESULT hr = S_OK; CoInitialize(NULL); // Create the Filter Graph Manager. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph); hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID *)&m_pSampleGrabberFilter); hr = m_pGraph->QueryInterface(IID_IMediaControl, (void**) &m_pMediaControl); hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void**) &m_pMediaEvent); hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID*) &m_pNullFilter); hr = m_pGraph->AddFilter(m_pNullFilter, L"NullRenderer"); hr = m_pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (void**)&m_pSampleGrabber); AM_MEDIA_TYPE mt; ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE)); mt.majortype = MEDIATYPE_Video; mt.subtype = MEDIASUBTYPE_RGB24; mt.formattype = FORMAT_VideoInfo; hr = m_pSampleGrabber->SetMediaType(&mt); MYFREEMEDIATYPE(mt); m_pGraph->AddFilter(m_pSampleGrabberFilter, L"Grabber"); // Bind Device Filter. We know the device because the id was passed in BindFilter(nCamID, &m_pDeviceFilter); m_pGraph->AddFilter(m_pDeviceFilter, NULL); CComPtr<IEnumPins> pEnum; m_pDeviceFilter->EnumPins(&pEnum); hr = pEnum->Reset(); hr = pEnum->Next(1, &m_pCameraOutput, NULL); pEnum = NULL; m_pSampleGrabberFilter->EnumPins(&pEnum); pEnum->Reset(); hr = pEnum->Next(1, &m_pGrabberInput, NULL); pEnum = NULL; m_pSampleGrabberFilter->EnumPins(&pEnum); pEnum->Reset(); pEnum->Skip(1); hr = pEnum->Next(1, &m_pGrabberOutput, NULL); pEnum = NULL; m_pNullFilter->EnumPins(&pEnum); pEnum->Reset(); hr = pEnum->Next(1, &m_pNullInputPin, NULL); //SetCrossBar(); if (bDisplayProperties) { CComPtr<ISpecifyPropertyPages> pPages; HRESULT hr = m_pCameraOutput->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pPages); if (SUCCEEDED(hr)) { PIN_INFO PinInfo; m_pCameraOutput->QueryPinInfo(&PinInfo); CAUUID caGUID; pPages->GetPages(&caGUID); OleCreatePropertyFrame(NULL, 0, 0, L"Property Sheet", 1, (IUnknown **)&(m_pCameraOutput.p), caGUID.cElems, caGUID.pElems, , 0, NULL); CoTaskMemFree(caGUID.pElems); PinInfo.pFilter->Release(); } pPages = NULL; } else { ////////////////////////////////////////////////////////////////////////////// // 加入由 lWidth和lHeight设置的摄像头的宽和高 的功能,默认320*240 // by flymanbox @2009-01-24 ////////////////////////////////////////////////////////////////////////////// int _Width = nWidth, _Height = nHeight; IAMStreamConfig* iconfig; iconfig = NULL; hr = m_pCameraOutput->QueryInterface(IID_IAMStreamConfig, (void**)&iconfig); AM_MEDIA_TYPE* pmt; if(iconfig->GetFormat(&pmt) !=S_OK) { //printf("GetFormat Failed ! \n"); returnfalse; } VIDEOINFOHEADER* phead; if ( pmt->formattype == FORMAT_VideoInfo) { phead=( VIDEOINFOHEADER*)pmt->pbFormat; phead->bmiHeader.biWidth = _Width; phead->bmiHeader.biHeight = _Height; if(( hr=iconfig->SetFormat(pmt)) != S_OK ) { returnfalse; } } iconfig->Release(); iconfig=NULL; MYFREEMEDIATYPE(*pmt); } hr = m_pGraph->Connect(m_pCameraOutput, m_pGrabberInput); hr = m_pGraph->Connect(m_pGrabberOutput, m_pNullInputPin); if (FAILED(hr)) { switch(hr) { case VFW_S_NOPREVIEWPIN : break; case E_FAIL : break; case E_INVALIDARG : break; case E_POINTER : break; } } m_pSampleGrabber->SetBufferSamples(TRUE); m_pSampleGrabber->SetOneShot(TRUE); hr = m_pSampleGrabber->GetConnectedMediaType(&mt); if(FAILED(hr)) returnfalse; VIDEOINFOHEADER *videoHeader; videoHeader = reinterpret_cast<VIDEOINFOHEADER*>(mt.pbFormat); m_nWidth = videoHeader->bmiHeader.biWidth; m_nHeight = videoHeader->bmiHeader.biHeight; m_bConnected =true; pEnum = NULL; returntrue; }