功能庫及其作用

記錄幾個庫,以後有用的時候直接拿來用

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

發佈了54 篇原創文章 · 獲贊 17 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章