📄 cvcam.cpp
字号:
cvcamGetProperty(k, CVCAM_DESCRIPTION, (void*)&descr); SendMessage(cbwnd,CB_ADDSTRING ,0,(LPARAM) descr.DeviceDescription); } return FALSE; }//case WM_INITDIALOG: case WM_COMMAND: { switch(LOWORD(wParam)) { case IDOK: { //get 1-st combo selection int firstcmb = SendDlgItemMessage(hwndDlg, IDC_COMBO1,CB_GETCURSEL,0,0); if(firstcmb == -1)//nothing selected { EndDialog(hwndDlg, 0); DestroyWindow(hwndDlg); return TRUE; }//if(firstcmb == -1) //get 2-nd combo selection HWND checkwnd = GetDlgItem(hwndDlg,IDC_CHECK1); long ischecked = SendMessage(checkwnd,BM_GETCHECK,0,0); int secondcmb; if(ischecked == BST_CHECKED) { secondcmb = SendDlgItemMessage(hwndDlg, IDC_COMBO2,CB_GETCURSEL,0,0); //consider we've deleted the 1-st combo selection //from the second one's list if(secondcmb>=firstcmb) secondcmb++; }//if (ischecked == BST_CHECKED) else secondcmb = -1; /* //remember windows vector<HWND> wnd; for(int i = 0; i< cvcamGetCamerasCount(); i++) { HWND wnd1; cvcamGetProperty(i, CVCAM_PROP_WINDOW, (void*)&wnd1); wnd.push_back(wnd1); } //reset cvcam cvcamStop(); cvcamExit(); cvcamGetCamerasCount(); //apply settings cvcamSetProperty(firstcmb,CVCAM_PROP_ENABLE, (void*)CVCAMTRUE); cvcamSetProperty(firstcmb,CVCAM_PROP_RENDER, (void*)CVCAMTRUE); if(secondcmb!=-1) { cvcamSetProperty(secondcmb,CVCAM_PROP_ENABLE, (void*)CVCAMTRUE); cvcamSetProperty(secondcmb,CVCAM_PROP_RENDER, (void*)CVCAMTRUE); } for(int j = 0; j< cvcamGetCamerasCount(); j++) cvcamSetProperty(j, CVCAM_PROP_WINDOW, (void*)&(wnd[j])); //init cvcam if(!cvcamInit() ) { MessageBox(0,"bad camera(s) or other error","cvcam error", MB_OK|MB_ICONERROR); return FALSE; } */ //making the return value. HIWORD is //number of cams selected(1 or 2). //HIBYTE of LOWORD is 1-st cam index //LOBYTE of LOWORD is 2-nd cam index int ncams = (secondcmb==-1)?1:2; int result = MAKELONG(MAKEWORD(secondcmb,firstcmb),ncams); EndDialog(hwndDlg, result); //DestroyWindow(hwndDlg); return TRUE; } case IDCANCEL: { EndDialog(hwndDlg, 0); //DestroyWindow(hwndDlg); return TRUE; } case IDC_CHECK1://do we want a second camera? { HWND checkwnd = GetDlgItem(hwndDlg,IDC_CHECK1); long ischecked = SendMessage(checkwnd,BM_GETCHECK,0,0); HWND secondcombo = GetDlgItem(hwndDlg, IDC_COMBO2); if(ischecked == BST_CHECKED) //we do { EnableWindow(secondcombo,TRUE); //fill the second combo int n = cvcamGetCamerasCount(); CameraDescription descr; HWND cbwnd = GetDlgItem(hwndDlg,IDC_COMBO2); SendMessage(cbwnd,CB_RESETCONTENT ,0,0); for(int k = 0; k < n; k++) { cvcamGetProperty(k, CVCAM_DESCRIPTION, (void*)&descr); SendMessage(cbwnd,CB_ADDSTRING ,0, (LPARAM) descr.DeviceDescription); } //remove item selected in a first combo int selected = SendDlgItemMessage(hwndDlg, IDC_COMBO1, CB_GETCURSEL,0,0); SendMessage(cbwnd,CB_DELETESTRING ,(WPARAM)selected,0); } else if(ischecked == BST_UNCHECKED) EnableWindow(secondcombo,FALSE); return FALSE; }//case IDC_CHECK1: case IDC_COMBO1: { if(HIWORD(wParam)==CBN_SELCHANGE) { //reset the second combo int n = cvcamGetCamerasCount(); CameraDescription descr; HWND cbwnd = GetDlgItem(hwndDlg,IDC_COMBO2); SendMessage(cbwnd,CB_RESETCONTENT ,0,0); for(int k = 0; k < n; k++) { cvcamGetProperty(k, CVCAM_DESCRIPTION, (void*)&descr); SendMessage(cbwnd,CB_ADDSTRING ,0, (LPARAM) descr.DeviceDescription); } //remove item selected in a first combo int selected = SendDlgItemMessage(hwndDlg, IDC_COMBO1, CB_GETCURSEL,0,0); SendMessage(cbwnd,CB_DELETESTRING ,(WPARAM)selected,0); }//if(HIWORD(wParam)==CBN_SELCHANGE) return FALSE; }//case IDC_COMBO1: } }//case WM_COMMAND: }//switch (message) return FALSE;}//////////////////////////////////////////////////////////////////////////////////////////*Pops up a camera(s) selection dialogReturn value - number of cameras selected (0,1 or 2);Argument(out): an array of selected cameras numbersNULL if none selected. Should be released with free() when not needed.if NULL passed, not used.*/int cvcamSelectCamera(int** out){ DWORD result = DialogBox( DLLhinst, MAKEINTRESOURCE(IDD_DIALOGBAR), 0, SelectionDlgProc ); if((out) && (HIWORD(result))) { *out = (int*)malloc(HIWORD(result)*sizeof(int)); (*out)[0] = (int)HIBYTE(LOWORD(result)); if(HIWORD(result) == 2) (*out)[1] = (int)LOBYTE(LOWORD(result)); }//if((*out) && (HIWORD(result))) return HIWORD(result);}//////////////////////////////////////////////////////////////////////////////////////////*Plays a specified avi file into a specified windowif file is NULL, file browser is opened. if window is 0,it is created. width and height mean output size's 0 meansthose of avi file are used. __cdecl (*callback)(IplImage*) would becalled on every frame. NULL means no callback*/CVCAM_API int cvcamPlayAVI(const char* file, void* window, int width, int height, void* callback){ //Get the file char path[256]; memset(path,0,256); if(!file) { OPENFILENAME fn; fn.lStructSize = sizeof(OPENFILENAME); fn.hwndOwner = NULL; fn.lpstrFilter = NULL; fn.lpstrFile = path; fn.nMaxFile = 256; fn.lpstrFileTitle = NULL; fn.lpstrInitialDir = NULL; fn.lpstrTitle = NULL; fn.Flags = NULL; fn.lpstrDefExt = "avi"; fn.hInstance = DLLhinst; fn.lpfnHook = NULL; fn.lpstrCustomFilter = NULL; fn.lCustData = NULL; if(!GetOpenFileName(&fn)) return -1; }//if(!file) else strcpy(path,file); /* Get the window */ HWND hWnd = (HWND)window; if(!hWnd) { hWnd = _cvcamCreateWindow(); } SafePointer<IGraphBuilder> pGraph; SafePointer<IMediaControl> pMediaControl; SafePointer<IMediaEventEx> pEvent; SafePointer<IVideoWindow> pVideoWindow; SafePointer<IPin> pPin; SafePointer<IBasicVideo> pBasicVideo; SafePointer<IBaseFilter> pSFilter; SafePointer<IProxyTransform> pProxyTrans; SafePointer<IPin> pSourceOut, pProxyIn, pProxyOut; SafePointer<IEnumPins> pEnumPins; SafePointer<IBaseFilter> pProxyBase; //Initialise COM cvcamUseCom Iusecom; // Create the filter graph manager and query for interfaces. CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); // Build the graph. wchar_t wpath[256]; mbstowcs(wpath, path, strlen(path)+1); HRESULT hres = pGraph->AddSourceFilter(wpath,L"source",&pSFilter); if(FAILED(hres)) return -1; // Create a proxy transform filter if(FAILED(CoCreateInstance(CLSID_ProxyTransform, NULL, CLSCTX_INPROC_SERVER, IID_IProxyTransform, (void**)&pProxyTrans))) { return -1; } //set callback pProxyTrans->set_transform((void(__cdecl*)(void*))callback, 0); //Get Source output pin pSFilter->EnumPins(&pEnumPins); unsigned long fetched(0); pEnumPins->Next(1,&pSourceOut,&fetched); if(!fetched) return -1; pEnumPins = NULL; //Put ProxyTrans into graph pProxyTrans->QueryInterface(IID_IBaseFilter,(void**)&pProxyBase); pGraph->AddFilter(pProxyBase.value(),L"proxy"); //Get ProxyTrans Pins pProxyIn = get_pin( pProxyBase.value(), PINDIR_INPUT ); pProxyOut= get_pin( pProxyBase.value(), PINDIR_OUTPUT ); hres = pGraph->Connect(pSourceOut.value(),pProxyIn.value()); if(FAILED(hres)) return -1; hres = pGraph->Render(pProxyOut.value()); if(FAILED(hres)) return -1; //Gain additional interfaces pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl); pGraph->QueryInterface(IID_IMediaEventEx, (void **)&pEvent); pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVideoWindow); if(!pMediaControl.is_valid() ||!pEvent.is_valid() ||!pVideoWindow.is_valid()) return -1; //Set up the window hres = pVideoWindow->put_Owner((OAHWND)hWnd); long flags; hres = pEvent->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0); hres = pEvent->SetNotifyFlags(0x00); hres = pEvent->CancelDefaultHandling(EC_COMPLETE); hres = pVideoWindow->get_WindowStyle(&flags); hres = pVideoWindow->put_WindowStyle(flags & (~WS_CAPTION) | WS_CHILD); hres = pVideoWindow->put_MessageDrain((OAHWND)hWnd); // Get the rectangle dimensions and resize the client window hres = pGraph->QueryInterface(IID_IBasicVideo,(void**)&pBasicVideo); if(SUCCEEDED(hres)) { long left, top, w, h; pBasicVideo->GetSourcePosition(&left, &top,&w,&h); pBasicVideo->SetDestinationPosition(0, 0, width?width:w, height?height:h); pVideoWindow->SetWindowPosition(0,0,width?width:w,height?height:h); const char* name = cvGetWindowName(hWnd); cvResizeWindow(name, width?width:w, height?height:h); }//if(SUCCEEDED(hres)) // Run the graph. pMediaControl->Run(); // Wait for completion. long evCode; pEvent->WaitForCompletion(INFINITE, &evCode); if(pVideoWindow.is_valid() ) { pVideoWindow->put_Visible(OAFALSE); pVideoWindow->put_Owner(NULL); pVideoWindow->put_MessageDrain(0); } return 0;}/////////////////////////////////////////////////////////////////////////////////////////cvcamAVIFILE cvcamAVIOpenFile (char* file){ return theAvis.AddSource(file, NULL);}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVICloseFile (cvcamAVIFILE file){ return theAvis.ReleaseSource(file);}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVISetWindow (cvcamAVIFILE file, void* window){ return (theAvis[file])->SetWindow((HWND)window);}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVISetCallback (cvcamAVIFILE file, void* callback){ return (theAvis[file])->SetCallBack((void (__cdecl *)(void *))callback);}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVISetSize (cvcamAVIFILE file, int width, int height){ if(width >= 0) theAvis[file]->SetWidth(width); if(height >= 0) theAvis[file]->SetHeight(height); return 0;}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVIRun (cvcamAVIFILE file){ LONGLONG i = 0; theAvis[file]->SetPosition(&i); return theAvis[file]->Start();}int cvcamAVIStop (cvcamAVIFILE file){ return theAvis[file]->Stop();}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVIPause (cvcamAVIFILE file){ return theAvis[file]->Pause();}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVIResume (cvcamAVIFILE file){ return theAvis[file]->Resume();}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVIWaitCompletion (cvcamAVIFILE file){ return theAvis[file]->WaitForCompletion();}/////////////////////////////////////////////////////////////////////////////////////////int cvcamAVIIsRunning (cvcamAVIFILE file){ return theAvis[file]->IsRunning();}/////////////////////////////////////////////////////////////////////////////////////////static int cvcamAVISetProperty(int source, const char* property, void* value){ if (strcmp(property,CVCAM_PROP_CALLBACK) == 0) { return cvcamAVISetCallback(source, value); } if (strcmp(property,CVCAM_PROP_WINDOW) == 0) { return cvcamAVISetWindow(source,value); } if (strcmp(property,CVCAM_RNDWIDTH) == 0) { return cvcamAVISetSize(source,(int)value,-1); } if (strcmp(property,CVCAM_RNDHEIGHT) == 0) { return cvcamAVISetSize(source,-1,(int)value); } if (strcmp(property,CVCAM_PROP_TIME_FORMAT) == 0) { return theAvis[source]->SetTimeFormat((int)value); } if (strcmp(property,CVCAM_PROP_POSITION) == 0) { return theAvis[source]->SetPosition((LONGLONG*)value); } return -1;}/////////////////////////////////////////////////////////////////////////////////////////static int cvcamAVIGetProperty(int source, const char* property, void* value){ if (strcmp(property,CVCAM_PROP_DURATION) == 0) { return theAvis[source]->GetDuration((LONGLONG*)value); } if (strcmp(property,CVCAM_PROP_POSITION) == 0) { return theAvis[source]->GetCurrentPosition((LONGLONG*)value); } return -1;}/////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -