目錄
實(shí)現(xiàn)效果
業(yè)務(wù)邏輯
借助QLabel容器,進(jìn)行顯示圖片作為背景,然后重寫QLabel類實(shí)現(xiàn)矩形,直線和圓形的實(shí)現(xiàn)。opencv板塊直接實(shí)現(xiàn)相關(guān)圖片操作。文章來源:http://www.zghlxwxcb.cn/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("錯(cuò)誤"), 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("錯(cuò)誤"), 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;
}
改變亮度和對(duì)比度
//改變亮度和對(duì)比度
void Widget::change_bright_and_contact() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("錯(cuò)誤"), 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(); //保存當(dāng)前圖片
is_save = false;
}
順時(shí)針旋轉(zhuǎn)和逆時(shí)針旋轉(zhuǎn)文章來源地址http://www.zghlxwxcb.cn/news/detail-597998.html
//順時(shí)針旋轉(zhuǎn)
void Widget::btn_right_rotation() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("錯(cuò)誤"), QString::fromLocal8Bit("照片為空!"));
return;
}
//順時(shí)針旋轉(zhuǎn)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;
}
//逆時(shí)針旋轉(zhuǎn)
void Widget::btn_left_rotation() {
if (container_is_empty){
QMessageBox::warning(this,QString::fromLocal8Bit("錯(cuò)誤"), QString::fromLocal8Bit("照片為空!"));
return;
}
//逆時(shí)針旋轉(zhuǎn)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)
{
}
//鼠標(biāo)按下
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
//當(dāng)鼠標(biāo)左鍵按下 提示信息
if( ev->button() == Qt::LeftButton && (rectangle_value || line_value || circle_value))
{
m_isMousePress = true;
//獲取點(diǎn)坐標(biāo)
m_beginPoint = ev->pos();
qDebug()<<"00"<<m_beginPoint;
//update();
}else if (ev->button() == Qt::LeftButton && is_operate_check){
//記錄初始坐標(biāo)
OldPos = ev->pos();
Pressed = true;
}
}
//鼠標(biāo)釋放
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);
}
}
//鼠標(biāo)移動(dòng),更新矩形框
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);//先調(diào)用父類的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();
}
到了這里,關(guān)于qt+opencv實(shí)現(xiàn)圖片編輯器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!