Qt5.13+Halcon realizes the enlargement, reduction and movement of pictures in the control

Qt5.13+Halcon realizes the enlargement, reduction and movement of pictures in the control

My implementation platform and premise

System environment: Windows 10
Compilation environment: Qt5.13 + MSVC2015 x64 + Halcon12.0
Prerequisite: The software in the computer has been installed correctly and the project configuration is perfect

First look at the renderings

insert image description here

Source project link

https://download.csdn.net/download/xiaohuihuihuige/12601041

configuration in the pro file

#-------------------------------------------------
#
# Project created by QtCreator 2020-07-06T16:23:43
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ShersDetection
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui


INCLUDEPATH += "$$(HALCONROOT)/include"
INCLUDEPATH += "$$(HALCONROOT)/include/halconcpp"


QMAKE_LIBDIR  += "$$(HALCONROOT)/lib/$$(HALCONARCH)"
LIBS    += "$$(HALCONROOT)/lib/$$(HALCONARCH)/halconcpp.lib"
LIBS    +="$$(HALCONROOT)/lib/$$(HALCONARCH)/halcon.lib"
# Default rules for deployment.
qnx: target.path = /tmp/$${
    
    TARGET}/bin
else: unix:!android: target.path = /opt/$${
    
    TARGET}/bin
!isEmpty(target.path): INSTALLS += target

code in .h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "Halcon.h"
#include "HalconCpp.h"
#include "QMouseEvent"
using namespace HalconCpp;
namespace Ui {
    
    
class MainWindow;
}

class MainWindow : public QMainWindow
{
    
    
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    HTuple hv_WindowHandle;//窗口句柄
    Hlong windID;//控件句柄
    QPoint lastPoint;//鼠标在控件中的位置
    bool mouseDown;//鼠标按下标志位
    HObject src1;//读入初始图像
    float zoom_scale;//放大倍数
    QPoint windowPoint,firstPoint;//图像移动点和初始点
    int m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1, m_dDispImagePartCol1;//显示图像的区域
    int m_dYOffset, m_dXOffset, m_dXO, m_dYO;//图像偏移,与初始位置

    void mousePressEvent(QMouseEvent *event);//鼠标按下事件
    void mouseMoveEvent(QMouseEvent *event);//鼠标移动事件
    void mouseReleaseEvent(QMouseEvent *event);//鼠标松开事件
    void wheelEvent(QWheelEvent *event);//鼠标滚轮事件
    void displayImage(HImage srcImg,HTuple hv_Window);//显示图像
    void moveWnd(QPoint point, HImage srcImg, HTuple hWindow);//移动显示区域

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

Code in .cpp file

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    //初始化窗口
    windID = (Hlong)this->ui->label->winId();
    HalconCpp::OpenWindow(0,0,ui->label->width(),ui->label->height(),windID,"visible","",&hv_WindowHandle);
    SetDraw(hv_WindowHandle,"margin");
    SetLineWidth(hv_WindowHandle,2);
    SetColor(hv_WindowHandle,"red");
    QString path;
    path=QString("E:\\IMG20140409173103.jpg");
    HTuple  img_width , img_height;
    //读入图像
    HalconCpp::ReadImage(&src1,path.toStdString().data());
    HalconCpp::GetImageSize(src1,&img_width,&img_height);
    //设置显示区域
    SetPart(hv_WindowHandle,0,0,img_height- 1 , img_width - 1);
    HalconCpp::DispObj(src1,hv_WindowHandle);
    //初始化放大倍数
    zoom_scale = 1;
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    
    

