first version finished.

This commit is contained in:
shouchih_chen 2022-02-13 15:37:34 +08:00
parent f20e51c9ba
commit 3534552d23
8 changed files with 176 additions and 73 deletions

View File

@ -2,5 +2,7 @@
<qresource prefix="/NameCreater">
<file>resource/background.jpg</file>
<file>resource/background_sample.jpg</file>
<file>resource/background_boy.jpg</file>
<file>resource/background_girl.jpg</file>
</qresource>
</RCC>

View File

@ -13,18 +13,18 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{B66B108B-6DA8-408D-936D-988FF8B6B722}</ProjectGuid>
<Keyword>QtVS_v304</Keyword>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|Win32'">8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|Win32'">8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|Win32'">10.0.19041.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|Win32'">10.0.19041.0</WindowsTargetPlatformVersion>
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt_defaults.props')">

View File

@ -1,6 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<QtLastBackgroundBuild>2022-02-09T21:34:52.5655210Z</QtLastBackgroundBuild>
</PropertyGroup>

View File

@ -1,2 +1,4 @@
boy_name,girl_name
łŻŚu§Ó,§d¨Îša
name_1,name_2,bg_type
ÄŹ´éśŁ,ÄŹ¤_ŽŚ,mix
łŻĽKŽŚ,łŻŹfżÎ,boy
§őŤBŞä,§őŤBŞä,girl

1 boy_name name_1 girl_name name_2 bg_type
2 łŻŚu§Ó ÄŹ´éśŁ §d¨Îša ÄŹ¤_ŽŚ mix
3 łŻĽKŽŚ łŻŹfżÎ boy
4 §őŤBŞä §őŤBŞä girl

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

View File

