一、qt设计师加入的控件如下:
- LineEdit //显示选择的照片路径
- toolButton // 选择照片按钮
- pushButton // 开始按钮
二、代码
qt_demo.h
#ifndef QT_DEMO_H
#define QT_DEMO_H
#include <QtWidgets/QMainWindow>
#include "ui_qt_demo.h"
//! qt
#include<qjsonobject.h>
#include<qjsonarray.h>
#include<qjsondocument.h>
#include<qbytearray.h>
#include<QFile>
#include<Qstring>
#include<qpixmap.h>
#include<qbuffer.h>
//!opencv
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
/*!
* @brief 存的点信息结构体
* param[in] vec_points: shape上有哪些点 typeID: shape的类型 shapetype: shape的形状
*/
struct Shape
{
vector<cv::Point2d> vec_points;
int typeID;
QString shapetype; // line polygon
};
class qt_demo : public QMainWindow
{
Q_OBJECT
public:
qt_demo(QWidget *parent = 0);
~qt_demo();
/*!
* @brief shape容器数据存入json文件
* param[in] filename: json保存路径 vec_points: shape容器 imagePath: 照片路径 imageData:照片base64信息 imageHeight:照片高度 imageWidth:照片宽度
*/
void points2Json(const QString &filename, const vector<Shape> &vec_points, const QString &imagePath, const QString &imageData, int imageHeight, int imageWidth);
/*!
* @brief 保存照片的base64信息
* param[in] filename: 照片路径 imageHeight: 照片高度 imageWidth: 照片宽度
*/
QString getImageData(const QString &filename, int &imageHeight, int &imageWidth);
private:
Ui::qt_demoClass ui;
private slots:
void pushButton(); // 开始按钮槽函数
void selectFigureButton(); //选择照片文件槽函数
};
#endif // QT_DEMO_H
qt_demo.cpp
#include "qt_demo.h"
#include <qfiledialog.h>
#include<qdebug.h>
using namespace std;
qt_demo::qt_demo(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
// 开始按钮
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(pushButton()));
//选择照片文件按钮
connect(ui.toolButton, SIGNAL(clicked()), this, SLOT(selectFigureButton()));
}
/*!
* @brief shape容器数据存入json文件
* param[in] filename: json保存路径 vec_points: shape容器 imagePath: 照片路径 imageData:照片base64信息 imageHeight:照片高度 imageWidth:照片宽度
*/
void qt_demo::points2Json(const QString &filename, const vector<Shape> &vec_points, const QString &imagePath, const QString &imageData, int imageHeight, int imageWidth)
{
QJsonObject rootObj;
QJsonObject flags;
rootObj.insert("version", "1.0.1");
rootObj.insert("flags", flags);
QJsonArray shapesJson;
for (int i = 0; i < vec_points.size(); i++)
{
QJsonObject shapeJson;
shapeJson.insert("label", vec_points[i].typeID);
QJsonArray pointsJson;
for (int j = 0; j < vec_points[i].vec_points.size(); j++)
{
QJsonArray pointJson;
pointJson.append(vec_points[i].vec_points[j].x);
pointJson.append(vec_points[i].vec_points[j].y);
pointsJson.append(pointJson);
}
shapeJson.insert("points", pointsJson);
shapeJson.insert("group_id", NULL);
shapeJson.insert("shape_type", vec_points[i].shapetype);
QJsonObject flags;
shapeJson.insert("flags", flags);
shapesJson.append(shapeJson);
}
rootObj.insert("shapes", shapesJson);
rootObj.insert("imagePath", imagePath);
rootObj.insert("imageData", imageData);
rootObj.insert("imageHeight", imageHeight);
rootObj.insert("imageWidth", imageWidth);
QJsonDocument doc(rootObj);
QByteArray ba = doc.toJson(QJsonDocument::Indented);
QFile labelFile(filename);
if (!labelFile.open(QIODevice::WriteOnly))
{
qDebug() << "写文件" << filename << "失败"<<endl;
}
labelFile.write(ba);
labelFile.close();
}
/*!
* @brief 保存照片的base64信息
* param[in] filename: 照片路径 imageHeight: 照片高度 imageWidth: 照片宽度
*/
QString qt_demo::getImageData(const QString &filename, int &imageHeight, int &imageWidth)
{
QImage image(filename);
QPixmap pixmap = QPixmap::fromImage(image);
imageHeight = pixmap.height();
imageWidth = pixmap.width();
QByteArray ba;
QBuffer buf(&ba);
pixmap.save(&buf, "jpg");
buf.close();
QString imageData = ba.toBase64();
return imageData;
}
void qt_demo::selectFigureButton()
{
QString directory = QFileDialog::getOpenFileName(this, tr("open a file."), "", tr("point cloud(*.jpg)"));
if (!directory.isEmpty())
{
ui.lineEdit->setText(directory);
}
}
void qt_demo::pushButton()
{
QString filename = ui.lineEdit->text().trimmed(); // 读取lineedit上的信息 .trimmed() 自动去除空格
int imageHeight, imageWidth;
imageHeight = imageWidth = 0;
QString imageData = getImageData(filename, imageHeight, imageWidth);
Shape shape;
shape.shapetype = "line";
for (int i = 100; i < 1000; i += 200)
{
cv::Point2d point;
point.x = i + 0.1;
point.y = i + 0.1;
shape.vec_points.push_back(point);
}
shape.typeID = 1;
vector<Shape> shapes;
shapes.push_back(shape);
QString filename_json = filename.left(filename.lastIndexOf(".") + 1) + "json";
points2Json(filename_json, shapes, "", imageData, imageHeight, imageWidth);
}
qt_demo::~qt_demo()
{
}
三、结果