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

📄 cvcam.cpp

📁 Using open CV draw color histogram, convert RGB To HSI
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            break;
        }

        if (Render)
        {

            _cvcamCvSync->QueryInterface(IID_IBaseFilter, (void**)&pcvSync);
            _objects.push_back(SafeUnknown(pcvSync));

            IPin* output[4];

            ASSERT(pcvSync);

            pcvSync->FindPin(L"Output1", &output[0]);
            pcvSync->FindPin(L"Output2", &output[1]);
            if (3==nb_cameras)
                pcvSync->FindPin(L"Output3", &output[2]);
            else if (4==nb_cameras)
            {
                pcvSync->FindPin(L"Output3", &output[2]);
                pcvSync->FindPin(L"Output4", &output[3]);
            }

            for (i=0;i<nb_cameras;i++)
                _objects.push_back(SafeUnknown(output[i]));

            for (i=0;i<nb_cameras;i++)
                ASSERT(output[i]);

            if( !_cvcamMediaControl.is_valid() )
            {
                return -1;
            }//if( !_cvcamMediaControl.is_valid() )

            PIN_INFO PinInfo;
            IBaseFilter* nextFilter;
            IPin* pPin;

            for (i=0;i<nb_cameras;i++)
            {
                if (cvcam_properties[indexes[i]].render)
                {
                    output[i]->ConnectedTo(&pPin);
                    if(!pPin)
                        return VFW_E_NOT_CONNECTED;
                    _objects.push_back(SafeUnknown(pPin));

                    pPin->QueryPinInfo(&PinInfo);

                    nextFilter = PinInfo.pFilter;

                    _objects.push_back(SafeUnknown(nextFilter));


                    //IVideoWindow* pVideoWindow;
                    //            nextFilter->QueryInterface(IID_IVideoWindow, (void**)&_cvcamVideoWindow);

                    HRESULT hr = _cvcamCapGraphBuilder->FindInterface(NULL, &MEDIATYPE_Video, nextFilter,
                        IID_IVideoWindow, (void **)&_cvcamVideoWindow);
                    if(hr != NOERROR)
                        return hr;

                    HWND hWnd;

                    cvcamGetProperty(indexes[i], CVCAM_PROP_WINDOW, &hWnd);
                    if(!hWnd)
                    {
                        hWnd = _cvcamCreateWindow();
                        cvcamSetProperty(indexes[i], CVCAM_PROP_WINDOW, &hWnd);
                    }

                    _cvcamVideoWindow->put_Owner((OAHWND)hWnd);

                    long flags;
                    _cvcamVideoWindow->get_WindowStyle(&flags);
                    _cvcamVideoWindow->put_WindowStyle(flags & (~WS_CAPTION) | WS_CHILD);
                    _cvcamVideoWindow->put_MessageDrain((OAHWND)hWnd);

                    // Get the rectangle dimensions and resize the client window
                    AM_MEDIA_TYPE amt;
                    pPin->ConnectionMediaType(&amt);
                    VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)amt.pbFormat;
                    if(!vih)
                    {
                        return -1;
                    }

                    int rndheight = cvcam_properties[indexes[i]].rndheight?
                        cvcam_properties[indexes[i]].rndheight:
                    vih->bmiHeader.biHeight;


                    int rndwidth = cvcam_properties[indexes[i]].rndwidth?
                        cvcam_properties[indexes[i]].rndwidth:
                    vih->bmiHeader.biWidth;


                    _cvcamVideoWindow->SetWindowPosition( 0, 0, rndwidth, rndheight );
                    const char* name = cvGetWindowName(hWnd);
                    cvResizeWindow(name, rndwidth, rndheight);


                }//if(cvcam_properties[indexes[i]].render)
            }

    }//if(cvcam_properties[indexes[0]].render||cvcam_properties[indexes[1]].render)

    // MessageBox(NULL,"going run", NULL,MB_OK);
    _cvcamMediaControl->Run();
    // MessageBox(NULL,"ran", NULL,MB_OK);

    return 1;
}

