-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
214 lines (170 loc) · 6.79 KB
/
Copy pathmainwindow.cpp
File metadata and controls
214 lines (170 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_base_image_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("打开PDF文件"), "",
tr("PDF文件 (*.pdf *.PDF)"));
if (fileName.length() == 0) {
return;
}
QProcess process;
QString command = "identify -format \"%n\n\" \"" + fileName + "\"";
// cerr << command.toStdString() << endl;
process.start(command);
process.waitForFinished(TIMEOUT/2);
QString output = QString(process.readAllStandardOutput());
// cerr << output.toStdString() << endl;
ui->page_end->setValue(output.split("\n").first().toInt());
baseFile = fileName;
ui->base_label->setText(fileName.split("/").last());
ui->textBrowser->append("要修改的图像: " + fileName);
}
void MainWindow::on_paste_image_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("打开照片"), "",
tr("照片 (*.png *.PNG *.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP *.ppm *.PPM *.xbm *.XBM)"));
if (fileName.length() == 0) {
return;
}
overlay = QPixmap(fileName);
if (overlay.isNull()) {
if (!loadPasteImg(fileName)) {
ui->textBrowser->append("<br><b>错误: 无法打开 " + fileName + " 或不支持的格式!</b>");
return;
}
}
pasteImage = fileName;
ui->paste_label->setText(fileName.split("/").last());
ui->textBrowser->append("要粘贴的图像: " + fileName);
}
void MainWindow::on_merge_button_clicked()
{
if (pasteImage.isEmpty() || baseFile.isEmpty()) {
QString error = (pasteImage.isEmpty()) ? "没有要粘贴的图像!" : "没有要修改的图像!";
ui->textBrowser->append(error);
return;
}
QString baseName = ui->base_label->text().split(".").first();
ui->textBrowser->append("在把 " + ui->base_label->text() + " 转换为照片 ...");
QProcess process;
QString command = "pdftopng.exe -r 300 \"" + baseFile + "\" \"" + baseName + ".png\"";
// cerr << command.toStdString() << endl;
process.setWorkingDirectory(QCoreApplication::applicationDirPath());
process.start(command);
process.waitForFinished(TIMEOUT * 120);
QString errors = QString(process.readAllStandardError());
// cout << process.readAllStandardOutput().toStdString() << endl;
if (errors.toLower().contains("invalid") || errors.toLower().contains("unable to open")) {
ui->textBrowser->append("<br>这个操作有错误:");
ui->textBrowser->append(errors);
deletePNG();
return;
}
else if (!errors.isEmpty()) {
ui->textBrowser->append("<br>这个操作有警告:");
ui->textBrowser->append(errors);
}
QStringList filters;
filters << (baseName + "*.png");
QDir dir(QCoreApplication::applicationDirPath());
QFileInfoList files = dir.entryInfoList(filters, QDir::Files|QDir::NoDotAndDotDot);
if (files.size() < ui->page_end->value()) {
ui->textBrowser->append("<br><b>错误: 页面结束比 " + baseFile + " 页数大!</b>");
deletePNG();
return;
}
ui->textBrowser->append("<br>在改照片 ...");
for(size_t i = ui->page_start->value() - 1; i < ui->page_end->value(); ++i) {
// cerr << files[i].fileName().toStdString() << endl;
ui->textBrowser->append(files[i].fileName() + " ...");
pasteTo(QCoreApplication::applicationDirPath() + "\\" + files[i].fileName());
}
QString newName = baseFile.split(".").first() + "_NEW.pdf";
ui->textBrowser->append("<br>在把照片转换为 <b>" + newName + "</b> ...");
command = "magick \"" + baseName + "*.png\" -units PixelsPerInch -quality 100 -density 300 \"" + newName + "\"";
process.start(command);
process.waitForFinished(TIMEOUT * 60);
errors = QString(process.readAllStandardError());
// cout << process.readAllStandardOutput().toStdString() << endl;
if (errors.toLower().contains("invalid") || errors.toLower().contains("unable to open")) {
ui->textBrowser->append("<br>这个操作有错误:");
ui->textBrowser->append(errors);
deletePNG();
return;
}
else if (!errors.isEmpty()) {
ui->textBrowser->append("<br>这个操作有警告:");
ui->textBrowser->append(errors);
}
ui->textBrowser->append("<br>完成。<br>");
if (!(ui->checkBox->isChecked())) {
deletePNG();
}
}
void MainWindow::on_convert_button_clicked()
{
if (baseFile.isEmpty()) {
ui->textBrowser->append("没有要修改的图像!");
return;
}
QString baseName = baseFile.split(".").first();
ui->textBrowser->append("在把 " + ui->base_label->text() + " 转换为照片 ...");
QProcess process;
QString command = "pdftopng.exe -r 300 -f " + QString::number(ui->page_start->value()) + " -l " + QString::number(ui->page_end->value());
command += " \"" + baseFile + "\" \"" + baseName + ".png\"";
// cerr << command.toStdString() << endl;
process.setWorkingDirectory(QCoreApplication::applicationDirPath());
process.start(command);
process.waitForFinished(TIMEOUT * 120);
QString errors = QString(process.readAllStandardError());
// cout << process.readAllStandardOutput().toStdString() << endl;
if (errors.toLower().contains("invalid") || errors.toLower().contains("unable to open")) {
ui->textBrowser->append("<br>这个操作有错误:");
ui->textBrowser->append(errors);
return;
}
else if (!errors.isEmpty()) {
ui->textBrowser->append("<br>这个操作有警告:");
ui->textBrowser->append(errors);
}
ui->textBrowser->append("<br>图像保存到: <b>" + baseFile.left(baseFile.size() - ui->base_label->text().size()) + "</b><br>");
}
bool MainWindow::loadPasteImg(const QString& fileName) {
for (size_t i = 0; i < supportedFormats.size(); ++i) {
overlay = QPixmap(fileName, supportedFormats[i]);
if (!overlay.isNull()) {
return true;
}
}
return false;
}
void MainWindow::pasteTo(const QString& baseImage) {
// cerr << baseImage.toStdString() << endl;
QPixmap base (baseImage, "PNG");
// cerr << (base.isNull() == true) << endl;
QPainter painter(&base);
painter.drawPixmap(ui->x_coord->value(), ui->y_coord->value(), overlay);
base.save(baseImage, "PNG");
}
void MainWindow::deletePNG() {
QProcess process;
process.setWorkingDirectory(QCoreApplication::applicationDirPath());
process.start("cmd.exe");
process.write("del *.png\n\r");
process.write("exit\n\r");
process.waitForFinished();
}