MFC+OpenGL显示视频流图片

由于想做一个应用软件,把目前自己的算法应用出去,给给为研究者提供一丢丢便利,需要在MFC中显示视频流文件。我尝试了两种方法:opencv+MFC;openGL+MFC;这两种方法都可行。但是在使用opencv+MFC时,以为是设置双缓冲处有点问题,导致视频流闪烁严重。(实际是读取视频的问题,修改后应该可行,后面改用opengl做了,也出现了这样的问题,后来修改好了)。

1、首先需要加载一个Picture Control的控件;设置ID等
2、在CTrackingAppDlg.h(XXXDlg.h)中定义双缓冲区:

    HDC _hrenderDC; //DC
    HGLRC _hrenderRC; //RC

添加两个初始化函数:

bool CTrackingAppDlg::SetWindowPixelFormat(HDC hDC)
{
    PIXELFORMATDESCRIPTOR pixelDesc;  

    pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);  

    pixelDesc.nVersion = 1;  
    pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER |PFD_TYPE_RGBA;

    pixelDesc.iPixelType = PFD_TYPE_RGBA;
    pixelDesc.cColorBits = 32;
    pixelDesc.cRedBits = 0;  
    pixelDesc.cRedShift = 0;
    pixelDesc.cGreenBits = 0;
    pixelDesc.cGreenShift = 0;
    pixelDesc.cBlueBits = 0;
    pixelDesc.cBlueShift = 0;  
    pixelDesc.cAlphaBits = 0;  
    pixelDesc.cAlphaShift = 0;  
    pixelDesc.cAccumBits = 0;  
    pixelDesc.cAccumRedBits = 0;  
    pixelDesc.cAccumGreenBits = 0;  
    pixelDesc.cAccumBlueBits = 0;  
    pixelDesc.cAccumAlphaBits = 0;  
    pixelDesc.cDepthBits = 0;  
    pixelDesc.cStencilBits = 1;  
    pixelDesc.cAuxBuffers = 0;  
    pixelDesc.iLayerType = PFD_MAIN_PLANE;  
    pixelDesc.bReserved = 0;  
    pixelDesc.dwLayerMask = 0;  
    pixelDesc.dwVisibleMask = 0;  
    pixelDesc.dwDamageMask = 0;  

    _pixelFormat = ChoosePixelFormat(hDC,&pixelDesc);  

    if( _pixelFormat==0 ) // Choose default  
    {  
        _pixelFormat = 1;  
        if(DescribePixelFormat(hDC,_pixelFormat,  sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)  
        {  
            return false;  
        }  
    } 

    if(SetPixelFormat(hDC,_pixelFormat,&pixelDesc)==FALSE)  
    {   
        return false;  
    }  

    return true;  
}

BOOL CTrackingAppDlg::CreateViewGLContext(HDC hDC)  
{  
    _hrenderRC = wglCreateContext(hDC);  

    if(_hrenderRC==NULL)  
        return FALSE;  

    if(wglMakeCurrent(hDC,_hrenderRC)==FALSE)  
        return FALSE; 

    return TRUE;  
}  

3、在InitDialog函数中初始化;

    //读取Picture控件的大小;
    CRect picRect;
    CWnd *wnd = GetDlgItem(IDC_PIC_RAW);
    wnd->GetWindowRect(&picRect);
    //用于设置图片显示的ViewPort窗口
    _app->getRawPictureView(i)->setViewPort(picRect.Width(),picRect.Height());

初始化并设置Opengl显示属性等:

_hrenderDC = ::GetDC(wnd->m_hWnd);
///////////////////////////////////////////Tian///////////////////////////
        if(SetWindowPixelFormat(_hrenderDC)==FALSE)  
            return 0;  

        if(CreateViewGLContext(_hrenderDC)==FALSE)  
            return 0;  

        glPolygonMode(GL_FRONT,GL_FILL);  
        glPolygonMode(GL_BACK,GL_FILL);  
        //////////////////////////////////////////////////////

        glEnable(GL_TEXTURE_2D);  
        glShadeModel(GL_SMOOTH);  
        //它负责把视景体截取的图像按照怎样的高和宽显示到屏幕上。
        //glViewport(0,0,259,231);  
        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();  

        /*      gluPerspective(45,1,0.1,100.0);  
        glMatrixMode(GL_MODELVIEW);  
        glLoadIdentity(); */ 

        glShadeModel(GL_SMOOTH);       // Enable Smooth Shading  
        glClearColor(1.0f, 1.0f, 1.0f, 0.5f);    // Black Background  
        glClearDepth(1.0f);         // Depth Buffer Setup  
        glEnable(GL_DEPTH_TEST);       // Enables Depth Testing  
        glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do  

    glEnableClientState(GL_VERTEX_ARRAY);  
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//缓存模式
    SetTimer(1,10,0);//每隔10ms执行Ontimer函数一次,将Reshape放在Ontimer中可以动态更换图片   

5、OnTimer类:

void CTrackingAppDlg::OnTimer(UINT_PTR nIDEvent)
{

    //showImage(picView->getRawImage(),IDC_PIC_RAW);
    //showImage(picView->getBitImage(),IDC_PIC_BIT);
    wglMakeCurrent(_hrenderDC , _hrenderRC);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(_app)
    {
        for(int i=0;i<2;i++)
            _app->getRawPictureView(i)->reshape();
    }
    SwapBuffers(_hrenderDC);
    CDialogEx::OnTimer(nIDEvent);
}

初始化部分就这么多,其他的与opengl使用相同。不懂的自己多补一点opengl相关基础。
reshape函数如下:

void RawPicView::reshape()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho( -1.0, 1.0, -1.0, 1.0, -40.0, 40.0 );

//  glMatrixMode(GL_MODELVIEW);

    if(_ChannelId == 0)
        glViewport(0,0,_rectWight/2-5,_rectHight);
    else
        glViewport(_rectWight/2+5,0,_rectWight/2-5,_rectHight);

    glLoadIdentity();
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    if(!_initialized)
        initlize();

    glDisable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _textureId );

    if( !_valid )
        replaceTexture();

    glColor3f( 1.0, 1.0, 1.0 );
    glBegin(GL_QUADS); // Drawing Using Triangles  
    ////////////////////////Tian//////////////////////////////////////////////
        glTexCoord2f( 0.0, 0.0 );
        glVertex2f( -1.0, 1.0f );

        glTexCoord2f( 0.0, 1.0 );
        glVertex2f( -1.0, -1.0);    

        glTexCoord2f( 1.0, 1.0 );
        glVertex2f( 1.0, -1.0);

        glTexCoord2f( 1.0, 0.0 );
        glVertex2f( 1.0, 1.0 ); 

    //////////////////////////////////////////////////////////////////////////
    glEnd();

}

当然还是给个结果比较好:
效果如下:这是准备做的一个应用软件截图,如果完成了将会上传,各位敬请期待

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页