目录
第一次编译并没有与翔云后台产生连接,问题出在OpenSSL,编译出来仅仅支持http但不支持https,而s的加密内容就是key和secret
解决方案:重装libcurl库,带ssl
./configure --prefix=$PWD_INSTALL --with-ssl
./configure --prefix=$PWD_INSTALL --host=arm-linux // 树莓派交叉编译
当你的本机没有ssl库时,会出现报错
configure: error: --with-ssl was given but OpenSSL could not be detected
openssl version //查看版本
which openss //查看路径
这时候需要安装OpenSSL库解决这个问题
wget http://www.openssl.org/source/openssl-1.1.1a.tar.gz
tar -vxf openssl-1.0.0d.tar.gz //解压
cd openssl-1.1.1a/
./config
需要添加图片以字符串形式,图片的base64流
图片是以RGB组成的,而jpg是由bmp压缩过后形成的
如何将jpg图片格式转换为base64数据并且读入
system("base64 dong2.jpg > tmpFile2"); //系统操作base64
int fd2 = open("./tmpFile2",O_RDWR); // 打开文件读取
int filelen2 = lseek(fd2,0,SEEK_END); //读取有多少大小
lseek(fd,0,SEEK_SET); //光标回到开头
char *bufpic2 = (char *)malloc(filelen2+2); //定义一个buf存放数据流
memset(bufpic2,0,filelen2+2); //清空
read(fd,bufpic2,filelen2); //读取内容到buf
close(fd2); //关闭
int len = strlen(key)+strlen(secret)+strlen(bufpic1)+strlen(bufpic2)+128;//定义一个len用来表示大小,很好用
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);//拼接
封装后
char *getPicBase64FromFile(char *filePath)
{
char cmd[32]={'\0'};
sprintf(cmd,"base64 %s > tmpFile",filePath);
system(cmd);
int fd = open("./tmpFile",O_RDWR);
int filelen = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
char *bufpic = (char *)malloc(filelen+2);
memset(bufpic,'\0',filelen+2);
read(fd,bufpic,filelen);
close(fd);
return bufpic
}
char *bufpic1 = getPicBase64FromFile("dong1.jpg");
char *bufpic2 = getPicBase64FromFile("dong2.jpg");
http初始化工作
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); //函数将在libcurl接收到数据后被调用
res = curl_easy_perform(curl);
printf("ok %d\n",res);
if(strstr(readBuf,"是")!= NULL){
printf("the same person\n");
}else{
printf("different person\n");
}
curl_easy_cleanup(curl);
}