国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

qt QPainter 實(shí)現(xiàn)圖片的縮放和平移

這篇具有很好參考價(jià)值的文章主要介紹了qt QPainter 實(shí)現(xiàn)圖片的縮放和平移。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

頭文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <opencv2/opencv.hpp>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    enum  Type {
        None          = 0,
        Amplification ,
        Shrink,
        Lift,
        Right,
        Up,
        Down,
        Move
    };


public:

    cv::Mat img;
    cv::Mat imgchance;
    cv::Mat scalingImg(cv::Mat& mat, int INPUT_H, int INPUT_W);
    QImage matToImage(const cv::Mat& mat);
    QImage imgDst;

private:
    Ui::MainWindow *ui;
//    QPixmap  *pix;
    int action;          //動(dòng)作(放大,縮小,移動(dòng)...)
    int pixW;            //圖片寬
    int pixH;            //圖片高

    int lableW;      //lable 寬
    int lableH;      //lable 高

    QRect PaintRect;         //繪畫區(qū)域
    QLabel label;

    float ratio;                //比例
    QPoint offset;              //一次的圖片偏移值
    QPoint Alloffset;           //總偏移

    void AddComboItem(QComboBox* cmbo);
    bool event(QEvent * event);
    void wheelEvent(QWheelEvent* e);     //鼠標(biāo)滑輪事件
private slots:
    void paintEvent(QPaintEvent *event);
};

#endif // MAINWINDOW_H

CPP

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    Alloffset(0,0)
{
    ui->setupUi(this);
    ratio= 1.0;             //初始化圖片縮放比例
    action = MainWindow::None;
    lableW = pixW = 500;
    lableH = pixH = 900;
    QString path = "D:/Vscode_Project/Img/H_w.jpg";
    img = cv::imread(path.toStdString());
    imgchance = scalingImg(img,pixH,pixW);
    pixW = imgchance.cols;
    pixH = imgchance.rows;
    imgDst = matToImage(imgchance);
    PaintRect.setRect(ui->label->x()-1,ui->label->y()-1,lableW,lableH);

}

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

bool MainWindow::event(QEvent * event)
{
    static bool press=false;
    static QPoint PreDot;

    if(event->type() == QEvent::MouseButtonPress )
    {
        QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);

        //判斷鼠標(biāo)是否是左鍵按下,且鼠標(biāo)位置是否在繪畫區(qū)域
        if(mouse->button()==Qt::LeftButton &&PaintRect.contains(mouse->pos()))
        {
            press=true;
            QApplication::setOverrideCursor(Qt::OpenHandCursor); //設(shè)置鼠標(biāo)樣式

            PreDot = mouse->pos();
        }

    }
    else if(event->type() == QEvent::MouseButtonRelease)
    {
        QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);

        //判斷鼠標(biāo)是否是左鍵釋放,且之前是在繪畫區(qū)域
        if(mouse->button()==Qt::LeftButton && press )
        {
            QApplication::setOverrideCursor(Qt::ArrowCursor); //改回鼠標(biāo)樣式
            press=false;
        }
    }

    if(event->type() == QEvent::MouseMove)              //移動(dòng)圖片
    {
        if(press)
        {
            QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);

            offset.setX(mouse->x() - PreDot.x());
            offset.setY(mouse->y() - PreDot.y());
            PreDot = mouse->pos();
            action = MainWindow::Move;
            this->update();
        }
    }
    return QWidget::event(event);
}

void MainWindow::wheelEvent(QWheelEvent* event)     //鼠標(biāo)滑輪事件
{
    if (event->delta()>0) {      //上滑,縮小

        action=MainWindow::Shrink;
        this->update();

    } else {                    //下滑,放大
        action=MainWindow::Amplification;
        this->update();
    }

    event->accept();
}