/////////////////////////////////////////////////////////////////////////////////////////
/* Start the video */
CVCAM_API int cvcamStart()
{
    if(_cvcamNumberOfEnabled() >= 2)
        return _cvcamStartSeveral();

    vector<SafeUnknown> _objects;
    IBaseFilter* pProxyTrans = 0;
    _cvcamProxyTrans->QueryInterface(IID_IBaseFilter, (void**)&pProxyTrans);
    _objects.push_back(SafeUnknown(pProxyTrans));
    IPin* pProxyTransOut    = get_pin( pProxyTrans, PINDIR_OUTPUT );
    _objects.push_back(SafeUnknown(pProxyTransOut));
    if(!pProxyTrans || !pProxyTransOut)
    {
        return -1;
    }

    if( !_cvcamMediaControl.is_valid() )
    {
        return -1;
    }

    int render;
    cvcamGetProperty(camera_index, CVCAM_PROP_RENDER, &render);
    if(render)
    {
        /* Get the window */
        HWND hWnd;
        cvcamGetProperty(camera_index, CVCAM_PROP_WINDOW, &hWnd);
        if(!hWnd)
        {
            hWnd = _cvcamCreateWindow();
            cvcamSetProperty(camera_index, CVCAM_PROP_WINDOW, &hWnd);
        }

        HRESULT hres = _cvcamVideoWindow->put_Owner((OAHWND)hWnd);
        long flags;
        hres = _cvcamMediaEventEx->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0);
        hres = _cvcamMediaEventEx->SetNotifyFlags(0x00);
        hres = _cvcamMediaEventEx->CancelDefaultHandling(EC_COMPLETE);
        hres = _cvcamVideoWindow->get_WindowStyle(&flags);
        hres = _cvcamVideoWindow->put_WindowStyle(flags & (~WS_CAPTION) | WS_CHILD);
        hres = _cvcamVideoWindow->put_MessageDrain((OAHWND)hWnd);

        // Get the rectangle dimensions and resize the client window
        AM_MEDIA_TYPE amt;
        pProxyTransOut->ConnectionMediaType(&amt);
        VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)amt.pbFormat;
        if(!vih)
        {
            return -1;
        }

        int rndheight = cvcam_properties[camera_index].rndheight?
            cvcam_properties[camera_index].rndheight:
        vih->bmiHeader.biHeight;


        int rndwidth = cvcam_properties[camera_index].rndwidth?
            cvcam_properties[camera_index].rndwidth:
        vih->bmiHeader.biWidth;


        _cvcamVideoWindow->SetWindowPosition( 0, 0, rndwidth, rndheight );
        const char* name = cvGetWindowName(hWnd);
        cvResizeWindow(name, rndwidth, rndheight);
    }

    _cvcamMediaControl->Run();
    return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
/* Stop the video */
CVCAM_API int cvcamStop()
{
    if( _cvcamMediaControl.is_valid() )
    {
        OAFilterState fs;
        _cvcamMediaControl->GetState(0,&fs);
        if(fs == State_Stopped)
            return S_OK;


        _cvcamMediaControl->StopWhenReady();



        if(_cvcamVideoWindow.is_valid() )
        {
            _cvcamVideoWindow->put_Visible(OAFALSE);
            _cvcamVideoWindow->put_Owner(NULL);
            _cvcamVideoWindow->put_MessageDrain(0);

        }

        if(_cvcamVideoWindow2.is_valid() )
        {
            _cvcamVideoWindow2->put_Visible(OAFALSE);
            _cvcamVideoWindow2->put_Owner(NULL);
            _cvcamVideoWindow2->put_MessageDrain(0);

        }

        if(_cvcamVideoWindow3.is_valid() )
        {
            _cvcamVideoWindow3->put_Visible(OAFALSE);
            _cvcamVideoWindow3->put_Owner(NULL);
            _cvcamVideoWindow3->put_MessageDrain(0);

        }

        return 0;
    }
    else
    {
        return -1;
    }
}
/////////////////////////////////////////////////////////////////////////////////////////
/* Pause the video; should be used for preventing data changes during frame reading
    using "frame" and other properties */
CVCAM_API int cvcamPause()
{
    if( _cvcamMediaControl.is_valid() )
    {
        OAFilterState fs;
        _cvcamMediaControl->GetState(0,&fs);
        if(fs == State_Stopped)
            return S_OK;
        _cvcamMediaControl->Pause();
    }

    return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
/* Resume the video */
CVCAM_API int cvcamResume()
{
    if( _cvcamMediaControl.is_valid() )
    {
        OAFilterState fs;
        _cvcamMediaControl->GetState(0,&fs);
        if(fs == State_Stopped)
            return S_OK;

        _cvcamMediaControl->Run();
    }

    return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////
/* Frees all resources */
CVCAM_API int cvcamExit()
{
    _cvcamSource.clear();
    _cvcamReset();
    _cvcamCreateDevEnum = 0;
    _cvcamEnumMon = 0;
    _cvcamMonikers.clear();
    cvcam_properties.clear();



    return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////

CVCAM_API int cvcamBuildStereo()
{
    return -1;
}
/////////////////////////////////////////////////////////////////////////////////////////
/*Procedure for camera selection dialog*/
BOOL CALLBACK SelectionDlgProc(HWND hwndDlg, UINT message,
        WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_INITDIALOG:
        {
            int n = cvcamGetCamerasCount();
            CameraDescription descr;
            HWND cbwnd = GetDlgItem(hwndDlg,IDC_COMBO1);



            for(int k = 0; k < n; k++)
            {
                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:
                {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -