MFC音视频播放器-支持电子放大等功能

前言

本播放器在VS2019下开发,使用ffmpeg+D3D实现视频播放渲染功能。同时本播放器支持录像功能、截图功能、音视频播放功能、码流信息显示、电子放大功能等。D3D的渲染同时支持surface和texture两种方式,电子放大功能是在D3D Texture方式下进行实现。以下为该功能的一些核心代码,具体可以通过本文末的链接进行工程源码下载。

一、播放器电子放大功能操作

步骤一:播放RTSP 视频流后,点击开启电子放大功能按钮,如下图:
步骤二:点击按钮后,视频上会出现电子放大的变倍信息,本功能支持鼠标滚轮进行图形放大与缩小,也支持鼠标拖动视频进行显示,如下图:
步骤三:鼠标滚轮操作电子放大的放大缩小功能,如下图:
步骤四:按住鼠标左键后,可以拖动视频进行显示,如下图:
步骤五:点击关闭电子放大按钮后,恢复正常显示:

二、电子放大相关源码

CVideoPlayer鼠标事件:

void CVideoPlayer::OnLButtonDown(UINT nFlags, CPoint point)
{
    myprint("OnLButtonDown");
    SetFocus();

    if (m_bDigitalZoom) // Digital Zoom
    {
        m_D3DRender.SetStartPt(point);
        m_bLBtnDown = true;
    }

    CStatic::OnLButtonDown(nFlags, point);
}


void CVideoPlayer::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_bLBtnDown = false;
    ReleaseCapture();
}


BOOL CVideoPlayer::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{    
    myprint("OnMouseWheel");
    CRect rcClient;
    GetClientRect(&rcClient);
    CPoint ptTemp = pt;
    ::ScreenToClient(m_hWnd, &ptTemp);
    if (!PtInRect(&rcClient, ptTemp))
        return FALSE;
    m_ptWheel = pt;
    ::ScreenToClient(m_hWnd, &m_ptWheel);

    if (m_bDigitalZoom)
    {
        zDelta /= WHEEL_DELTA;

        myprint("OnMouseWheel zDelta:%d", zDelta);
        if (zDelta > 0)
        {
            for (short i = 0; i < zDelta; i++)
                m_D3DRender.ZoomOut();
        }
        else
        {
            for (short i = 0; i > zDelta; i--)
                m_D3DRender.ZoomIn();
        }
        return TRUE;
    }
    return FALSE;
}

void CVideoPlayer::OnMouseMove(UINT nFlags, CPoint point)
{
    if (m_bPlaying == false)
        return;

    SetFocus();

    if (m_bLBtnDown && m_bDigitalZoom)
    {
        CRect rc;
        GetClientRect(&rc);
        m_D3DRender.MoveToPtStart(point, rc);
    }
    
    CStatic::OnMouseMove(nFlags, point);
}

CD3DRender处理:

//zoom
void CD3DRender::ZoomIn()
{
    m_Radius += m_RadiusFirst / DEFAULT_ZOOM_TIMES;
    myprint("ZoomIn m_RadiusFirst:%lf m_Radius:%lf\n", m_RadiusFirst, m_Radius);

    if (m_Radius >= DEFAULT_ZOOM_TIMES)
    {
        m_Radius = DEFAULT_ZOOM_TIMES;
    }

    D3DXVECTOR3 position(-x_curCam, y_curCam, sinf(m_Angle) * m_Radius);
    D3DXVECTOR3 target(-x_curCam, y_curCam, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &position, &target, &up);

    m_pDirect3DDevice->SetTransform(D3DTS_VIEW, &V);
}

void CD3DRender::ZoomOut()
{
    m_Radius -= m_RadiusFirst / DEFAULT_ZOOM_TIMES;
    myprint("ZoomOut m_RadiusFirst:%lf m_Radius:%lf\n", m_RadiusFirst, m_Radius);

    if (m_Radius <= 1.1f)
    {
        m_Radius = 1.1f;
    }

    D3DXVECTOR3 position(-x_curCam, y_curCam, sinf(m_Angle) * m_Radius);
    D3DXVECTOR3 target(-x_curCam, y_curCam, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &position, &target, &up);

    m_pDirect3DDevice->SetTransform(D3DTS_VIEW, &V);
}

