http发送和接收图片json文件

news/2025/2/22 7:47:20/

 一、http数据发送

1、先将图片转换为base64格式

std::string detectNet::Mat2Base64(const cv::Mat &image, std::string imgType){std::vector<uchar> buf;cv::imencode(imgType, image, buf);//uchar *enc_msg = reinterpret_cast<unsigned char*>(buf.data());std::string img_data = base64_encode(buf.data(), buf.size(), false);return img_data;
}

2、将数据以json格式进行发送

void detectNet::send_json_people(cv::Mat img, std::string label, std::string level, std::string rtsp){std::string out = Mat2Base64(img,".jpg");//std::cout << out << std::endl;ImgInfo imgInfo(out, label, level, rtsp);static auto client = httplib::Client("127.0.0.1", 18080);auto result = client.Post("/uploadAlgorithmResult", imgInfo.to_json_people(), "application/json");if (result != nullptr && result->status == 200) {std::cout << result->body << std::endl;}
}

其中 ImgInfo 类为:

#ifndef HTTP_DEMO_IMGINFO_H
#define HTTP_DEMO_IMGINFO_H#include <utility>#include "cJSON.h"
#include "base64.h"#define RTSP_URL "rtsp://admin:a123456789@192.168.8.31:554/h264/ch1/main/av_stream/1"
#define AlgorithmType "hook_detection"
#define AlgorithmType_people "danger_zone"
#define AlgorithmType_crooked "crooked"class ImgInfo {
private:std::string img;std::string label;std::string rtsp;std::string level;
public:ImgInfo(std::string img, std::string label, std::string level, std::string rtsp) : img(std::move(img)), label(std::move(label)),level(std::move(level)), rtsp(std::move(rtsp)) {}std::string to_json_people() {auto *root = cJSON_CreateObject();cJSON_AddStringToObject(root, "image", img.c_str());cJSON_AddStringToObject(root, "level", level.c_str());cJSON_AddStringToObject(root, "rtsp", rtsp.c_str());cJSON_AddStringToObject(root, "type", AlgorithmType_people);cJSON_AddStringToObject(root, "label", label.c_str());/*cJSON *label_array = cJSON_CreateArray();for (auto &i: label) {cJSON_AddItemToArray(label_array, cJSON_CreateString(i.c_str()));}cJSON_AddItemToObject(root, "label", label_array);*/char *out = cJSON_Print(root);std::string res = out;free(out);cJSON_Delete(root);return res;}
};#endif //HTTP_DEMO_IMGINFO_H

上述代码中json数据有五个部分:image为图片数据,level是告警等级,rtsp为数据流地址,type是算法类型,label是算法标签等,所以数据发送为这五个内容。

二、http数据接收

HttpServer.cpp如下: 

//#include <QFile>
//#include <FileUtils.hpp>
#include "HttpServer.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
//#include "QString"
//#include "TimeUtils.hpp"
//#include "AlgorithmCommon.h"
//#include "QJsonObject"
//#include "QJsonDocument"
//#include "QJsonArray"
//#include "httplib.h"//#include "log.h"
//#include "cJSON.h"HttpServer::HttpServer(std::string ip, int port) : server_ip(ip), server_port(port) {//event_handler = new AlgorithmAlarmHandler();}HttpServer::~HttpServer() {stopListen();if (read_thd.joinable()) {read_thd.join();}
}void HttpServer::startListen() {read_thd = std::thread([=]() {serverListen();});//event_handler->startHandlerThread();
}void HttpServer::stopListen() {if (server.is_running()) {server.stop();}
}void HttpServer::serverListen() {if (!server.is_valid()) {std::cout << "http server invalid" << std::endl;return;}// 服务器状态server.Get("/server/status", [&](const Request &req, Response &res) {onServerStatus(req, res);});// 上传算法结果server.Post("/uploadAlgorithmResult", [&](const Request &req, Response &res) {onUploadAlgorithmResult(req, res);});// 错误处理server.set_error_handler([&](const Request &req, Response &res) {onErrorHandle(req, res);});//qDebug() << "server start listen";server.listen(server_ip.c_str(), server_port);
}void HttpServer::onServerStatus(const httplib::Request &req, httplib::Response &res) {res.body = R"({"code": 200,"msg": "Server is already Running"})";
}void HttpServer::onUploadAlgorithmResult(const httplib::Request &req, httplib::Response &res) {std::string content_type = httputillib::GetContentType(req.headers);//if (content_type != "application/json") {//     qDebug() << "contentType 异常, content_type:" << QString::fromStdString(content_type);//}bool parseRet = parseAlgorithmResult(req.body);//bool parseRet = true;std::string rspMsg;if (parseRet) {rspMsg = std::string(R"({"msg":"Recv Success, Parse Success."})");} else {rspMsg = std::string(R"({"msg":"Recv Success, Parse Failed."})");}res.body = std::move(rspMsg);
}// 错误请求处理
void HttpServer::onErrorHandle(const httplib::Request &req, httplib::Response &res) {const char *fmt = {"\"error\": \"服务器不支持该方法\""};char buf[BUFSIZ] = {0};snprintf(buf, sizeof(buf), fmt, res.status);res.set_content(buf, "application/json");
}bool HttpServer::parseAlgorithmResult(const std::string &body) {//std::cout << "body:" << body << std::endl;Json::Reader reader;Json::Value root;//std::ifstream in(body, std::ios::binary);//if (!in.is_open()) {//    std::cout << "open file failed" << std::endl;//    return false;//}if (!reader.parse(body, root)) {std::cout << "parse failed" << std::endl;return false;}std::string rtsp = root["rtsp"].asString();std::cout << "rtsp:" << rtsp << std::endl;std::string level = root["level"].asString();std::cout << "level:" << level << std::endl;std::string type = root["type"].asString();std::cout << "type:" << type << std::endl;std::string image = root["image"].asString();//std::cout << "image:" << image << std::endl;std::string out = base64_decode(image);std::string decoded_jpeg = std::move(out);//std::cout << "decoded_jpeg: " << decoded_jpeg << std::endl;cv::Mat mat2(1, decoded_jpeg.size(), CV_8U, (char*)decoded_jpeg.data());cv::Mat dst = cv::imdecode(mat2, CV_LOAD_IMAGE_COLOR);cv::imwrite(rtsp.substr(1) + ".jpg", dst);  return true;
}

 HttpServer.h如下:

