目录
实现效果
业务逻辑
借助QLabel容器,进行显示图片作为背景,然后重写QLabel类实现矩形,直线和圆形的实现。opencv板块直接实现相关图片操作。文章来源:https://www.toymoban.com/news/detail-597998.html
核心代码部分
打开图片
//打开图片
void Widget::on_pushButton_clicked()
{
QImage image;
QString OpenFile;
OpenFile = QFileDialog::getOpenFileName(this,
"please choose an image file",
"",
"Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
if (OpenFile =="") {
QMessageBox::warning(this,QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("打开照片为空!"));
return;
}
file_name = OpenFile;
is_save = true;
container_is_empty = false;
image.load(OpenFile);
src_img.load(OpenFile);
current_img.load(OpenFile);
ui->label->clear();
QPixmap pixmap = QPixmap::fromImage(image);
ui->label->resize(QSize(image.width(),image.height()));
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->setScaledContents(true);
ui->radioButton->setChecked(1);
}
裁切
//裁切
void Widget::btn_cut_img_clicked() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("照片为空!"));
return;
}
//本地裁剪
QImage img = ui->label->pixmap()->toImage();
QImage testImg = img.copy(begin_point.x(), begin_point.y(), end_point.x()-begin_point.x(),end_point.y()-begin_point.y());
QPixmap resImage = QPixmap::fromImage(testImg);
resImage.scaled(ui->label->size(), Qt::IgnoreAspectRatio);
QSize size;
size.setWidth(testImg.width());
size.setHeight(testImg.height());
ui->label->resize(size);
ui->label->setGeometry(init_pos_x, init_pos_y, end_point.x()-begin_point.x(),end_point.y()-begin_point.y());
ui->label->setPixmap(resImage);
current_img = testImg.copy();
ui->label->setScaledContents(true);
ui->radioButton->setChecked(1);
ui->pushButton_5->setEnabled(false);
is_save = false;
}
改变亮度和对比度
//改变亮度和对比度
void Widget::change_bright_and_contact() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("照片为空!"));
return;
}
bright = ui->spinBox->value();
contract = ui->spinBox_2->value();
printf("br= %d,con=%d\n",bright,contract);
QImage image = ui->label->pixmap()->toImage();
Mat img = QImageToMat(current_img),res;
img.convertTo(res,-1,(double)(bright/30+1),contract);
image = Mat2QImage(res);
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->setScaledContents(true);
current_img = image.copy(); //保存当前图片
is_save = false;
}
顺时针旋转和逆时针旋转文章来源地址https://www.toymoban.com/news/detail-597998.html
//顺时针旋转
void Widget::btn_right_rotation() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("照片为空!"));
return;
}
//顺时针旋转90度
QMatrix matrix;
matrix.rotate(90.0);//以90度为例
QImage image = ui->label->pixmap()->toImage();
image = image.transformed(matrix,Qt::SmoothTransformation);
QPixmap pixmap = QPixmap::fromImage(image);
ui->label->setGeometry(init_pos_x,init_pos_y,image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->setScaledContents(true);
current_img = image.copy();
is_save = false;
}
//逆时针旋转
void Widget::btn_left_rotation() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("照片为空!"));
return;
}
//逆时针旋转90度
QMatrix matrix;
matrix.rotate(-90.0);//以90度为例
QImage image = ui->label->pixmap()->toImage();
image = image.transformed(matrix,Qt::SmoothTransformation);
QPixmap pixmap = QPixmap::fromImage(image);
ui->label->setGeometry(init_pos_x,init_pos_y,image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->setScaledContents(true);
current_img = image.copy();
is_save = false;
}
重写的QLabel
//
// Created by ROG on 2022/10/13.
//
#include "MyLabel.h"
#include<QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QRect>
#include<QLabel>
extern bool rectangle_value = false;
extern bool circle_value = false;
extern bool line_value = false;
extern bool is_operate_check = false;
extern QPoint begin_point={0,0},end_point={0,0};
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
}
//鼠标按下
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
//当鼠标左键按下 提示信息
if( ev->button() == Qt::LeftButton && (rectangle_value || line_value || circle_value))
{
m_isMousePress = true;
//获取点坐标
m_beginPoint = ev->pos();
qDebug()<<"00"<<m_beginPoint;
//update();
}else if (ev->button() == Qt::LeftButton && is_operate_check){
//记录初始坐标
OldPos = ev->pos();
Pressed = true;
}
}
//鼠标释放
void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
if(ev->button()==Qt::LeftButton && (rectangle_value || line_value || circle_value))
{
m_endPoint = ev->pos();
m_isMousePress = false;
qDebug()<<"00"<<m_endPoint;
begin_point = m_beginPoint;
end_point = m_endPoint;
update();
}else if (ev->button()==Qt::LeftButton && is_operate_check){
Pressed = false;
setCursor(Qt::ArrowCursor);
}
}
//鼠标移动,更新矩形框
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
if( ev->buttons() & Qt::LeftButton && (rectangle_value || line_value || circle_value))
{
m_midPoint=ev->pos();
update();
}else if (ev->buttons() & Qt::LeftButton && is_operate_check){
this->setCursor(Qt::SizeAllCursor);
QPoint pos = ev->pos();
int xPtInterval = pos.x() - OldPos.x();
int yPtInterval = pos.y() - OldPos.y();
XPtInterval += xPtInterval;
YPtInterval += yPtInterval;
OldPos = pos;
this->setGeometry(XPtInterval,YPtInterval, this->width(), this->height());
update();
}
}
//画画
void MyLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev);//先调用父类的paintEvent为了显示'背景'!!!
QPainter m_painter(this);
m_painter.setPen(QPen(Qt::red,2));
m_painter.save();
if (m_isMousePress)
{
if (rectangle_value) m_painter.drawRect(QRect(m_beginPoint,m_midPoint));
else if (circle_value) m_painter.drawEllipse(QRectF(m_beginPoint,m_midPoint));
else if (line_value) m_painter.drawLine(m_beginPoint,m_midPoint);
}
else if (!m_isMousePress )
{
if (rectangle_value) m_painter.drawRect(QRect(m_beginPoint,m_endPoint));
else if (circle_value) m_painter.drawEllipse(QRectF(m_beginPoint,m_endPoint));
else if (line_value) m_painter.drawLine(m_beginPoint,m_endPoint);
}
m_painter.restore();
}
到了这里,关于qt+opencv实现图片编辑器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!