| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include "vmerge.h"
- #include "ui_vmerge.h"
- #include "qtoolbar.h"
- #include "QVBoxLayout"
- #include "QDir"
- #include "QFileDialog"
- #include "QDebug"
- #include <QWheelEvent>
- #include <QMenu>
- #include <QPainter>
- #include<QVector>
- #include<QDesktopWidget>
- VMerge::VMerge(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::VMerge)
- {
- ui->setupUi(this);
- //使用UI设计的Actions设计工具栏
- QToolBar* locToolBar = new QToolBar(tr("图片"),this); //创建工具栏
- locToolBar->addAction(ui->saveAction);
- connect (ui->saveAction,SIGNAL(triggered(bool)),this,SLOT(imageSave()));
- locToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- QVBoxLayout *Layout = new QVBoxLayout();//图片合成主界面垂直布局
- Layout->addWidget(locToolBar); //设置工具栏和编辑器上下布局
- label1=new QLabel();
- label1->setStyleSheet("border:1px solid red;");
- label1->installEventFilter(this);//安装事件过滤器
- label2=new QLabel();
- label2->setStyleSheet("border:1px solid red;");
- label2->installEventFilter(this);//安装事件过滤器
- Layout->addWidget(label1);
- Layout->addWidget(label2);
- this->setLayout(Layout); //设置布局
- }
- void VMerge::imageSave(){
- QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.jpg)")); //选择路径
- QVector<QImage> vector1;
- QImage q1=label1->pixmap()->toImage();
- vector1.append(label1->pixmap()->scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation).toImage());
- vector1.append(label2->pixmap()->scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation).toImage());
- QImage q= MergeImageV(vector1,filename1);
- }
- VMerge::~VMerge()
- {
- delete ui;
- }
- bool VMerge::eventFilter(QObject *obj, QEvent *event)
- {
- if (obj == label1)//指定某个QLabel
- {
- if (event->type() == QEvent::MouseButtonPress)//mouse button pressed
- {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
- if(mouseEvent->button() == Qt::LeftButton)
- {
- QString curPath=QDir::currentPath();//获取系统当前目录
- //获取应用程序的路径
- QString dlgTitle="选择一个文件"; //对话框标题
- QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
- QString LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
- QImage Image;
- Image.load(LocalFileName);
- double dpiX = QApplication::desktop()->physicalDpiX();
- double dpiY = QApplication::desktop()->physicalDpiY();
- int physicalX=(int)(dpiX*5.5/2.54+0.5f);
- int physicalY=(int)(dpiY*7/2.54+0.5f);
- QPixmap pixmap = QPixmap::fromImage(Image);
- QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- // label1->setScaledContents(true);
- label1->setAlignment(Qt::AlignCenter);
- label1->setPixmap(fitpixmap);
- // qDebug() << pixmap.width()<<pixmap.height();
- // fitpixmap = pixmap.scaled(307, 389, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- // label2->setPixmap(fitpixmap);
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- if (obj == label2)//指定某个QLabel
- {
- if (event->type() == QEvent::MouseButtonPress)//mouse button pressed
- {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
- if(mouseEvent->button() == Qt::LeftButton)
- {
- QString curPath=QDir::currentPath();//获取系统当前目录
- //获取应用程序的路径
- QString dlgTitle="选择一个文件"; //对话框标题
- QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
- QString LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
- QImage Image;
- Image.load(LocalFileName);
- double dpiX = QApplication::desktop()->physicalDpiX();
- double dpiY = QApplication::desktop()->physicalDpiY();
- int physicalX=(int)(dpiX*5.5/2.54+0.5f);
- int physicalY=(int)(dpiY*7/2.54+0.5f);
- QPixmap pixmap = QPixmap::fromImage(Image);
- QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- // label1->setScaledContents(true);
- label2->setAlignment(Qt::AlignCenter);
- label2->setPixmap(fitpixmap);
- update();
- // qDebug() << pixmap.width()<<pixmap.height();
- // fitpixmap = pixmap.scaled(307, 389, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- // label2->setPixmap(fitpixmap);
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- QImage VMerge::MergeImageV(QVector<QImage>image, QString filename){
- int max_width = 0;
- int image_height=0;
- QVector <QImage > ::iterator it;
- for(it = image.begin();it!=image.end();++it)
- {
- int height = (*it).height();
- image_height += height;
- if((*it).width() > max_width)
- {
- max_width =(*it).width();
- }
- }
- QImage result_image_v(max_width,image_height+100,QImage::Format_RGB32);
- result_image_v.fill(Qt::white);
- QPainter painter_v;
- painter_v.begin(&result_image_v);
- int y_number=0;
- for(it = image.begin();it!=image.end();++it)
- {
- qDebug()<<y_number<<(*it).height();
- //只执行一次,绘制图片间隔
- if(y_number==0){
- painter_v.drawImage(0,y_number,(*it));
- y_number += (*it).height()+100;
- }else{
- painter_v.drawImage(0,y_number,(*it));
- }
- }
- painter_v.end();
- // QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
- // emit startConvertMove(1,1);
- result_image_v.save(filename,"jpg");
- return result_image_v;
- }
|