功能库及其作用

记录几个库,以后有用的时候直接拿来用

gflags

gflags 是 google 开源的用于处理命令行参数的项目。

#include <iostream>

#include <gflags/gflags.h>

/**
 *  定义命令行参数变量
 *  默认的主机地址为 127.0.0.1,变量解释为 'the server host'
 *  默认的端口为 12306,变量解释为 'the server port'
 */
DEFINE_string(host, "127.0.0.1", "the server host");
DEFINE_int32(port, 12306, "the server port");

int main(int argc, char** argv) {
    // 解析命令行参数,一般都放在 main 函数中开始位置
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    // 访问参数变量,加上 FLAGS_
    std::cout << "The server host is: " << FLAGS_host
        << ", the server port is: " << FLAGS_port << std::endl;
    return 0;
}

gflags 一共支持 5 种类型的命令行参数定义:

DEFINE_bool: 布尔类型
DEFINE_int32: 32 位整数
DEFINE_int64: 64 位整数
DEFINE_uint64: 无符号 64 位整数
DEFINE_double: 浮点类型 double
DEFINE_string: C++ string 类型

glog

glog是一个轻量、稳定、开源的日志系统

jsoncpp

jsoncpp是一个C++端处理json文件的库.
使用方法:

#include"json/json.h"
//json转字符
string post::Json2String(Json::Value root)
{
    Json::FastWriter writer;
    string data = writer.write(root);
    return data;
}
int main()
{
    Json::Value root;
    root["key"] = "value";
}

代码地址:https://download.csdn.net/download/san_junipero/10437746

curl

curl是一个发送接受请求的库.

//命令行用法:
curl -X GET --header 'Accept: application/json' --header 'Content-Type:application/json' -d '{"a":b}' 'http://www.baidu.com'
//C++用法
#include<curl/curl.h>
void post_info(string ip,string port,string data)
{
    string addr = "http://"+ip+":"+port+"/";
    try
    {
        CURL *pCurl = NULL;
        CURLcode res;
        // In windows, this will init the winsock stuff
        curl_global_init(CURL_GLOBAL_ALL);

        // get a curl handle
        pCurl = curl_easy_init();
        if (NULL != pCurl)
        {
            // 设置超时时间为10秒
            curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 10);

            //设置发送到的地址
            curl_easy_setopt(pCurl, CURLOPT_URL, addr.c_str());

            //设置header
            struct curl_slist *headers = NULL;
            headers = curl_slist_append(headers,"Content-Type:application/json;charset=UTF-8");
            headers = curl_slist_append(headers,"Authorization: token");        
            curl_easy_setopt(pCurl,CURLOPT_HTTPHEADER,headers);

            // 设置请求方式为POST  设置提交的JSON数据
            curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, data.c_str());

            // 提交请求,res是返回码
            res = curl_easy_perform(pCurl);

            // 查看错误
            if (res != CURLE_OK)
            {
                printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));
            }

            // 必须要cleanup
            curl_easy_cleanup(pCurl);
        }
        curl_global_cleanup();
    }
    catch (std::exception &ex)
    {
        printf("curl exception %s.\n", ex.what());
    }
}

base64

将图片转换为base64字符串
使用方法

#include<opencv2/opencv.hpp>
#include<iostream>
#include"zbase64.h"

string jpg2base64(Mat img)
{
    vector<uchar> vecImg;                               //Mat 图片数据转换为vector<uchar>
    vector<int> vecCompression_params;
    vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    vecCompression_params.push_back(90);
    imencode(".jpg", img, vecImg, vecCompression_params);  //图片格式,此处为jpg
    ZBase64 base64;
    string imgbase64 = base64.Encode(vecImg.data(), vecImg.size());     //实现图片的base64编码
    return imgbase64;
}
int main()
{
    Mat img = imread("/home/emily/Pictures/Selection_012.png");
    string imgbase64 = p.jpg2base64(img);
}

代码地址:https://download.csdn.net/download/san_junipero/10437755

猜你喜欢

转载自blog.csdn.net/San_Junipero/article/details/80447996