1.翔云人脸识别的API
1.1 图片img上传是以base64流的形式上传
百度百科
图片是由无数个"小色块’组成每个小色块都是由红绿蓝来表示
#ff9899…
这样图片就能用字符串表示
Linux自带base64工具
直接base64 xxx.jpg 就能看到该图片的64流编码
程序里的代码
system("base64 reba1.jpg > tmpFile");
把生成的编码传到tmpFile
2.代码
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef unsigned int bool;
#define true 1
#define false 0
char buf[1024] = {'\0'};
size_t readData( void *ptr, size_t size,size_t nmemb, void *stream)
{
strncpy(buf,ptr,1024);
// printf("==================get data================\n");
printf("%s\n",buf );
}
char* getPicBase64FromFile(char *filePath)
{
char *bufPic;
char cmd[128] = {'\0'};
sprintf(cmd,"base64 %s > tmpFile",filePath);
system(cmd);
int fd = open("./tmpFile",O_RDWR);
int fileLen = lseek(fd,0,SEEK_END)+2;
lseek(fd,0,SEEK_SET);
bufPic = (char *)malloc(fileLen);
memset(bufPic,'\0',fileLen);
read(fd,bufPic,fileLen);
close(fd);
system("rm -f tmpFile");
return bufPic;
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char *key = "5Fr48MNMbvTMDbjhD4****";
char *secret = "f6b80478f43148c9bf4de0c9b9ec****";
int typeId = 21;
char *format = "xml";
char *bufPic1 = getPicBase64FromFile("reba1.jpg");
char *bufPic2 = getPicBase64FromFile("reba2.jpg");
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,typeId,format);
curl = curl_easy_init();
if (curl)
{
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);
res = curl_easy_perform(curl);
if(strstr(buf,"是") != NULL){
printf("the same person\n");
}else{
printf("different person\n");
}
printf("Return:%d\n",res);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
遇到过的报错
1.产品类型错误
原因:typeId没有严格按照网页的填写,我写成了typeID
出错代码
sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeID=%d&format=%s","bufPic1","bufPic2",key,secret,typeId,format);