    QPoint s(ui->centralWidget->pos() + ui->label->pos());//控件在窗口内的坐标
    lastPoint = QPoint(event->pos().x() - s.x(), event->pos().y() - s.y());//鼠标在控件上的坐标
    mouseDown = true;
    if(ui->label->rect().contains(lastPoint))
    {
    
    
        firstPoint = lastPoint;
        m_dXO = firstPoint.x();
        m_dYO = firstPoint.y();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    
       
    QPoint s(ui->centralWidget->pos() + ui->label->pos());//控件在窗口内的坐标
    lastPoint = QPoint(event->pos().x() - s.x(), event->pos().y() - s.y());//鼠标在控件上的坐标
    if(ui->label->rect().contains(lastPoint))
    {
    
    
        windowPoint = lastPoint;
    }
    else
    {
    
    
        windowPoint=QPoint(-1,-1);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    
    

    moveWnd(windowPoint, src1, hv_WindowHandle);
    mouseDown = false;
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    
    
    short zDelta =short(event->delta());
    if (zDelta>0)           //图像放大
    {
    
    
        if (zoom_scale<6)      //最大放大6倍
        {
    
    
            zoom_scale = zoom_scale*1.05;
            displayImage(src1, hv_WindowHandle);
        }
    }
    else if (zDelta<0)                   //图像缩小
    {
    
    
        if (zoom_scale>1)
        {
    
    
            zoom_scale = zoom_scale / 1.05;
            if (zoom_scale < 1)
            {
    
    
                zoom_scale = 1;
            }
            displayImage(src1, hv_WindowHandle);
        }
    }
}

void MainWindow::displayImage(HImage srcImg,HTuple hv_Window)
{
    
    
    double dWidth = srcImg.Width().I();
    double dHeight = srcImg.Height().I();
    double dWidth2 = dWidth / zoom_scale;
    double dHeight2 = dHeight / zoom_scale;
    if (dHeight / 2 - dHeight2 / 2 >= m_dYOffset && dHeight / 2 + dHeight2 / 2 - m_dYOffset <= dHeight)
    {
    
    
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    else if (dHeight / 2 - dHeight2 / 2 <= m_dYOffset)
    {
    
    
        m_dYOffset = dHeight / 2 - dHeight2 / 2;
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    else if (dHeight / 2 + dHeight2 / 2 - m_dYOffset >= dHeight)
    {
    
    
        m_dYOffset = dHeight / 2 + dHeight2 / 2 - dHeight;
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    if (dWidth / 2 - dWidth2 / 2 >= m_dXOffset && dWidth / 2 + dWidth2 / 2 - m_dXOffset <= dWidth)
    {
    
    
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    else if (dWidth / 2 - dWidth2 / 2 <= m_dXOffset)
    {
    
    
        m_dXOffset = dWidth / 2 - dWidth2 / 2;
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    else if (dWidth / 2 + dWidth2 / 2 - m_dXOffset >= dWidth)
    {
    
    
        m_dXOffset = dWidth / 2 + dWidth2 / 2 - dWidth;
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    SetPart(hv_Window, m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1-1, m_dDispImagePartCol1-1);
    DispObj(srcImg, hv_Window);
}

void MainWindow::moveWnd(QPoint point, HImage srcImg, HTuple hWindow)
{
    
    

    QRect m_rPic=ui->label->rect();
    double wWidth = m_rPic.right() - m_rPic.left();
    double wHeight = m_rPic.bottom() - m_rPic.top();
    double dWidth = srcImg.Width().I();
    double dHeight = srcImg.Height().I();
    int xOffset = point.x() - m_dXO;
    int yOffset = point.y() - m_dYO;
    m_dXOffset = m_dXOffset + (point.x() - m_dXO)*dWidth / wWidth / zoom_scale;
    m_dYOffset = m_dYOffset + (point.y() - m_dYO)*dHeight / wHeight / zoom_scale;
    if (m_dDispImagePartRow0 >= yOffset *dHeight / wHeight / zoom_scale && m_dDispImagePartRow1 - yOffset *dHeight / wHeight / zoom_scale <= dHeight)
    {
    
    
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - yOffset *dHeight / wHeight / zoom_scale;
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - yOffset *dHeight / wHeight / zoom_scale;
    }
    else if (m_dDispImagePartRow0 <= yOffset *dHeight / wHeight / zoom_scale)
    {
    
    
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - m_dDispImagePartRow0;
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - m_dDispImagePartRow0;
    }
    else if (m_dDispImagePartRow1 - yOffset *dHeight / wHeight / zoom_scale >= dHeight)
    {
    
    
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - m_dDispImagePartRow1 + dHeight;
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - m_dDispImagePartRow1 + dHeight;
    }
    if (m_dDispImagePartCol0 >= xOffset *dWidth / wWidth / zoom_scale && m_dDispImagePartCol1 - xOffset *dWidth / wWidth / zoom_scale <= dWidth)
    {
    
    
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - xOffset *dWidth / wWidth / zoom_scale;
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - xOffset *dWidth / wWidth / zoom_scale;
    }
    else if (m_dDispImagePartCol0 <= xOffset *dWidth / wWidth / zoom_scale)
    {
    
    
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - m_dDispImagePartCol0;
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - m_dDispImagePartCol0;
    }
    else if (m_dDispImagePartCol1 - xOffset *dWidth / wWidth / zoom_scale >= dWidth)
    {
    
    
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - m_dDispImagePartCol1 + dWidth;
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - m_dDispImagePartCol1 + dWidth;
    }
    ClearWindow(hWindow);
    SetPart(hWindow, m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1-1, m_dDispImagePartCol1-1);
    DispObj(srcImg, hWindow);
}


Guess you like

Origin blog.csdn.net/xiaohuihuihuige/article/details/107282420