hmerge.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "hmerge.h"
  2. #include "ui_hmerge.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 <QScrollArea>
  12. #include<QVector>
  13. #include<QDesktopWidget>
  14. #include<QScreen>
  15. HMerge::HMerge(QWidget *parent) :
  16. QWidget(parent),
  17. ui(new Ui::HMerge)
  18. {
  19. ui->setupUi(this);
  20. //使用UI设计的Actions设计工具栏
  21. QToolBar* locToolBar = new QToolBar(tr("图片"),this); //创建工具栏
  22. locToolBar->addAction(ui->imageOpen);
  23. connect (ui->imageOpen,SIGNAL(triggered(bool)),this,SLOT(imageOpen()));
  24. locToolBar->addAction(ui->saveAction);
  25. connect (ui->saveAction,SIGNAL(triggered(bool)),this,SLOT(imageSave()));
  26. locToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  27. QVBoxLayout *Layout = new QVBoxLayout();//图片标注主界面垂直布局
  28. Layout->addWidget(locToolBar); //设置工具栏和编辑器上下布局
  29. QHBoxLayout *layout1=new QHBoxLayout();//工具栏下方水平布局,放入两个垂直布局
  30. QVBoxLayout *leftLayout=new QVBoxLayout();//工具栏下方左侧垂直布局
  31. label1=new QLabel(tr("1.5x1.9"));
  32. leftLayout->addStretch(1);
  33. leftLayout->addWidget(label1);
  34. leftLayout->addStretch(1);
  35. label2=new QLabel(tr("5.5x7"));
  36. QVBoxLayout *rightLayout=new QVBoxLayout();//工具栏下方右侧垂直布局
  37. // label2->setStyleSheet("border:2px solid red;");
  38. // label2->setPixmap(fitpixmap);
  39. rightLayout->addWidget(label2);
  40. layout1->addLayout(leftLayout);
  41. // layout1->addStretch();
  42. layout1->addLayout(rightLayout);
  43. // Layout->setContentsMargins(2,2,2,2); //减小边框的宽度
  44. // Layout->setSpacing(2);
  45. Layout->addLayout(layout1);
  46. this->setLayout(Layout); //设置布局
  47. }
  48. void HMerge::imageOpen(){
  49. // //选择单个文件
  50. QString curPath=QDir::currentPath();//获取系统当前目录
  51. //获取应用程序的路径
  52. QString dlgTitle="选择一个文件"; //对话框标题
  53. QString filter="图片文件(*.jpg *.jpeg *.gif *.png)"; //文件过滤器
  54. LocalFileName=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
  55. QImage Image;
  56. Image.load(LocalFileName);
  57. qDebug()<<Image.width()<<Image.height();
  58. double dpiX = QApplication::desktop()->physicalDpiX();
  59. double dpiY = QApplication::desktop()->physicalDpiY();
  60. int physicalX=(int)(dpiX*1.5/2.54+0.5f);
  61. int physicalY=(int)(dpiY*1.9/2.54+0.5f);
  62. qDebug()<<"aaa:"<<physicalX<<"bbb:"<<physicalY;
  63. QPixmap pixmap = QPixmap::fromImage(Image);
  64. QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  65. // label1->setScaledContents(true);
  66. label1->setAlignment(Qt::AlignCenter);
  67. label1->setPixmap(fitpixmap);
  68. int physicalX1=(int)(dpiX*5.5/2.54+0.5f);
  69. int physicalY1=(int)(dpiY*7/2.54+0.5f);
  70. qDebug()<<"aaa:"<<physicalX1<<"bbb:"<<physicalY1;
  71. fitpixmap = pixmap.scaled(physicalX1, physicalY1, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  72. label2->setPixmap(fitpixmap);
  73. // label2->setScaledContents(true);
  74. // LocalFileName = QFileDialog::getOpenFileName(this, "Open Image", "./", tr("Images (*.png *.xpm *.jpg)"));
  75. QFile file(LocalFileName);
  76. if (!file.exists())
  77. return;
  78. // ZoomValue = 1.0;
  79. // XPtInterval = 0;
  80. // YPtInterval = 0;
  81. update();
  82. }
  83. //合成图片
  84. void HMerge::imageSave(){
  85. // QImage resultImg = QImage(orginImg.width(), orginImg.height(), QImage::Format_RGB32);
  86. // QPainter *painter = new QPainter(&resultImg);
  87. // painter->drawImage(0, 0, leftImg);
  88. // painter->drawImage(leftImg.width(), 0, rightImg);
  89. // resultImg.save("flip.jpg");
  90. QString filename1 = QFileDialog::getSaveFileName(this,tr("保存图片"),"",tr("Images (*.jpg)")); //选择路径
  91. QVector<QImage> vector1;
  92. QImage q1,q2;
  93. q1.load(LocalFileName);
  94. vector1.append(q1);
  95. q2.load(LocalFileName);
  96. QPixmap pixmap = QPixmap::fromImage(q2);
  97. QPixmap fitpixmap = pixmap.scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  98. vector1.append(fitpixmap.toImage());
  99. QImage q= MergeImageH(vector1,filename1);
  100. }
  101. HMerge::~HMerge()
  102. {
  103. delete ui;
  104. }
  105. QImage HMerge::MergeImageH(QVector <QImage > image, QString filename)
  106. {
  107. int image_width=0;
  108. int max_height = 0;
  109. QVector <QImage > ::iterator it;
  110. for(it = image.begin();it!=image.end();++it)
  111. {
  112. int width = (*it).width();
  113. image_width += width;
  114. qDebug()<<width<<(*it).height();
  115. if((*it).height()>max_height)
  116. {
  117. max_height =(*it).height();
  118. }
  119. }
  120. QImage result_image_h(image_width+300,max_height,QImage::Format_RGB32);
  121. result_image_h.fill(Qt::white);
  122. QPainter painter_h;
  123. painter_h.begin(&result_image_h);
  124. int x_number=0;
  125. for(it = image.begin();it!=image.end();++it)
  126. {
  127. if(x_number==0){//左侧图片绘制上下空白区域以及中间空白区域
  128. painter_h.drawImage(x_number,(*it).height(),(*it));
  129. x_number += (*it).width()+300;
  130. }else{
  131. painter_h.drawImage(x_number,0,(*it));
  132. // x_number += (*it).width();
  133. }
  134. }
  135. painter_h.end();
  136. // QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
  137. result_image_h.save(filename,"jpg");
  138. return result_image_h;
  139. }