@ -1,97 +1,189 @@
#include "NameCreater.h"
#include <cstdlib>
#include <QtWidgets/QApplication>
#include <QTextCodec>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QGraphicsTextItem>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QDir>
#include "QGraphicsScene"
#include "QGraphicsView"
#include "QGraphicsPixmapItem"
#include "QGraphicsTextItem"
#define BG_PATH ":/NameCreater/resource/background.jpg"
#define BG_MIX_PATH ":/NameCreater/resource/background.jpg"
#define BG_BOY_PATH ":/NameCreater/resource/background_boy.jpg"
#define BG_GIRL_PATH ":/NameCreater/resource/background_girl.jpg"
#define FONT_NAME QStringLiteral("華康圓體 Std W8")
#define FONT_SIZE 76
#define FONT_SIZE 74
#define BG_MIX_TYPE_STR "mix"
#define BG_BOY_TYPE_STR "boy"
#define BG_GIRL_TYPE_STR "girl"
struct NameInfo_t {
QString name1;
QString name2;
int bg_type;
};
static QPointF BOY_POS[] = {
QPointF(235, 400), QPointF(366, 1420),
QPointF(555, 400), QPointF(692, 1420),
QPointF(878, 400), QPointF(1014, 1420),
QPointF(1200, 400), QPointF(1337, 1420),
QPointF(1527, 400), QPointF(1663, 1420),
QPointF(235, 387), QPointF(366, 1433),
QPointF(555, 387), QPointF(692, 1433),
QPointF(878, 387), QPointF(1014, 1433),
QPointF(1200, 387), QPointF(1337, 1433),
QPointF(1527, 387), QPointF(1663, 1433),
};
static int BOY_POS_COUNT = sizeof(BOY_POS) / sizeof(BOY_POS[0]);
static QPointF GIRL_POS[] = {
QPointF(1854, 400), QPointF(1987, 1420),
QPointF(2176, 400), QPointF(2314, 1420),
QPointF(2498, 400), QPointF(2635, 1420),
QPointF(2821, 400), QPointF(2958, 1420),
QPointF(3147, 400), QPointF(3285, 1420),
QPointF(1854, 387), QPointF(1987, 1433),
QPointF(2176, 387), QPointF(2314, 1433),
QPointF(2498, 387), QPointF(2635, 1433),
QPointF(2821, 387), QPointF(2958, 1433),
QPointF(3147, 387), QPointF(3285, 1433),
};
static int GIRL_POS_COUNT = sizeof(GIRL_POS) / sizeof(GIRL_POS[0]);
void saveToImage(const QString& filename , QGraphicsScene *scene) {
void saveToImage(const QString& filename, QGraphicsScene* scene) {
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
int dpm = 300 / 0.0254; // ~300 DPI
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
int dpm = 300 / 0.0254; // ~300 DPI
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
QPainter painter(&image);
scene->render(&painter);
image.save(filename);
QPainter painter(&image);
scene->render(&painter);
if (!QDir("export").exists()) {
QDir().mkdir("export");
}
image.save(QString("export/").append(filename));
}
int main(int argc, char *argv[])
QList<NameInfo_t> getNameByFile(const QString& filename) {
QList<NameInfo_t> nameList;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Load file failed!";
return nameList;
}
int row = 0;
while (!file.atEnd())
{
NameInfo_t info;
QByteArray line = file.readLine();
QByteArrayList csvList = line.split(',');
QString name1 = QString::fromLocal8Bit(csvList.at(0));
QString name2 = QString::fromLocal8Bit(csvList.at(1));
QString type_str = QString::fromLocal8Bit(csvList.last());
qDebug() << "row :" << row << " " << name1 << " " << name2;
if (name1.compare("name_1") == 0 || name2.compare("name_2") == 0) {
continue;
}
info.name1 = name1;
info.name2 = name2;
info.bg_type = type_str.contains(QRegExp("boy")) ? 1 : (type_str.contains(QRegExp("girl"))) ? 2 : 0;
nameList.append(info);
row++;
}
return nameList;
}
int main(int argc, char* argv[])
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
int export_times = 0;
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
QFont font(FONT_NAME, FONT_SIZE);
QColor font_color(0, 0, 0);
QGraphicsScene scene;
QGraphicsView view(&scene);
QImage image(BG_PATH);
QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene.addItem(bgItem);
QFont font(FONT_NAME, FONT_SIZE);
QColor font_color(0, 0, 0);
for (int i = 0; i < BOY_POS_COUNT; i++) {
QGraphicsTextItem* textItem = new QGraphicsTextItem(QStringLiteral("Ĭ\n´é\n¶£"));
textItem->setFont(font);
textItem->setDefaultTextColor(font_color);
textItem->setPos(BOY_POS[i]);
int xscale, yscale;
xscale = yscale = (i % 2 == 1) ? -1 : 1;
textItem->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem);
}
QList<NameInfo_t> nameList = getNameByFile("name.csv");
if (nameList.isEmpty()) {
QMessageBox msgBox;
msgBox.setText(("Name is empty !"));
msgBox.exec();
}
QGraphicsScene scene;
QGraphicsView view(&scene);
for (int n = 0; n < nameList.length(); n++) {
//msgBox.setText(QString().sprintf("Export: %d / %d", export_times, nameList.length()));
NameInfo_t name_info = nameList.at(n);
scene.clear();
QImage image((name_info.bg_type == 1) ? BG_BOY_PATH : (name_info.bg_type == 2) ? BG_GIRL_PATH : BG_MIX_PATH);
QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene.addItem(bgItem);
for (int j = 0; j < 2; j++) {
int flag = j % 2;
QString name = (flag == 0) ? name_info.name1 : name_info.name2;
for (int i = 0; i < BOY_POS_COUNT; i++) {
int xscale, yscale;
xscale = yscale = (i % 2 == 1) ? -1 : 1;
QPointF pos = (flag == 0) ? BOY_POS[i] : GIRL_POS[i];
QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1));
textItem1->setFont(font);
textItem1->setDefaultTextColor(font_color);
textItem1->setPos(pos);
textItem1->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem1);
QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1));
textItem2->setFont(font);
textItem2->setDefaultTextColor(font_color);
textItem2->setPos(pos.x(), pos.y() + 148 * xscale);
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem2);
QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1));
textItem3->setFont(font);
textItem3->setDefaultTextColor(font_color);
textItem3->setPos(pos.x(), pos.y() + 294 * xscale);
textItem3->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem3);
}
}
QString file_name = QString("export_%1.jpg").arg(QString::number(n + 1));
saveToImage(file_name, &scene);
export_times++;
}
QMessageBox msgBox;
msgBox.setText(QString().sprintf("Export Finshed. Count:%d",export_times));
msgBox.exec();
for (int i = 0; i < GIRL_POS_COUNT; i++) {
QGraphicsTextItem* textItem = new QGraphicsTextItem(QStringLiteral("Ĭ\n¤_\n®¦"));
textItem->setFont(font);
textItem->setDefaultTextColor(font_color);
textItem->setPos(GIRL_POS[i]);
int xscale, yscale;
xscale = yscale = (i % 2 == 1) ? -1 : 1;
textItem->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem);
}
view.show();
saveToImage("test.jpg",&scene);
return a.exec();
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB