国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

http發(fā)送和接收?qǐng)D片json文件

這篇具有很好參考價(jià)值的文章主要介紹了http發(fā)送和接收?qǐng)D片json文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

?一、http數(shù)據(jù)發(fā)送

1、先將圖片轉(zhuǎn)換為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、將數(shù)據(jù)以json格式進(jìn)行發(fā)送

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數(shù)據(jù)有五個(gè)部分:image為圖片數(shù)據(jù),level是告警等級(jí),rtsp為數(shù)據(jù)流地址,type是算法類型,label是算法標(biāo)簽等,所以數(shù)據(jù)發(fā)送為這五個(gè)內(nèi)容。

二、http數(shù)據(jù)接收

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;
    }

    // 服務(wù)器狀態(tài)
    server.Get("/server/status", [&](const Request &req, Response &res) {
        onServerStatus(req, res);
    });

    // 上傳算法結(jié)果
    server.Post("/uploadAlgorithmResult", [&](const Request &req, Response &res) {
        onUploadAlgorithmResult(req, res);
    });


    // 錯(cuò)誤處理
    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);
}


// 錯(cuò)誤請(qǐng)求處理
void HttpServer::onErrorHandle(const httplib::Request &req, httplib::Response &res) {
    const char *fmt = {"\"error\": \"服務(wù)器不支持該方法\""};
    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();

    // 回調(diào)函數(shù): 錯(cuò)誤請(qǐng)求處理
    void onErrorHandle(const httplib::Request &req, httplib::Response &res);

    // 回調(diào)函數(shù): 獲取服務(wù)器狀態(tài)
    void onServerStatus(const httplib::Request &req, httplib::Response &res);

    // 回調(diào)函數(shù): 上傳算法結(jié)果
    void onUploadAlgorithmResult(const httplib::Request &req, httplib::Response &res);

    // 回調(diào)函數(shù):處理算法數(shù)據(jù)
    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 {
// 打印請(qǐng)求頭
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;
}

// 從請(qǐng)求同獲取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或者百度網(wǎng)盤文章來源地址http://www.zghlxwxcb.cn/news/detail-727064.html

到了這里,關(guān)于http發(fā)送和接收?qǐng)D片json文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • C/C++ 發(fā)送與接收HTTP/S請(qǐng)求

    C/C++ 發(fā)送與接收HTTP/S請(qǐng)求

    HTTP(Hypertext Transfer Protocol)是一種用于傳輸超文本的協(xié)議。它是一種無狀態(tài)的、應(yīng)用層的協(xié)議,用于在計(jì)算機(jī)之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 服務(wù)器之間進(jìn)行數(shù)據(jù)通信。HTTP 是由互聯(lián)網(wǎng)工程任務(wù)組(IETF)定義的,它是基于客戶端-服務(wù)器模型的協(xié)議,其中客戶端

    2024年02月05日
    瀏覽(25)
  • vue使用axios發(fā)送post請(qǐng)求攜帶json body參數(shù),后端使用@RequestBody進(jìn)行接收

    vue使用axios發(fā)送post請(qǐng)求攜帶json body參數(shù),后端使用@RequestBody進(jìn)行接收

    最近在做自己項(xiàng)目中,做一個(gè)非常簡(jiǎn)單的新增用戶場(chǎng)景,但是使用原生axios發(fā)送post請(qǐng)求的時(shí)候,還是踩了不少坑的。 唉,說多了都是淚,小小一個(gè)新增業(yè)務(wù),在自己前后端一起開發(fā)的時(shí)候,硬是搞了好久。 下面就把問題總結(jié)分享下,防止后人再踩坑。 首先先看下我的接口定

    2024年02月02日
    瀏覽(24)
  • .net6 接收json數(shù)據(jù) Controller http post

    .net6 接收json數(shù)據(jù) Controller http post

    .net6 接收json數(shù)據(jù) Controller http post 要添加這兩個(gè)包 前端ajax請(qǐng)求 關(guān)鍵在contentType 和JSON.stringify 如果這2兩個(gè)沒加上后臺(tái)還是接收不到的! contentType: “application/json”, 后臺(tái)接收加上一個(gè) [FromBody] 后臺(tái)示例 后臺(tái)完整代碼

    2024年02月05日
    瀏覽(23)
  • MFC發(fā)送http https以及json解析

    請(qǐng)求三部曲:

    2024年02月05日
    瀏覽(18)
  • Qt 使用HTTP請(qǐng)求網(wǎng)絡(luò)API并接收返回的JSON格式的數(shù)據(jù)

    引入網(wǎng)絡(luò)模塊: mainwindow.h: mainwindow.cpp:

    2024年02月13日
    瀏覽(20)
  • java http get post 和 發(fā)送json數(shù)據(jù)請(qǐng)求

    java http get post 和 發(fā)送json數(shù)據(jù)請(qǐng)求

    瀏覽器請(qǐng)求效果 ? ? ? main調(diào)用 ?

    2024年02月16日
    瀏覽(32)
  • postman如何發(fā)送json請(qǐng)求其中file字段是一個(gè)圖片

    在Postman中發(fā)送一個(gè)包含文件(如圖片)的JSON請(qǐng)求通常意味著你需要發(fā)送一個(gè)multipart/form-data請(qǐng)求。因?yàn)樵贘SON中直接嵌入二進(jìn)制文件數(shù)據(jù)(如圖片)通常不是一個(gè)有效的做法。下面是如何在Postman中發(fā)送這樣的請(qǐng)求的步驟: 打開Postman并創(chuàng)建一個(gè)新的請(qǐng)求 。 設(shè)置請(qǐng)求類型為 PO

    2024年04月28日
    瀏覽(16)
  • FormData異步發(fā)送文件,后端SpringBoot接收

    FormData異步發(fā)送文件,后端SpringBoot接收

    平時(shí)我們用表單提交數(shù)據(jù)時(shí),所有的數(shù)據(jù)都是以鍵值對(duì)的形式提交給后端的,對(duì)于文件二進(jìn)制流數(shù)據(jù)也是以鍵值對(duì)提交的,只是說此時(shí)值的內(nèi)容是二進(jìn)制數(shù)據(jù)罷了。如果我們想給后端上傳文件,一般都是利用表單里的File控件去提交的,但這時(shí)候有一個(gè)問題,這種上傳方式不是

    2024年02月15日
    瀏覽(18)
  • Go語言項(xiàng)目后端使用gin框架接收前端發(fā)送的三種格式數(shù)據(jù)(form-data,json,Params)

    Go語言項(xiàng)目后端使用gin框架接收前端發(fā)送的三種格式數(shù)據(jù)(form-data,json,Params)

    使用gin框架的BindJSON方法,將前端的json格式數(shù)據(jù)將后端的結(jié)構(gòu)體相綁定,從而獲取到前端所發(fā)送的數(shù)據(jù),并返回給前端 1.將前端發(fā)送過來的數(shù)據(jù)全部返回 2.將前端發(fā)送過來的json格式數(shù)據(jù)選擇性返回 ? 使用gin框架的PostForm方法,從而獲取到前端form格式的參數(shù) 使用gin框架中的

    2024年02月01日
    瀏覽(436)
  • C# 使用Http Post方式發(fā)送Json數(shù)據(jù),只需二步。

    一.先在工程增加 RestClient.cs類 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; namespace CM2.CentreWin { class RestClient { private System.Net.CookieContainer Cookies = new System.Net.CookieContainer(); priv

    2024年02月09日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包