void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    int NowW = ratio *pixW;
    int NowH = ratio *pixH;

    if(action==MainWindow::Amplification)           //縮小
    {
        ratio-=0.1*ratio;
        if(ratio<0.18)
            ratio = 0.1;


    }
    else  if(action==MainWindow::Shrink)           //放大
    {

        ratio+=0.1*ratio;
        if(ratio>4.5)
            ratio = 5.000;


    }
    if(action==MainWindow::Amplification || action==MainWindow::Shrink)      //更新圖片
    {
        NowW = ratio *pixW;
        NowH = ratio *pixH;
        action=MainWindow::None;
        imgchance = scalingImg(img,NowH,NowW);
        imgDst = matToImage(imgchance);

    }

    if(action==MainWindow::Move)                    //移動(dòng)
    {
        int offsetx=Alloffset.x()+offset.x();
        Alloffset.setX(offsetx);

        int offsety=Alloffset.y()+offset.y();
        Alloffset.setY(offsety);
        action=MainWindow::None;
    }

    if(abs(Alloffset.x())>=(lableW/2 + NowW/2 -10))    //限制X偏移值
    {
        if(Alloffset.x()>0)
            Alloffset.setX(lableW/2 + NowW/2 -10);
        else
            Alloffset.setX(-lableW/2 + -NowW/2 +10);
    }
    if(abs(Alloffset.y())>=(lableH/2 + NowH/2 -10))    //限制Y偏移值
    {
        if(Alloffset.y()>0)
            Alloffset.setY(lableH/2 + NowH/2 -10);
        else
            Alloffset.setY(-lableH/2 + -NowH/2 +10);

    }

    int x = lableW/2 + Alloffset.x() -NowW/2;
    if(x<0)
        x=0;


    int y = lableH/2 + Alloffset.y() -NowH/2;
    if(y<0)
        y=0;

    int  sx = NowW/2 - lableW/2 - Alloffset.x();
    if(sx<0)
        sx=0;

    int  sy = NowH/2 - lableH/2 - Alloffset.y();
    if(sy<0)
        sy=0;


    int w =(NowW - sx)>lableW? lableW : (NowW - sx);
    if(w>(lableW-x))
        w = lableW-x;

    int h =(NowH - sy)>lableH? lableH : (NowH - sy);
    if(h>(lableH-y))
        h = lableH-y;
    qDebug()<<"start w "<<w;
    qDebug()<<"start h "<<h;

    NowW = ratio *pixW;
    NowH = ratio *pixH;
    action=MainWindow::None;
    imgchance = scalingImg(img,NowH,NowW);
    imgDst = matToImage(imgchance);

    painter.drawRect(ui->label->x()-1,ui->label->y()-1,lableW,lableH); //畫框

    painter.drawTiledPixmap(x+ui->label->x(),y+ui->label->y(),w,h,QPixmap::fromImage(imgDst),sx,sy);
    qDebug()<<"       ";
    qDebug()<<"drawTiledPixmap w "<<w;
    qDebug()<<"drawTiledPixmap h "<<h;
//    qDebug()<<"imgchance w "<<imgchance.cols;
//    qDebug()<<"imgchance h "<<imgchance.rows;




}


cv::Mat MainWindow::scalingImg(cv::Mat& mat, int INPUT_H, int INPUT_W)
{

    int nh = mat.rows;
    int nw = mat.cols;
    double divisor;
    double m_nh = nh / 1.0 / INPUT_H;
    double m_nw = nw / 1.0 / INPUT_W;
    divisor = (m_nh > m_nw ? m_nh : m_nw);
    cv::Mat image = mat.clone();
    cv::Mat image_resize; // 等比例縮放圖
    //圖片要縮:INTER_AREA  圖片要放:INTER_CUBIC
    if (nh > INPUT_H || nw > INPUT_W)
    {
        cv::resize(image, image_resize, cv::Size(nw / divisor, nh / divisor), cv::INTER_AREA);
    }
    else
    {
        cv::resize(image, image_resize, cv::Size(nw / divisor, nh / divisor), cv::INTER_CUBIC);
    }
    return image_resize;
}