void CD3DRender::ResetZoom()
{
    x_Cam = x_curCam = 0;
    y_Cam = y_curCam = 0;

    m_Radius = m_RadiusFirst;
    m_Angle = m_AngleFirst;

    D3DXVECTOR3 position(cosf(m_Angle) * m_Radius, 0.0f, sinf(m_Angle) * m_Radius);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &position, &target, &up);

    m_pDirect3DDevice->SetTransform(D3DTS_VIEW, &V);
}

HRESULT CD3DRender::AppendZoomTextInfo(TCHAR* sText, CRect rcText) 
{
    my_mutex_lock(m_pDrawMutex);

    memset(&m_DigZoomInfo, 0, sizeof(StreamInfo_t));

    _sntprintf_s(m_DigZoomInfo.sText, sizeof(m_DigZoomInfo.sText), sizeof(m_DigZoomInfo.sText) - 1, "%s", sText);
    m_DigZoomInfo.rcText.top = rcText.top;
    m_DigZoomInfo.rcText.left = rcText.left;
    m_DigZoomInfo.rcText.right = rcText.right;
    m_DigZoomInfo.rcText.bottom = rcText.bottom;

    my_mutex_unlock(m_pDrawMutex);
    return 0;
}

HRESULT CD3DRender::ClearZoomTextInfo()
{
    my_mutex_lock(m_pDrawMutex);

    memset(&m_DigZoomInfo, 0, sizeof(StreamInfo_t));

    my_mutex_unlock(m_pDrawMutex);
    return 0;
}

void CD3DRender::D3DDrawZoomText()
{
    my_mutex_lock(m_pDrawMutex);

    m_pD3DFont->DrawText(NULL, m_DigZoomInfo.sText, -1, &m_DigZoomInfo.rcText, DT_LEFT/*DT_CENTER | DT_VCENTER*/, D3DCOLOR_XRGB(220, 20, 60));

    my_mutex_unlock(m_pDrawMutex);
}

void CD3DRender::SetStartPt(::CPoint pt)
{
    pt_start = pt;
    x_Cam = x_curCam;
    y_Cam = y_curCam;
}


void CD3DRender::MoveToPtStart(::CPoint pt, CRect rc)
{
    float x = x_Cam + 40.0f * ((float)m_Radius / 20.0f) * (float)(pt.x - pt_start.x) / (float)(rc.right - rc.left);
    float y = y_Cam + 40.0f * ((float)m_Radius / 20.0f) * (float)(pt.y - pt_start.y) / (float)(rc.bottom - rc.top);
    x_curCam = x;
    y_curCam = y;

    myprint("position( %f, %f, %f)  x_Cam = %f, y_Cam = %f\n", x, y, sinf(m_Angle) * m_Radius, x_Cam, y_Cam);
    myprint("rc(%d, %d, %d, %d)", rc.left, rc.top, rc.right, rc.bottom);
    D3DXVECTOR3 position(-x, y, sinf(m_Angle) * m_Radius);
    D3DXVECTOR3 target(-x, y, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &position, &target, &up);

    m_pDirect3DDevice->SetTransform(D3DTS_VIEW, &V);
}

三、相关下载

链接1: 可执行exe下载

链接2: 播放器工程源码下载

相关推荐
yuanbenshidiaos3 分钟前
c++------------------函数
开发语言·c++
yuanbenshidiaos7 分钟前
C++----------函数的调用机制
java·c++·算法
tianmu_sama31 分钟前
[Effective C++]条款38-39 复合和private继承
开发语言·c++
羚羊角uou1 小时前
【C++】优先级队列以及仿函数
开发语言·c++
姚先生971 小时前
LeetCode 54. 螺旋矩阵 (C++实现)
c++·leetcode·矩阵
FeboReigns1 小时前
C++简明教程(文章要求学过一点C语言)(1)
c语言·开发语言·c++
FeboReigns1 小时前
C++简明教程(文章要求学过一点C语言)(2)
c语言·开发语言·c++
264玫瑰资源库2 小时前
从零开始C++棋牌游戏开发之第二篇:初识 C++ 游戏开发的基本架构
开发语言·c++·架构
_小柏_2 小时前
C/C++基础知识复习(43)
c语言·开发语言·c++
264玫瑰资源库2 小时前
从零开始C++棋牌游戏开发之第四篇:牌桌界面与交互实现
开发语言·c++·交互