C++传输图片给服务端

  1. C++用curl来发送编码base64的图片给服务端
//    post请求 
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <curl/curl.h>
#include<iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

std::string ZBase64(const unsigned char* Data, int DataByte)
{

    //编码表
    const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    //返回值
    std::string strEncode;
    unsigned char Tmp[4]={0};
    int LineLength=0;
    for(int i=0;i<(int)(DataByte / 3);i++)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        Tmp[3] = *Data++;
        strEncode+= EncodeTable[Tmp[1] >> 2];
        strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
        strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
        strEncode+= EncodeTable[Tmp[3] & 0x3F];
        if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}
    }
    //对剩余数据进行编码
    int Mod=DataByte % 3;
    if(Mod==1)
    {
        Tmp[1] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];
        strEncode+= "==";
    }
    else if(Mod==2)
    {
        Tmp[1] = *Data++;
        Tmp[2] = *Data++;
        strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];
        strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
        strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];
        strEncode+= "=";
    }
    
    return strEncode;
}


int main() {

    std::string path = "../1.jpg";
    cv::Mat img = cv::imread(path);
    std::vector<uchar> vec_image;
    std::vector<int> vec_compression;
    vec_compression.push_back(cv::IMWRITE_JPEG_QUALITY);
    vec_compression.push_back(90);
    cv::imencode(".jpg", img, vec_image, vec_compression);
    std::string imgbase64 = ZBase64(vec_image.data(), vec_image.size());
    std::string data = "image="+ imgbase64;
    // std::cout << data <<std::endl;

    //1. 创建一个curl句柄
    CURL* curl = nullptr;
    CURLcode res;
    
    //2. 初始化一个curl句柄
    curl = curl_easy_init();

    if(nullptr == curl) {
    printf("curl init error");
    return 0;
    }
    //给当前curl变成post请求
    curl_easy_setopt(curl, CURLOPT_POST, 1);

    //3. 给该句柄设定一些参数 (封装一个http请求消息)
    curl_easy_setopt(curl, CURLOPT_URL, "http://地址:7000/get_frame"); //http://www.baidu.com
    
    //给当前curl设置需要传递post数据
    // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
    // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, send_data.size());

    //4. 将curl句柄 向远程服务器 提交请求 并得到一个返回值
    res = curl_easy_perform(curl);  //阻塞等待服务器返回
    if(res != CURLE_OK) {
    printf("curl easy perform error res = %d\n", res);
    return 1;
    }
    //5. 处理服务器返回数据
    //6. 清空 释放句柄内存空间
    curl_easy_cleanup(curl);

    return 0;
}
  1. 用flask建立一个 服务器等待接收base64的图片然后存图到本地
from flask import request, Flask
import base64
import cv2
import numpy as np

app = Flask(__name__)
@app.route("/get_frame", methods=['POST','GET'])
def get_frame():

    data = request.get_json(force=True)
    print(data)
    
    img = base64.b64decode(data['image'])
    image_data = np.fromstring(img, np.uint8)
    image_data = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
    cv2.imwrite('./01.png', image_data)
    return 'coco'

if __name__ == "__main__":
    app.run("0.0.0.0", port=7000) 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值