QImage MainWindow::matToImage(const cv::Mat& mat)
{
    if (mat.type() == CV_8UC1)
    {
        QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
        image.setColorCount(256);
        for (int i = 0; i < 256; i++)
        {
            image.setColor(i, qRgb(i, i, i));
        }
        uchar* pSrc = mat.data;
        for (int row = 0; row < mat.rows; row++)
        {
            uchar* pDest = image.scanLine(row);
            memcpy(pDest, pSrc, mat.cols);
            pSrc += mat.step;
        }
        return image;
    }
    else if (mat.type() == CV_8UC3)
    {

        const uchar* pSrc = (const uchar*)mat.data;
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_BGR888);
        return image.rgbSwapped();
    }
    else if (mat.type() == CV_8UC4)
    {
        cv::cvtColor(mat, mat, cv::COLOR_BGRA2RGBA);
        const uchar* pSrc = (const uchar*)mat.data;
        QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image;

    }
    else
    {
        return QImage();
    }
}

lable 不是必要的,設(shè)置好關(guān)鍵的2個(gè)值最重要文章來源地址http://www.zghlxwxcb.cn/news/detail-596105.html

到了這里,關(guān)于qt QPainter 實(shí)現(xiàn)圖片的縮放和平移的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • c++ QT opengl鼠標(biāo)控制平移、縮放、繞點(diǎn)旋轉(zhuǎn)

    c++ QT opengl鼠標(biāo)控制平移、縮放、繞點(diǎn)旋轉(zhuǎn)

    坐標(biāo)系固定在左下角 坐標(biāo)系和正方形 一起旋轉(zhuǎn),但不平移與縮放 鼠標(biāo)左鍵平移正方形,右鍵旋轉(zhuǎn),滾輪縮放(放大與縮?。?編寫繪制正方形與坐標(biāo)系函數(shù) 在OpenGL窗口界面繪制 實(shí)現(xiàn)鼠標(biāo)左鍵平移移動(dòng),右鍵旋轉(zhuǎn),滾輪縮放(放大與縮小) 設(shè)置正方形的旋轉(zhuǎn)點(diǎn) ,以及坐標(biāo)系

    2024年02月06日
    瀏覽(42)
  • opencv006圖像處理之仿射變換(旋轉(zhuǎn),縮放,平移)

    opencv006圖像處理之仿射變換(旋轉(zhuǎn),縮放,平移)

    空間變換中的仿射變換對應(yīng)著五種變換,平移,縮放,旋轉(zhuǎn),翻轉(zhuǎn),錯(cuò)切。而這五種變化由原圖像轉(zhuǎn)變到變換圖像的過程,可以用仿射變換矩陣進(jìn)行描述。而這個(gè)變換過程可以用一個(gè)2*3的矩陣與原圖進(jìn)行相乘得到。關(guān)鍵就是這個(gè)矩陣M: ?平移,旋轉(zhuǎn)? ?透視 M: 變換矩陣 desi

    2024年01月21日
    瀏覽(95)
  • 【QT開發(fā)(5)】0919-QT里面新增ui類,新增使用opencv讀取圖片的普通類,在ui類中顯示圖片

    【QT開發(fā)(5)】0919-QT里面新增ui類,新增使用opencv讀取圖片的普通類,在ui類中顯示圖片

    1、Qt Creator快速入門_第三版__霍亞飛編著 2、《Qt+OpenCV顯示圖片(Mat轉(zhuǎn)QImage然后顯示在QLabel上)》 https://gitee.com/hiyanyx/qt5.14-cpp_-empty_-project/tree/Study2023-section5/ git分支“Study2023-section5” 新增ui類 新增使用opencv讀取圖片的普通類 為了更加方便,可在QT 中添加普通類,這樣會(huì)自動(dòng)生

    2024年02月07日
    瀏覽(24)
  • QT學(xué)習(xí)筆記(三)——vs2019+Qt實(shí)現(xiàn)打開影像并以鼠標(biāo)為中心用滾輪控制圖片縮放

    QT學(xué)習(xí)筆記(三)——vs2019+Qt實(shí)現(xiàn)打開影像并以鼠標(biāo)為中心用滾輪控制圖片縮放

    之前寫了一個(gè)博客講怎么顯示一張影像,那個(gè)是基于Qpainter的 今天使用QLabel來顯示影像,并且用鼠標(biāo)滾輪控制縮放。 關(guān)于圖像的打開和顯示,主要參考這個(gè)博客 關(guān)于如何使圖片自適應(yīng)窗口與鋪滿窗口,可以參考這個(gè)博客。 這兩個(gè)博客出自同一作者,都很詳細(xì)。 其中按照第二

    2024年02月09日
    瀏覽(27)
  • Halcon用矩陣實(shí)現(xiàn)圖像變換(平移,旋轉(zhuǎn),縮放,鏡像等)

    Halcon用矩陣實(shí)現(xiàn)圖像變換(平移,旋轉(zhuǎn),縮放,鏡像等)

    目錄 圖像變換介紹 ?用Halcon自帶的算子實(shí)現(xiàn)圖像變換 使用矩陣來實(shí)現(xiàn)相關(guān)算子的功能 一、平移 二、旋轉(zhuǎn) 三、縮放 四、鏡像 完整代碼 ????????在halcon中經(jīng)常會(huì)用到圖像變換的操作,然后這次作業(yè)是用矩陣來實(shí)現(xiàn)相關(guān)算子的功能,學(xué)到了挺多的所以就記錄下來方便復(fù)習(xí)。

    2024年04月17日
    瀏覽(231)
  • python實(shí)現(xiàn)兩函數(shù)通過縮放,平移和旋轉(zhuǎn)進(jìn)行完美擬合

    python實(shí)現(xiàn)兩函數(shù)通過縮放,平移和旋轉(zhuǎn)進(jìn)行完美擬合

    前幾天在工作的時(shí)候接到了一個(gè)需求,希望將不同坐標(biāo)系,不同角度的兩條不規(guī)則曲線,并且組成該曲線的點(diǎn)集數(shù)量不一致,需求是希望那個(gè)可以通過算法的平移和旋轉(zhuǎn)搞到一個(gè)概念里最貼合,擬合態(tài)進(jìn)行比較。 這是初步將兩組數(shù)據(jù)畫到圖里的情況,和背景需求是一致的。其

    2024年02月15日
    瀏覽(19)
  • Android中矩陣Matrix實(shí)現(xiàn)平移,旋轉(zhuǎn),縮放和翻轉(zhuǎn)的用法詳細(xì)介紹

    Android中矩陣Matrix實(shí)現(xiàn)平移,旋轉(zhuǎn),縮放和翻轉(zhuǎn)的用法詳細(xì)介紹

    一,矩陣Matrix的數(shù)學(xué)原理 矩陣的數(shù)學(xué)原理涉及到矩陣的運(yùn)算和變換,是高等代數(shù)學(xué)中的重要概念。在圖形變換中,矩陣起到關(guān)鍵作用,通過矩陣的變換可以改變圖形的位置、形狀和大小。矩陣的運(yùn)算是數(shù)值分析領(lǐng)域的重要問題,對矩陣進(jìn)行分解和簡化可以簡化計(jì)算過程。對于

    2024年01月22日
    瀏覽(38)
  • qt+opencv實(shí)現(xiàn)圖片編輯器

    qt+opencv實(shí)現(xiàn)圖片編輯器

    借助QLabel容器,進(jìn)行顯示圖片作為背景,然后重寫QLabel類實(shí)現(xiàn)矩形,直線和圓形的實(shí)現(xiàn)。opencv板塊直接實(shí)現(xiàn)相關(guān)圖片操作。 打開圖片 裁切 改變亮度和對比度 順時(shí)針旋轉(zhuǎn)和逆時(shí)針旋轉(zhuǎn) 重寫的QLabel

    2024年02月16日
    瀏覽(26)
  • OpenCV對圖片進(jìn)行縮放處理

    OpenCV對圖片進(jìn)行縮放處理

    在下面的代碼中,我會(huì)為你優(yōu)化和解釋這段程序:

    2024年02月13日
    瀏覽(15)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包