#ifndef JSONBASE_H
#define JSONBASE_H
#include<QJsonObject>
class IJsonSerialize
{
public:
IJsonSerialize() = default;
virtual void read( QJsonObject
&json
) = 0;
virtual void write( QJsonObject
&json
) = 0;
};
#endif
#ifndef CAMSETTINGS_H
#define CAMSETTINGS_H
#include<QObject>
#include<JsonBase.h>
#include<QString>
enum ActionType
{Auto
,Manual
};
enum PixelFormat
{JPEG
,PNG
,RAW
};
class Exposure:public IJsonSerialize
{
Q_GADGET
;
public:
Exposure() = default;
void read( QJsonObject
&json
) override
;
void write( QJsonObject
&json
) override
;
ActionType
Mode()const;
void setMode(const ActionType type
);
int ISP() const;
void setISP(const int i
);
int Time() const;
void setTime(const int i
);
private:
ActionType _mode
;
int _isp
;
int _time
;
};
class CamSettings:public IJsonSerialize
{
Q_GADGET
;
public:
CamSettings() =default;
void read( QJsonObject
&json
) override
;
void write( QJsonObject
&json
) override
;
Exposure
getExposure() const;
void setExposure(const Exposure
&e
);
ActionType
Af_Mode() const;
void setAfMode(const ActionType a
);
QString
Rate() const;
void setRate(const QString
&s
);
QString
ProjName() const;
void setProjName(const QString
&s
);
QString
PixFormat()const;
void setPixFormat(const QString
&s
) ;
private:
Exposure _exposure
;
ActionType _af_mode
;
QString _rate
;
QString _proj_name
;
QString _pix_format
;
};
#endif
#include "camsettings.h"
void CamSettings
::read( QJsonObject
&json
)
{
if(json
.contains("exposure") && json
["exposure"].isObject())
{
Exposure ex
;
auto a
=json
["exposure"].toObject();
ex
.read(a
);
_exposure
= ex
;
}
if(json
.contains("afmode") && json
["afmode"].isDouble())
_af_mode
= ActionType(json
["afmode"].toInt());
if(json
.contains("rate") && json
["rate"].isString())
_rate
= json
["rate"].toString();
if(json
.contains("projname") && json
["projname"].isString())
_proj_name
= json
["projname"].toString();
if(json
.contains("pixformat") && json
["pixformat"].isString())
_pix_format
= json
["pixformat"].toString();
}
void CamSettings
::write( QJsonObject
&json
)
{
QJsonObject exp
;
_exposure
.write(exp
);
json
["exposure"] = exp
;
json
["afmode"] = _af_mode
;
json
["rate"] = _rate
;
json
["projname"] = _proj_name
;
json
["pixformat"] = _pix_format
;
}
Exposure CamSettings
::getExposure() const
{
return _exposure
;
}
void CamSettings
::setExposure(const Exposure
&e
)
{
_exposure
= e
;
}
ActionType CamSettings
::Af_Mode() const
{
return _af_mode
;
}
void CamSettings
::setAfMode(const ActionType a
)
{
_af_mode
= a
;
}
QString CamSettings
::Rate() const
{
return _rate
;
}
void CamSettings
::setRate(const QString
&s
)
{
_rate
= s
;
}
QString CamSettings
::ProjName() const
{
return _proj_name
;
}
void CamSettings
::setProjName(const QString
&s
)
{
_proj_name
= s
;
}
QString CamSettings
::PixFormat() const
{
return _pix_format
;
}
void CamSettings
::setPixFormat(const QString
&s
)
{
_pix_format
= s
;
}
void Exposure
::read( QJsonObject
&json
)
{
if(json
.contains("mode") && json
["mode"].isDouble())
_mode
= ActionType(json
["mode"].toInt());
if(json
.contains("isp") && json
["isp"].isDouble())
_isp
= json
["isp"].toInt();
if(json
.contains("time") && json
["time"].isDouble())
_time
= json
["time"].toInt();
}
void Exposure
::write( QJsonObject
&json
)
{
json
["mode"]= _mode
;
json
["isp"] = _isp
;
json
["time"] = _time
;
}
ActionType Exposure
::Mode() const
{
return _mode
;
}
void Exposure
::setMode(const ActionType type
)
{
_mode
= type
;
}
int Exposure
::ISP() const
{
return _isp
;
}
void Exposure
::setISP(const int i
)
{
_isp
=i
;
}
int Exposure
::Time() const
{
return _time
;
}
void Exposure
::setTime(const int i
)
{
_time
= i
;
}
#include <QCoreApplication>
#include<QFile>
#include<QBitArray>
#include<QJsonDocument>
#include<QDebug>
#include "camsettings.h"
using namespace std
;
int main(int argc
, char *argv
[])
{
QFile
_file("camsettings.json");
#ifdef SERIALIZE
Exposure ex
;
ex
.setISP(200);
ex
.setMode(Auto
);
ex
.setTime(30000);
CamSettings cs
;
cs
.setExposure(ex
);
cs
.setRate("30fps");
cs
.setAfMode(Manual
);
cs
.setProjName("MyTestOne");
cs
.setPixFormat("RAW");
QJsonObject obj
;
cs
.write(obj
);
if (!_file
.open(QIODevice
::WriteOnly
)) {
qWarning("Couldn't open save file.");
return false;
}
_file
.write(QJsonDocument(obj
).toJson());
#else
if (!_file
.open(QIODevice
::ReadOnly
)) {
qWarning("Couldn't open save file.");
return false;
}
QByteArray data
= _file
.readAll();
auto doc
= QJsonDocument
::fromJson(data
).object();
CamSettings cs
;
cs
.read(doc
);
qDebug()<< cs
.getExposure().ISP();
#endif
_file
.flush();
_file
.close();
}
转载请注明原文地址: https://lol.8miu.com/read-12773.html