51 lines
1.7 KiB
C++
51 lines
1.7 KiB
C++
|
#include "Settings.h"
|
||
|
#include "BoostLog.h"
|
||
|
#include <boost/property_tree/ptree.hpp>
|
||
|
#include <boost/property_tree/xml_parser.hpp>
|
||
|
#include <filesystem>
|
||
|
|
||
|
constexpr auto SettingsFilePath = "settings.xml";
|
||
|
|
||
|
Settings::Settings() {
|
||
|
if (!std::filesystem::exists(SettingsFilePath)) {
|
||
|
save();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Settings::save() {
|
||
|
boost::property_tree::ptree ptree;
|
||
|
ptree.put("Application.DataCollection.ImageFormat", static_cast<int>(m_imageFormat));
|
||
|
|
||
|
std::ostringstream oss;
|
||
|
for (int i = 0; i < static_cast<int>(ImageFormat::None); i++) {
|
||
|
oss << i << ":" << static_cast<ImageFormat>(i) << " ";
|
||
|
}
|
||
|
ptree.put("Application.DataCollection.ImageFormat.<xmlcomment>", oss.str());
|
||
|
|
||
|
ptree.put("Application.DataCollection.ImageQuality", m_imageQuality);
|
||
|
ptree.put("Application.DataCollection.ImageQuality.<xmlcomment>", "0-100,仅对jpg有效");
|
||
|
|
||
|
boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
|
||
|
boost::property_tree::write_xml(SettingsFilePath, ptree, std::locale(), settings);
|
||
|
}
|
||
|
|
||
|
void Settings::load() {
|
||
|
boost::property_tree::ptree ptree;
|
||
|
try {
|
||
|
boost::property_tree::read_xml(SettingsFilePath, ptree);
|
||
|
m_imageFormat = static_cast<ImageFormat>(ptree.get<int>("Application.DataCollection.ImageFormat"));
|
||
|
m_imageQuality = ptree.get<int>("Application.DataCollection.ImageQuality");
|
||
|
} catch (...) {
|
||
|
LOG(error) << "parse " << SettingsFilePath << " failed.";
|
||
|
}
|
||
|
LOG(info) << "image format: " << m_imageFormat << ", quality: " << m_imageQuality;
|
||
|
}
|
||
|
|
||
|
ImageFormat Settings::imageFormat() const {
|
||
|
return m_imageFormat;
|
||
|
}
|
||
|
|
||
|
int Settings::imageQuality() const {
|
||
|
return m_imageQuality;
|
||
|
}
|