使用libcurl库编程实现人脸识别和base64的基本认识

还是使用翔云平台来实现人脸识别方案。

使用libcurl库编程实现人脸识别和base64的基本认识_第1张图片
API调用的要求:
①POST请求
②url地址(上图中的接口地址)
③访问接口需要的参数
④识别的图片为base64流

编程curl_easy_setopt函数post请求所需要的参数:
CURLOPT_POSTFIELDS:发送包含此数据的POST参数
CURLOPT_COOKIEFILE:读取cookie到文件中

什么是base64?

Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。

在参数传输的过程中经常遇到的一种情况:使用全英文的没问题,但一旦涉及到中文就会出现乱码情况。与此类似,网络上传输的字符并不全是可打印的字符,比如二进制文件、图片等。Base64的出现就是为了解决此问题,它是基于64个可打印的字符来表示二进制的数据的一种方法。

关于BMP,JPG,RGB的简单了解

BMP
是Windows操作系统中的标准图像文件格式,能够被多种Windows应用程序所支持.(未压缩的图片,文件太大,不利于网络传输)
RGB
对一种颜色进行编码的方法统称为“颜色空间”或“色域”。用最简单的话说,世界上任何一种颜色的“颜色空间”都可定义成一个固定的数字或变量。采用这种编码方法,每种颜色都可用红,绿,蓝三个变量来表示。
JPG
JPG是采用高压缩比技术的图像存储格式(有损压缩,文件小,利于网络传输)

人脸识别示例demo

识别图片:
使用libcurl库编程实现人脸识别和base64的基本认识_第2张图片
使用libcurl库编程实现人脸识别和base64的基本认识_第3张图片

#include 
#include
#include 
#include 
#include 
#include 
#include
#include 

#define false   0
#define true    1
typedef unsigned int bool;

char *imgToBase64(char *img)
{
        int fd;
        char cmd[128]={'\0'};
        char *buf=NULL;
        int len=0;
        fd= open("./imgToBase64.txt",O_RDWR|O_CREAT,0666);
        sprintf(cmd,"base64 %s > ./imgToBase64.txt",img);
        system(cmd);
        len=lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        buf=(char*)malloc(len+1);
        memset(buf,0,len+1);
        int n_read=read(fd,buf,len);
        close(fd);
        system("rm -f imgToBase64.txt");
        return buf;


}

ssize_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{

        printf("%s",(char*)ptr);
        return size*nmemb;
}
bool postUrl()
{
        CURL *curl;
        CURLcode res;
        int len=0;
        //post can shu 
        char * img1=imgToBase64("faker1.jpg");
        char * img2=imgToBase64("faker2.jpg");
        char * key="6AcKZAeYU6yQm2HSxYnnde2";
        char * secret="68a6b4e146624bff94f47c0f3jdsjdka";
        int typeId=21;
        char * format="xml";


        char *postString;
        len=strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+128;
        postString=(char *)malloc(len);
        memset(postString,0,len);
        sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
        img1,img2,key,secret,typeId,format);//字符串拼接

        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "./cookie.txt"); // 指定cookie文件
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // 指定post内容
                curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // 指定url
                curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
                res = curl_easy_perform(curl); // 执行
                printf("OK %d\n",res);
                curl_easy_cleanup(curl);
        }
        free(img1);
        free(img2);
        free(postString);
        return true;
}
int main(void)
{
        curl_global_init(CURL_GLOBAL_ALL);
        postUrl();
        curl_global_cleanup();
        return 0;
}

使用libcurl库编程实现人脸识别和base64的基本认识_第4张图片

你可能感兴趣的:(#,linux,网络协议,c语言)