//#ifndef CPPHTTPSERVER_HTTPSERVER_H
//#define CPPHTTPSERVER_HTTPSERVER_H#include "httputillib.h"
#include "httplib.h"
#include "base64.h"
//#include "thread"
#include <thread>
#include <time.h>#include <json.h>class HttpServer{
public:HttpServer(std::string ip, int port);~HttpServer();void startListen();void stopListen();private:void serverListen();// 回调函数: 错误请求处理void onErrorHandle(const httplib::Request &req, httplib::Response &res);// 回调函数: 获取服务器状态void onServerStatus(const httplib::Request &req, httplib::Response &res);// 回调函数: 上传算法结果void onUploadAlgorithmResult(const httplib::Request &req, httplib::Response &res);// 回调函数:处理算法数据bool parseAlgorithmResult(const std::string &body);std::string server_ip;int server_port;Server server;std::thread read_thd;};//#endif //CPPHTTPLIB_HTTPLIB_H

httputillib.h如下:

#include <httplib.h>using namespace httplib;namespace httputillib {
// 打印请求头
static std::string DumpHeaders(const Headers &headers) {std::string s;char buf[BUFSIZ] = {0};for (auto it = headers.begin(); it != headers.end(); ++it) {const auto &x = *it;snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());s += buf;}return s;
}// 从请求同获取content type
static std::string GetContentType(const httplib::Headers &headers) {auto iter = headers.find("Content-Type");if (iter == headers.end()) {return std::string();}return iter->second;
}};

上述完整代码可详见github或者百度网盘


http://www.ppmy.cn/news/1140721.html

相关文章

[NOIP2010 提高组] 机器翻译

[NOIP2010 提高组] 机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件&#xff0c;他经常用这个软件来翻译英语文章。 题目描述 这个翻译软件的原理很简单&#xff0c;它只是从头到尾&#xff0c;依次将每个英文单词用对应的中文含义来替换。对于每个英文单词&#xf…

js 封装一个异步任务函数

// 异步任务 封装 // 1,定义函数 // 2&#xff0c;使用核心api(queueMicrotask,MutationObserver,setTimeout) function runAsynctask (callback){if(typeof queueMicrotask "function" ){queueMicrotask(callback)}else if( typeof MutationObserver "functio…

分布式软件架构——服务端缓存的三种属性

服务端缓存 在透明多级分流系统中&#xff0c;我们以流量从客户端中发出开始&#xff0c;以流量到达服务器集群中真正处理业务的节点结束。一起探索了在这个过程中与业务无关的一些通用组件&#xff0c;包括DNS、CDN、客户端缓存&#xff0c;等等。 实际上&#xff0c;服务端缓…

鼠标灵敏度怎么调?4个方法提高使用体验感!

“不知道怎么回事&#xff0c;我的鼠标反应很慢慢&#xff0c;有时候好像会有延迟。使用起来感觉特别不舒服。这个问题应该怎么解决呢&#xff1f;希望大家帮帮我&#xff01;” 在使用电脑的过程中&#xff0c;鼠标是很常用的一个工具。调整鼠标的灵敏度对电脑的使用体验会有显…

人体姿态标注

人体姿态标注 一 标注工具labelme1.1 安装方式1.2 界面说明 二 数据集准备以下每张图片的命名方式&#xff1a;状态_学号_序号.jpg (注意 一定是jpg格式) 保存到一个文件夹中&#xff0c;便于标注。 例如&#xff1a;FALL_0000_0001.jpg 站立数据(UP)&#xff1a;不同方向&…

高压放大器的主要技术指标是什么

高压放大器是一种特殊类型的放大器&#xff0c;主要用于将低电压信号放大到高电压水平。它在许多领域中具有重要的应用&#xff0c;如医学成像、科学研究、工业设备等。为了正确选择和评估高压放大器&#xff0c;我们需要了解其主要技术指标。以下是高压放大器的主要技术指标的…

【初识C语言】数据类型

char 字符数据类型 short 短整型 int 整形 long 长整型 longlong 更长的整形 float 单精度浮点数 double 双精度浮点数 每种类型的大小是多少&#xff1f;

Gitlab+Jenkins自动化部署,解放双手

项目打包 ​ 在部署项目前需要对源码进行打包&#xff0c;一个简单的SpringBoot项目默认是打包为jar包&#xff0c;也就是在pom.xml中的<packaging>jar</packaging>方式&#xff0c;当然也会有一些打包成war包方式&#xff0c;使用外置的Tomcat应用服务器部署war包…