linux(ubuntu)人脸图片识别配置与实现

这段代码展示了如何使用CURL库在C语言中实现图片的base64编码转换,并将两个图片的base64数据通过POST请求发送到特定API(https://netocr.com/api/faceliu.do)。代码首先读取两个图片文件,然后拼接成一个POST字符串,包含关键参数如key和secret。最后,它执行POST请求并将响应数据打印出来。
摘要由CSDN通过智能技术生成

代码分析:

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define true 1
#define false 0

typedef unsigned int bool;

size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)
{
        char buf[10240] = {'\0'};
        strncpy(buf, ptr ,10240);
        printf("===================================get Data=====================\n");
        printf("%s\n",buf);
}


bool postUrl()
{
        CURL *curl;
        CURLcode res;
        char *postString=NULL;

        char *img1[12];
        char *img2[12];
        char *key = "7AVtT3g3TRKnMPXGkNALGA";
        char *secret = "3d2252223b274c429d99ac7a5b5b6482";
        int typeId = 21;
        char *format = "xml";
        system("base64 1.jpg > photo3");  //将base64生成的文件信息导到photo3文件中来
        int fd = open("./photo3",O_RDWR);//打开photo3文件
        int filelen = lseek(fd, 0, SEEK_END);
        lseek(fd, 0, SEEK_SET); //计算文件都大小
        char *bufpic1 = (char *)malloc(filelen + 2);
        memset(bufpic1, 0, filelen+2);
        read(fd, bufpic1, filelen);//从文件中读取出来
        close(fd);


        system("base64 2.jpg > photo4");
        fd = open("./photo4",O_RDWR);
        filelen = lseek(fd, 0, SEEK_END);
        lseek(fd, 0, SEEK_SET);
        char *bufpic2 = (char *)malloc(filelen + 2);
        memset(bufpic2, 0, filelen+2);
        read(fd, bufpic2, filelen);
        close(fd);


        int len = strlen(key) + strlen(secret) + strlen(bufpic1) + strlen(bufpic2) + 124;//计算拼接的字符总长度
        postString = (char *)malloc(len);
        memset(postString , '\0', len);
        sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",bufpic1,bufpic2,key,secret,21,format );
       curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/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, readData); //将返回http头输出到fp指定的文件     
                res = curl_easy_perform(curl);
                printf("OK: %d\n",res);//将请求的结果打印出来
                curl_easy_cleanup(curl);
        }
        return true;
}

int main(void)
{
        //      getUrl("/tmp/get.html");
        postUrl();

        return 0;
}


                                                                                 

运行结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值