vmerge.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "vmerge.h"
  2. #include "ui_vmerge.h"
  3. #include "qtoolbar.h"
  4. #include "QVBoxLayout"
  5. #include "QDir"
  6. #include "QFileDialog"
  7. #include "QDebug"
  8. #include <QWheelEvent>
  9. #include <QMenu>
  10. #include <QPainter>
  11. #include<QVector>
  12. #include<QDesktopWidget>
  13. VMerge::VMerge(QWidget *parent) :
  14. QWidget(parent),
  15. ui(new Ui::VMerge)
  16. {
  17. ui->setupUi(this);
  18. //使用UI设计的Actions设计工具栏
  19. QToolBar* locToolBar = new QToolBar(tr("图片"),this); //创建工具栏
  20. locToolBar->addAction(ui->saveAction);
  21. connect (ui->saveAction,SIGNAL(triggered(bool)),this,SLOT(imageSave()));
  22. locToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  23. QVBoxLayout *Layout = new QVBoxLayout();//图片合成主界面垂直布局
  24. Layout->addWidget(locToolBar); //设置工具栏和编辑器上下布局
  25. label1=new QLabel();
  26. label1->setStyleSheet("border:1px solid red;");
  27. label1->installEventFilter(this);//安装事件过滤器
  28. label2=new QLabel();
  29. label2->setStyleSheet("border:1px solid red;");
  30. label2->installEventFilter(this);//安装事件过滤器
  31. Layout->addWidget(label1);
  32. Layout->addWidget(label2);
  33. this->setLayout(Layout); //设置布局
  34. }
  35. void VMerge::imageSave(){
  36. QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.jpg)")); //选择路径
  37. QVector<QImage> vector1;
  38. QImage q1=label1->pixmap()->toImage();
  39. vector1.append(label1->pixmap()->scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation).toImage());
  40. vector1.append(label2->pixmap()->scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation).toImage());
  41. QImage q= MergeImageV(vector1,filename1);
  42. }
  43. VMerge::~VMerge()
  44. {
  45. delete ui;
  46. }
  47. bool VMerge::eventFilter(QObject *obj, QEvent *event)
  48. {
  49. if (obj == label1)//指定某个QLabel
  50. {
  51. if (event->type() == QEvent::MouseButtonPress)//mouse button pressed
  52. {
  53. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  54. if(mouseEvent->button() == Qt::LeftButton)
  55. {
  56. QString curPath=QDir::currentPath();//获取系统当前目录
  57. //获取应用程序的路径
  58. QString dlgTitle="选择一个文件"; //对话框标题
  59. QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
  60. QString LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
  61. QImage Image;
  62. Image.load(LocalFileName);
  63. double dpiX = QApplication::desktop()->physicalDpiX();
  64. double dpiY = QApplication::desktop()->physicalDpiY();
  65. int physicalX=(int)(dpiX*5.5/2.54+0.5f);
  66. int physicalY=(int)(dpiY*7/2.54+0.5f);
  67. QPixmap pixmap = QPixmap::fromImage(Image);
  68. QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  69. // label1->setScaledContents(true);
  70. label1->setAlignment(Qt::AlignCenter);
  71. label1->setPixmap(fitpixmap);
  72. // qDebug() << pixmap.width()<<pixmap.height();
  73. // fitpixmap = pixmap.scaled(307, 389, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  74. // label2->setPixmap(fitpixmap);
  75. return true;
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81. }
  82. else
  83. {
  84. return false;
  85. }
  86. }
  87. if (obj == label2)//指定某个QLabel
  88. {
  89. if (event->type() == QEvent::MouseButtonPress)//mouse button pressed
  90. {
  91. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
  92. if(mouseEvent->button() == Qt::LeftButton)
  93. {
  94. QString curPath=QDir::currentPath();//获取系统当前目录
  95. //获取应用程序的路径
  96. QString dlgTitle="选择一个文件"; //对话框标题
  97. QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
  98. QString LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
  99. QImage Image;
  100. Image.load(LocalFileName);
  101. double dpiX = QApplication::desktop()->physicalDpiX();
  102. double dpiY = QApplication::desktop()->physicalDpiY();
  103. int physicalX=(int)(dpiX*5.5/2.54+0.5f);
  104. int physicalY=(int)(dpiY*7/2.54+0.5f);
  105. QPixmap pixmap = QPixmap::fromImage(Image);
  106. QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  107. // label1->setScaledContents(true);
  108. label2->setAlignment(Qt::AlignCenter);
  109. label2->setPixmap(fitpixmap);
  110. update();
  111. // qDebug() << pixmap.width()<<pixmap.height();
  112. // fitpixmap = pixmap.scaled(307, 389, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  113. // label2->setPixmap(fitpixmap);
  114. return true;
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. }
  127. QImage VMerge::MergeImageV(QVector<QImage>image, QString filename){
  128. int max_width = 0;
  129. int image_height=0;
  130. QVector <QImage > ::iterator it;
  131. for(it = image.begin();it!=image.end();++it)
  132. {
  133. int height = (*it).height();
  134. image_height += height;
  135. if((*it).width() > max_width)
  136. {
  137. max_width =(*it).width();
  138. }
  139. }
  140. QImage result_image_v(max_width,image_height+100,QImage::Format_RGB32);
  141. result_image_v.fill(Qt::white);
  142. QPainter painter_v;
  143. painter_v.begin(&result_image_v);
  144. int y_number=0;
  145. for(it = image.begin();it!=image.end();++it)
  146. {
  147. qDebug()<<y_number<<(*it).height();
  148. //只执行一次,绘制图片间隔
  149. if(y_number==0){
  150. painter_v.drawImage(0,y_number,(*it));
  151. y_number += (*it).height()+100;
  152. }else{
  153. painter_v.drawImage(0,y_number,(*it));
  154. }
  155. }
  156. painter_v.end();
  157. // QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
  158. // emit startConvertMove(1,1);
  159. result_image_v.save(filename,"jpg");
  160. return result_image_v;
  161. }