使用QRubberBand在Qt中裁剪图像 [英] Using QRubberBand to crop image in Qt

查看:1746
本文介绍了使用QRubberBand在Qt中裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用橡皮筋选择图像的一个区域,然后移除橡胶带外的图像部分并显示新图像。但是,当我目前这样做,它不会裁剪正确的区域,并给我错误的形象。

I want to be able to use rubberband to select an area of an image, then remove the parts of the image outside of the rubberband and display the new image. However when I currently do it, it doesnt crop the correct areas and gives me the wrong image.

#include "mainresizewindow.h"
#include "ui_mainresizewindow.h"

QString fileName;

MainResizeWindow::MainResizeWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainResizeWindow)
{
    ui->setupUi(this);
    ui->imageLabel->setScaledContents(true);
    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));
}

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

void MainResizeWindow::open()
{
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());


    if (!fileName.isEmpty()) {
        QImage image(fileName);

        if (image.isNull()) {
            QMessageBox::information(this, tr("Image Viewer"),
                                 tr("Cannot load %1.").arg(fileName));
            return;
        }

    ui->imageLabel->setPixmap(QPixmap::fromImage(image));
    ui->imageLabel->repaint();
    }
}

void MainResizeWindow::mousePressEvent(QMouseEvent *event)
{
    if(ui->imageLabel->underMouse()){
        myPoint = event->pos();
        rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
        rubberBand->show();
    }
}

void MainResizeWindow::mouseMoveEvent(QMouseEvent *event)
{
    rubberBand->setGeometry(QRect(myPoint, event->pos()).normalized());
}

void MainResizeWindow::mouseReleaseEvent(QMouseEvent *event)
{
    QRect myRect(myPoint, event->pos());

    rubberBand->hide();

    QPixmap OriginalPix(*ui->imageLabel->pixmap());

    QImage newImage;
    newImage = OriginalPix.toImage();

    QImage copyImage;
    copyImage = copyImage.copy(myRect);

    ui->imageLabel->setPixmap(QPixmap::fromImage(copyImage));
    ui->imageLabel->repaint();
}

任何帮助。

推荐答案

这里有两个问题 - 相对于图像的矩形的位置和图像在标签中可能缩放的事实。

There are two issues here - the position of the rect relative to the image and the fact that the image is (potentially) scaled in the label.

位置问题:

QRect myRect(myPoint, event->pos());

您应该更改为:

QPoint a = mapToGlobal(myPoint);
QPoint b = event->globalPos();

a = ui->imageLabel->mapFromGlobal(a);
b = ui->imageLabel->mapFromGlobal(b);

然后,标签可能缩放图像,因为您使用setScaledContents所以你需要计算出非标度图像上的实际坐标。这样的东西可能是(未测试/编译过):

Then, the label may be scaling the image because you used setScaledContents(). So you need to work out the actual coordinates on the unscaled image. Something like this maybe (untested/compiled):

QPixmap OriginalPix(*ui->imageLabel->pixmap());
double sx = ui->imageLabel->rect().width();
double sy = ui->imageLabel->rect().height();
sx = OriginalPix.width() / sx;
sy = OriginalPix.height() / sy;
a.x = int(a.x * sx);
b.x = int(b.x * sx);
a.y = int(a.y * sy);
b.y = int(b.y * sy);

QRect myRect(a, b);
...

这篇关于使用QRubberBand在Qt中裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