不說別的直接上代碼:下面是.h檔案的代碼:
#pragma once
#include <QString>
#include <memory>
using namespace std;
//相機內引數
struct mInsideParam
{
double F;
double Dx;
double Dy;
double K1;
double K2;
double K3;
double B1;
double B2;
double E1;
double E2;
};
struct mOutsideParam
{
bool standcamera;//是否基準相機
int outsideparamStyle;//外參模式
float Matrix[50];//外引數
};
struct CameraSettingData
{
int m_CameraStyle;//相機型別
int m_ImageWidth;//影像寬
int m_ImageHeight;//影像高度
int m_PictureStyle;//圖片型別
QString m_CameraSN;//相機SN號
int m_TrigStyle;//觸發型別
int m_SampleStyle;//采集型別
QString m_pCameraName;//相機名稱
float m_pFocusDis;//焦距
double m_pPixelSize;//像元尺寸
bool m_pColorful;//是否彩色
mInsideParam m_pInsideParam;//內參
mOutsideParam m_pOutsideParam;//外參
};
class CameraContext
{
public:
CameraContext()
{
}
~CameraContext()
{
}
CameraSettingData m_pCSD;//相機設定資料
bool Load_Setting_File(QString FilePath);//讀取Json檔案
bool Write_CameraParam(std::shared_ptr<CameraSettingData> iParam, QString pJsonFile);//寫Json檔案
};
接下來是cpp檔案的代碼:
#include "SettingJsonReader.h"
#include <QVariant>
#include "qtextcodec.h"
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/prettywriter.h" // for stringify JSON
#include "rapidjson/filereadstream.h"
#include "rapidjson\istreamwrapper.h"
#include "rapidjson/filewritestream.h"
#include <cstdio>
#include <fstream>
#include <memory>
#include <QVector>
using namespace rapidjson;
using namespace std;
//文本決議器
struct DeviceSettingParseHandler
: public BaseReaderHandler<UTF8<>, DeviceSettingParseHandler>
{
DeviceSettingParseHandler() : objstate_(ALL_NONE), name_()
{
this->vec_CameraParam = make_shared< CameraSettingData>();
return;
}
typedef QMap<QString, QVariant> MapDef; //定義臨時鍵值對
bool StartObject()
{
this->NowObjMap = make_shared<MapDef>(); //建立一個新的臨時鍵值對
if (objstate_ == ALL_NONE)
{
//Do nothing in totalHeader
//return true;
}
this->MapStack.push_back(NowObjMap);
if (name_ == QStringLiteral("相機"))
{
objstate_ = ObjState::Camera_Parser;
return true;
}
else if (name_ == QStringLiteral("相機內參")){
objstate_ = ObjState::Camera_InsideParam_Parser;
return true;
}
else if (name_ == QStringLiteral("相機外參")){
objstate_ = ObjState::Camera_OutsideParam_Parser;
return true;
}
else {
//沒有的型別
return true;
}
}
bool EndObject(SizeType) {
//結束當前物件并且進行資料封裝
//彈出
this->NowObjMap = MapStack.last();
this->MapStack.pop_back();
switch (this->objstate_)
{
case ALL_NONE:
{
//決議完畢
break;
}
case ObjState::Camera_Parser:
{
auto &p = *this->vec_CameraParam;
p.m_CameraStyle = NowObjMap->find(QStringLiteral("相機型別")).value().toInt();
p.m_pCameraName = NowObjMap->find(QStringLiteral("相機名稱")).value().toString();
p.m_CameraSN = NowObjMap->find(QStringLiteral("相機SN")).value().toString();
p.m_pFocusDis= NowObjMap->find(QStringLiteral("焦距")).value().toDouble();
p.m_pPixelSize = NowObjMap->find(QStringLiteral("像元尺寸")).value().toDouble();
p.m_ImageWidth = NowObjMap->find(QStringLiteral("幅面寬度")).value().toDouble();
p.m_ImageHeight = NowObjMap->find(QStringLiteral("幅面高度")).value().toDouble();
p.m_pColorful = NowObjMap->find(QStringLiteral("是否彩色")).value().toBool();
p.m_PictureStyle = NowObjMap->find(QStringLiteral("圖片型別")).value().toInt();
p.m_TrigStyle = NowObjMap->find(QStringLiteral("觸發型別")).value().toInt();
p.m_SampleStyle = NowObjMap->find(QStringLiteral("采集型別")).value().toInt();
this->objstate_ = ObjState::ALL_NONE;
break;
}
case ObjState::Camera_InsideParam_Parser:
{
auto &p = this->vec_CameraParam->m_pInsideParam;
p.F = NowObjMap->find(QStringLiteral("F")).value().toDouble();
p.Dx = NowObjMap->find(QStringLiteral("Dx")).value().toDouble();
p.Dy = NowObjMap->find(QStringLiteral("Dy")).value().toDouble();
p.K1 = NowObjMap->find(QStringLiteral("K1")).value().toDouble();
p.K2 = NowObjMap->find(QStringLiteral("K2")).value().toDouble();
p.K3 = NowObjMap->find(QStringLiteral("K3")).value().toDouble();
p.B1 = NowObjMap->find(QStringLiteral("B1")).value().toDouble();
p.B2 = NowObjMap->find(QStringLiteral("B2")).value().toDouble();
p.E1 = NowObjMap->find(QStringLiteral("E1")).value().toDouble();
p.E2 = NowObjMap->find(QStringLiteral("E2")).value().toDouble();
//this->objstate_ = ObjState::Camera_Parser;
break;
}
case ObjState::Camera_OutsideParam_Parser:
{
auto &p = this->vec_CameraParam->m_pOutsideParam;
p.outsideparamStyle = NowObjMap->find(QStringLiteral("外參模式")).value().toInt();
p.standcamera = NowObjMap->find(QStringLiteral("是否基準相機")).value().toBool();
this->objstate_ = ObjState::Camera_Parser;
break;
}
default:
{
break;
}
}
return true;
}
bool Key(const char* str, SizeType length, bool copy){
name_ = QString::fromLocal8Bit(str,length);
return true;
}
bool StartArray(){
//清空陣列堆疊
this->mArrayValue.clear();
return true;
}
bool EndArray(SizeType elementCount){
//直接就搞起來了
switch (objstate_)
{
case Camera_OutsideParam_Parser:
memcpy_s(this->vec_CameraParam->m_pOutsideParam.Matrix, sizeof(float) * 16,
this->mArrayValue.data(), sizeof(float) *this->mArrayValue.length());
break;
default:
break;
}
return true;
}
bool Bool(bool b){
this->NowObjMap->insert(name_, b);
return true;
}
bool Int(int i){
this->NowObjMap->insert(name_, i);
return true;
}
bool Uint(unsigned i){
this->NowObjMap->insert(name_, i);
return true;
}
bool Double(double d){
switch (objstate_)
{
case Camera_OutsideParam_Parser:
this->mArrayValue.push_back(d);
break;
default:
this->NowObjMap->insert(name_, d);
break;
}
return true;
}
bool String(const char* str, SizeType length, bool) {
QString vV = QString::fromLocal8Bit(str, length);
this->NowObjMap->insert(name_, vV);
return true;
}
bool Default() {
return false; } // All other events are invalid.
//判斷當前決議物件 JSON Object
enum ObjState {
ALL_NONE, //剛開始
Camera_Parser, //相機
Camera_InsideParam_Parser, //相機內參
Camera_OutsideParam_Parser, //相機外參
}objstate_;
QString name_;
QVector<float> mArrayValue;
shared_ptr< MapDef> NowObjMap;
QVector<std::shared_ptr<MapDef>> MapStack;
std::shared_ptr<CameraSettingData> vec_CameraParam; //相機屬性
};
bool CameraContext::Load_Setting_File(QString FilePath)
{
using namespace rapidjson;
using namespace std;
try{
ifstream ifs(FilePath.toLocal8Bit().data());
DeviceSettingParseHandler handler;
Reader reader;
IStreamWrapper isw(ifs);
if (reader.Parse(isw, handler)){
//讀取成功
}
else{
//讀取失敗
}
this->m_pCSD = *handler.vec_CameraParam;
ifs.close();
return true;
}
catch (...){
//LOGE(QStringLiteral("讀取組態檔出錯").toLocal8Bit().data());
return false;
}
}
bool CameraContext::Write_CameraParam(std::shared_ptr<CameraSettingData> iParam,QString pJsonFile)//PrettyWriter<StringBuffer>& W,
{
StringBuffer s;
PrettyWriter<StringBuffer> W(s);
W.StartObject();
W.Key("相機");
{
W.StartObject();
W.Key("相機型別");
W.Int(iParam->m_CameraStyle);
W.Key("相機名稱");
W.String(iParam->m_pCameraName.toLocal8Bit());
W.Key("相機SN");
W.String(iParam->m_CameraSN.toLocal8Bit());
W.Key("焦距");
W.Double(iParam->m_pFocusDis);
W.Key("像元尺寸");
W.Double(iParam->m_pPixelSize);
W.Key("幅面寬度");
W.Double(iParam->m_ImageWidth);
W.Key("幅面高度");
W.Double(iParam->m_ImageHeight);
W.Key("圖片型別");
W.Int(iParam->m_PictureStyle);
W.Key("觸發型別");
W.Int(iParam->m_TrigStyle);
W.Key("采集型別");
W.Int(iParam->m_SampleStyle);
W.Key("是否彩色");
W.Bool(iParam->m_pColorful);
//相機內參
W.Key("相機內參");
{
W.StartObject();
auto& NC = iParam->m_pInsideParam;
W.Key("F");
W.Double(NC.F);
W.Key("Dx");
W.Double(NC.Dx);
W.Key("Dy");
W.Double(NC.Dy);
W.Key("K1");
W.Double(NC.K1);
W.Key("K2");
W.Double(NC.K2);
W.Key("K3");
W.Double(NC.K3);
W.Key("B1");
W.Double(NC.B1);
W.Key("B2");
W.Double(NC.B2);
W.Key("E1");
W.Double(NC.E1);
W.Key("E2");
W.Double(NC.E2);
W.EndObject();
}
//相機外參
W.Key("相機外參");
{
W.StartObject();
auto& WC = iParam->m_pOutsideParam;
W.Key("是否基準相機");
W.Bool(WC.standcamera);
W.Key("外參模式");
W.Int(WC.outsideparamStyle);
W.Key("外引數");
W.StartArray();
for (int i = 0; i < 16; i++){
W.Double(WC.Matrix[i]);
}
W.EndArray();
W.EndObject();
}
W.EndObject();
}
W.EndObject();
ofstream ofs; //打開檔案用于寫,若檔案不存在就創建它
locale loc = locale::global(locale("")); //要打開的檔案路徑含中文,設定全域locale為本地環境
char* ch;
QByteArray ba = pJsonFile.toLatin1(); // must
ch = ba.data();
ofs.open(ch, ios::out | ios::trunc, _SH_DENYNO); //輸出到檔案 ,清除方式,二進制。 可同時用其他的工具打開此檔案
locale::global(loc); //恢復全域locale
if (!ofs.is_open())
return false; //打開檔案失敗則結束運行
ofs << s.GetString();
ofs.close();
return true;
};
測驗代碼:
#include "RapidJsonWR.h"
#include <QtWidgets/QApplication>
#include "QRapidjsonWR.h"
#include "SettingJsonReader.h"
#include <memory>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//QRapidjsonWR PTEST;
//PTEST.read("C:\\Users\\DELL\\Desktop\\HKCameraSet.json");
//string ppp = PTEST.Find("相機SN");
CameraContext pCC;
pCC.Load_Setting_File("C:\\Users\\DELL\\Desktop\\HKCameraSet.json");
pCC.Write_CameraParam(std::make_shared<CameraSettingData>(pCC.m_pCSD), "C:\\Users\\DELL\\Desktop\\HKCameraSet22.json");
//RapidJsonWR w;
//w.show();
return a.exec();
}
我的Json檔案:
{
"相機": {
"相機型別": 16843009,
"相機名稱": "",
"相機SN": "22191954",
"焦距": 12.0,
"像元尺寸": 5.449999809265137,
"幅面寬度": 1920.0,
"幅面高度": 1200.0,
"圖片型別": 23,
"觸發型別": 1,
"采集型別": 2,
"是否彩色": false,
"相機內參": {
"F": 2109.07177734375,
"Dx": -1.106784701347351,
"Dy": 21.3510379791259
uj5u.com熱心網友回復:
這貼不是問題,不用回復,謝謝,第一次玩CSDN,不會用
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/56194.html
標籤:Qt
上一篇:小白求問
下一篇:xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0
