⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvcam.cpp

📁 Using open CV draw color histogram, convert RGB To HSI
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                    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 dialog
Return value - number of cameras selected (0,1 or 2);
Argument(out): an array of selected cameras numbers
NULL 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 window
if file is NULL, file browser is opened. if window is 0,
it is created. width and height mean output size's 0 means
those of avi file are used. __cdecl (*callback)(IplImage*) would be
called 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 + -