1.配置OpenCV、DirectShow.
2.代码如下:
int hello::Camera(){ int cam_count; cam_count = CCameraDS::CameraCount();//output the number of cameras in my computer cout<<cam_count<<endl; for(int i = 0;i < cam_count;i++){//output the name of all the cameras char cam_name[1024]; int retval = CCameraDS::CameraName(i,cam_name,sizeof(cam_name)); if(retval > 0) cout<<"Camera "<<i<<"'s Name is "<<cam_name<<endl; else cout<<"Can not get Camera "<<i<<"'s name."<<endl; } if(cam_count == 0)return -1; CCameraDS camera;//declare the first camera CCameraDS camera1;//declare the second camera if(!camera.OpenCamera(0,false,320,240)){ cout<<"Can not open 0's camera."<<endl; } if(!camera1.OpenCamera(1,false,320,240)){ cout<<"Can not open 1's camera."<<endl; } IplImage* pFrame = camera.QueryFrame(); IplImage* pFrame1 = camera1.QueryFrame(); cvNamedWindow("camera"); cvNamedWindow("camera1"); while(1){ pFrame = camera.QueryFrame(); cvShowImage("camera",pFrame); pFrame1 = camera1.QueryFrame(); cvShowImage("camera1",pFrame1); if(cvWaitKey(40)==27)break; } cvReleaseImage(&pFrame); cvReleaseImage(&pFrame1); cvDestroyWindow("camera"); cvDestroyWindow("camera1"); return 0; }