关于QT加载海康威SDK图像像叠加的解决方案

关于QT加载海康威SDK图像像叠加的解决方案


       最近,需要用QT做一个界面,在显示实时图像的基础上,需要加一个鼠标在视频图像上绘图的功能(即图像叠加),在Github上找不到QT版本的方案,经过试验,代码如下,供大家参考:

/****************鼠标按下事件*****************/
void Calibration::mousePressEvent(QMouseEvent *e)
{
    Q_UNUSED(e);
    left_1=ui->widget_5->mapFromGlobal(QCursor::pos()).x();
    top_1=ui->widget_5->mapFromGlobal(QCursor::pos()).y();
    right_1=ui->widget_5->mapFromGlobal(QCursor::pos()).x();
    bottom_1=ui->widget_5->mapFromGlobal(QCursor::pos()).y();
}
/****************鼠标移动事件*****************/
void Calibration::mouseMoveEvent(QMouseEvent *e)
{
    Q_UNUSED(e);
    right_1=ui->widget_5->mapFromGlobal(QCursor::pos()).x();
    bottom_1=ui->widget_5->mapFromGlobal(QCursor::pos()).y();
}
/****************鼠标绘图事件*****************/
void Calibration::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);
    if(bool_Rectangle==true)
    {
        NET_DVR_RigisterDrawFun(IRealPlayHandle_1,g_DrawFun_Rect_1,0);
    }
}
/****************矩形绘图触发*****************/
void Calibration::on_pbn_Rectangle_clicked()
{
    bool_Rectangle=true;
}
/****************图像叠加回调函数*****************/
void CALLBACK Calibration::g_DrawFun_Rect_1(LONG lRealHandle, HDC hDc, DWORD dwUser)
{
    Q_UNUSED(lRealHandle);
    Q_UNUSED(dwUser);
    RECT rect;
    rect.left=ca->left_1;
    rect.top=ca->top_1;
    rect.right=ca->right_1;
    rect.bottom=ca->bottom_1;
    DrawEdge(hDc,&rect,BDR_SUNKENOUTER,BF_RECT);
}

       视频图像叠加主要用到了NET_DVR_RigisterDrawFun函数,通过调用回调函数获取画图HDC,在HDC上进行绘图。

//横线
int point_X=ca->stPoint.CenterX;
int point_Y=ca->stPoint.CenterY;
MoveToEx(hDc,point_X-50,point_Y,nullptr);
LineTo(hDc,point_X+50,point_Y);
//竖线
int point_X=ca->stPoint.CenterX;
int point_Y=ca->stPoint.CenterY;
MoveToEx(hDc,point_X,point_Y-50,nullptr);
LineTo(hDc,point_X,point_Y+50);    
//交叉线
int point_X=ca->stPoint.CenterX;
int point_Y=ca->stPoint.CenterY;
MoveToEx(hDc,point_X-50,point_Y,nullptr);
LineTo(hDc,point_X+50,point_Y);
MoveToEx(hDc,point_X,point_Y-50,nullptr);
LineTo(hDc,point_X,point_Y+50);
//矩形
POINT poly[5];
poly[0].x=ca->stRectangle.left;
poly[0].y=ca->stRectangle.top;
poly[1].x=ca->stRectangle.right;
poly[1].y=ca->stRectangle.top;
poly[2].x=ca->stRectangle.right;
poly[2].y=ca->stRectangle.bottom;
poly[3].x=ca->stRectangle.left;
poly[3].y=ca->stRectangle.bottom;
poly[4].x=ca->stRectangle.left;
poly[4].y=ca->stRectangle.top;
Polyline(hDc,poly,5);
//圆弧
HPEN hPen=CreatePen(PS_SOLID,1,ca->selectColor);
SelectObject(hDc,hPen);
int point_X=ca->stPoint.CenterX;
int point_Y=ca->stPoint.CenterY;
int Radius =ca->stPoint.Radius;
Arc(hDc,point_X-Radius,point_Y-Radius,point_X+Radius,point_Y+Radius,point_X,point_Y-Radius,point_X,point_Y-Radius);

备注:
1.MinGW与MSVC编译器的结果不一样,MSVC编译器无法得出结果。
2.回调函数中必须要引用全局变量,要经过处理。
3.必须在登录和预览函数,获取到图像句柄之后,再调用NET_DVR_RigisterDrawFun叠加函数。
4.画圆弧Arc时,要指定画笔颜色。
5. .pro添加LIBS += -lgdi32 -luser32;
6. .h添加

你可能感兴趣的:(QT,QT,海康威视,叠加,绘图)