翔云ocr平台需要的信息
使用post请求实现人脸识别:
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define true 1
#define false 2
typedef unsigned int bool;
char buf[1024] = {'\0'};
size_t readData( void *ptr, size_t size, size_t nmemb, void *stream)
{
char buf[1024] = {'\0'};
strcpy(buf,ptr);
printf("%s\n",buf);
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char img1[1024];
char img2[1024];
char *key = "";
char *secret = "";
int typeld = 21;
char *format = "xml";
int len = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+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,21,format);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do ");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
res = curl_easy_perform(curl);
printf("ok!%d\n",res);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
由于该方案使用的是https协议,之前编译出的libcurl不支持ssl,所以需要重新编译 删除之前编译出来的_install文件 阅读docs中INSTALL.md文件,查看编译步骤,发现如果需要支持SSL需要进行在config中加入如下选项 退出docs目录进行如下操作 但是配置后显示没有openssl库所以需要下载openssl库 读README 需要打开INSTALL 按照步骤安装,其中./config不使用–prefix,会安装到默认路径(usr/local);make test可以省略,之后的make install需要加sudo;
继续到curl文件夹中安装ssl 安装完之后在make,编译curl 安装make install 接下来就可以编译post请求的代码,并执行,由于图片没有上传所以会出现必传参数为空字样 将图片放入进去 Base64: Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。使用Base64编码具有不可读性,需要解码才能阅读。 图片可以利用LInux下base64命令转换成base64格式; 在代码中使用system将base64格式存放到临时文件中;
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define true 1
#define false 2
typedef unsigned int bool;
char buf[1024] = {'\0'};
size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)
{
strcpy(buf,ptr);
printf("%s\n",buf);
}
char* getPicBase64(char *filePath)
{
char *bufPic = NULL;
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);
lseek(fd,0,SEEK_SET);
bufPic = (char *)malloc(sizeof(char)*(fileLen+2));
memset(bufPic,'\0',fileLen+2);
read(fd,bufPic,fileLen);
close(fd);
system("rm -rf tmpfile");
return bufPic;
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char img1[1024];
char img2[1024];
char *key = "";
char *secret = "";
int typeld = 21;
char *format = "xml";
char *bufPic1 = getPicBase64("./mi.jpeg");
char *bufPic2 = getPicBase64("./mi2.jpeg");
int len = strlen(img1)+strlen(img2)+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_POSTFIELDS, postString);
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do ");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);
res = curl_easy_perform(curl);
printf("ok!%d\n",res);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}
比较两个图片利用set-opt中回调函数打印到标准输出结果判断;
|