| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "hmerge.h"
- #include "ui_hmerge.h"
- #include "qtoolbar.h"
- #include "QVBoxLayout"
- #include "QDir"
- #include "QFileDialog"
- #include "QDebug"
- #include <QWheelEvent>
- #include <QMenu>
- #include <QPainter>
- #include <QScrollArea>
- #include<QVector>
- #include<QDesktopWidget>
- #include<QScreen>
- HMerge::HMerge(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::HMerge)
- {
- ui->setupUi(this);
- //使用UI设计的Actions设计工具栏
- QToolBar* locToolBar = new QToolBar(tr("图片"),this); //创建工具栏
- locToolBar->addAction(ui->imageOpen);
- connect (ui->imageOpen,SIGNAL(triggered(bool)),this,SLOT(imageOpen()));
- locToolBar->addAction(ui->saveAction);
- connect (ui->saveAction,SIGNAL(triggered(bool)),this,SLOT(imageSave()));
- locToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
- QVBoxLayout *Layout = new QVBoxLayout();//图片标注主界面垂直布局
- Layout->addWidget(locToolBar); //设置工具栏和编辑器上下布局
- QHBoxLayout *layout1=new QHBoxLayout();//工具栏下方水平布局,放入两个垂直布局
- QVBoxLayout *leftLayout=new QVBoxLayout();//工具栏下方左侧垂直布局
- label1=new QLabel(tr("1.5x1.9"));
- leftLayout->addStretch(1);
- leftLayout->addWidget(label1);
- leftLayout->addStretch(1);
- label2=new QLabel(tr("5.5x7"));
- QVBoxLayout *rightLayout=new QVBoxLayout();//工具栏下方右侧垂直布局
- // label2->setStyleSheet("border:2px solid red;");
- // label2->setPixmap(fitpixmap);
- rightLayout->addWidget(label2);
- layout1->addLayout(leftLayout);
- // layout1->addStretch();
- layout1->addLayout(rightLayout);
- // Layout->setContentsMargins(2,2,2,2); //减小边框的宽度
- // Layout->setSpacing(2);
- Layout->addLayout(layout1);
- this->setLayout(Layout); //设置布局
- }
- void HMerge::imageOpen(){
- // //选择单个文件
- QString curPath=QDir::currentPath();//获取系统当前目录
- //获取应用程序的路径
- QString dlgTitle="选择一个文件"; //对话框标题
- QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
- LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
- QImage Image;
- Image.load(LocalFileName);
- qDebug()<<Image.width()<<Image.height();
- double dpiX = QApplication::desktop()->physicalDpiX();
- double dpiY = QApplication::desktop()->physicalDpiY();
- int physicalX=(int)(dpiX*1.5/2.54+0.5f);
- int physicalY=(int)(dpiY*1.9/2.54+0.5f);
- qDebug()<<"aaa:"<<physicalX<<"bbb:"<<physicalY;
- 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);
- int physicalX1=(int)(dpiX*5.5/2.54+0.5f);
- int physicalY1=(int)(dpiY*7/2.54+0.5f);
- qDebug()<<"aaa:"<<physicalX1<<"bbb:"<<physicalY1;
- fitpixmap = pixmap.scaled(physicalX1, physicalY1, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- label2->setPixmap(fitpixmap);
- // label2->setScaledContents(true);
- // LocalFileName = QFileDialog::getOpenFileName(this, "Open Image", "./", tr("Images (*.png *.xpm *.jpg)"));
- QFile file(LocalFileName);
- if (!file.exists())
- return;
- // ZoomValue = 1.0;
- // XPtInterval = 0;
- // YPtInterval = 0;
- update();
- }
- //合成图片
- void HMerge::imageSave(){
- // QImage resultImg = QImage(orginImg.width(), orginImg.height(), QImage::Format_RGB32);
- // QPainter *painter = new QPainter(&resultImg);
- // painter->drawImage(0, 0, leftImg);
- // painter->drawImage(leftImg.width(), 0, rightImg);
- // resultImg.save("flip.jpg");
- QString filename1 = QFileDialog::getSaveFileName(this,tr("保存图片"),"",tr("Images (*.jpg)")); //选择路径
- QVector<QImage> vector1;
- QImage q1,q2;
- q1.load(LocalFileName);
- vector1.append(q1);
- q2.load(LocalFileName);
- QPixmap pixmap = QPixmap::fromImage(q2);
- QPixmap fitpixmap = pixmap.scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
- vector1.append(fitpixmap.toImage());
- QImage q= MergeImageH(vector1,filename1);
- }
- HMerge::~HMerge()
- {
- delete ui;
- }
- QImage HMerge::MergeImageH(QVector <QImage > image, QString filename)
- {
- int image_width=0;
- int max_height = 0;
- QVector <QImage > ::iterator it;
- for(it = image.begin();it!=image.end();++it)
- {
- int width = (*it).width();
- image_width += width;
- qDebug()<<width<<(*it).height();
- if((*it).height()>max_height)
- {
- max_height =(*it).height();
- }
- }
- QImage result_image_h(image_width+300,max_height,QImage::Format_RGB32);
- result_image_h.fill(Qt::white);
- QPainter painter_h;
- painter_h.begin(&result_image_h);
- int x_number=0;
- for(it = image.begin();it!=image.end();++it)
- {
- if(x_number==0){//左侧图片绘制上下空白区域以及中间空白区域
- painter_h.drawImage(x_number,(*it).height(),(*it));
- x_number += (*it).width()+300;
- }else{
- painter_h.drawImage(x_number,0,(*it));
- // x_number += (*it).width();
- }
- }
- painter_h.end();
- // QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
- result_image_h.save(filename,"jpg");
- return result_image_h;
- }
|