很多AI平台上只写了C++、Java、python调用的接口
C语言是同样可以做的
原理如下
https://blog.csdn.net/qq_28258885/article/details/112093574
https://blog.csdn.net/qq_28258885/article/details/112093574
https://blog.csdn.net/qq_28258885/article/details/112094219
本篇介绍一种方法使用C语言进行外部API的调用
AI平台
现在的AI平台有很多,各个公司都在做
本篇选用祥云平台,一毛钱能用100次
在个人中心能看到自己的OCR key和OCR secret
在人脸识别相对应的页面,可以看到需要的参数
这里要注意的是,这款API的调用使用的是https协议
https就是http加上openSSL加密
相关库的安装
阅读curl的说明
如果要支持,那么就要输入参数选项
需要在此之前安装openSSL库作为依赖
openssl
https://www.openssl.org/source/
解压后进入文件夹
依次输入指令
./config
make -j8 && sudo make install
curl
下载
https://github.com/curl/curl/releases/tag/curl-7_71_1
进入curl解压后的文件夹中
依次输入
./configure --prefix=$PWD/_install --with-ssl
make -j8 && make install
–prefix是指定安装路径
准备图片
选择两张jpg格式图片
代码
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>//for open
#include <sys/types.h>
#include <unistd.h>//for lseek
#define true 1
#define false 0
typedef unsigned int bool;
char buf[10240] = {"\0"};
size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)
{
char buf[1024] = {"\0"};
strncpy(buf, ptr, 1024);
printf("===================get Data==================\n");
printf("%s\n",buf); //only for test
printf("=============================================\n");
}
char* getPicBase64FromFile(char *filePath)//the path is used to store base64
{
char *bufPic;
char cmd[128] = {"\0"};
sprintf(cmd, "base64 %s > tmpFile", filePath);//create a file to store base64
system(cmd);
int fd = open("./tmpFile", O_RDWR);
int filelen = lseek(fd, 0, SEEK_END);//get the len of base64
lseek(fd, 0, SEEK_SET);
bufPic = (char *)malloc(filelen + 2);
memset(bufPic, '\0', filelen + 2);
read(fd, bufPic, filelen);//read base64 to bufPic
close(fd);
system("rm -f tmpFile");//remove the file after read
return bufPic;//return the dest of the space of malloc,the space still exists
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char *key = "L22***********************u4";//ur OCR key
char *secret = "95c*******************************8a";//ur OCR secret
int typeId = 21;
char *format = "xml";//by the setting of the web onwer
char *bufPic1 = getPicBase64FromFile("./reba1.jpg");//we need 2 pics
char *bufPic2 = getPicBase64FromFile("./reba2.jpg");//translate them to base64
int len = strlen(key) + strlen(secret) + strlen(bufPic1) + strlen(bufPic2) + 124;//get len
postString = (char *)malloc(len);
sprintf(postString, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
bufPic1, bufPic2, key, secret, 21, format);//get the string which we post to the OCR platform
curl = curl_easy_init();//lib curl 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);//callBack function
res = curl_easy_perform(curl);
printf("ok:%d\n", res);//get the return num
if(strstr(buf,"是") != NULL)
{
printf("the same person\n");
}else{
printf("different person\n");
}//get result
curl_easy_cleanup(curl);//lib curl cleanup
}
return true;
}
int main(void)
{
postUrl();
}
执行
编译运行
gcc cmpPerson.c -I ~/tools/curl-7.71.1/_install/include/ -L ~/tools/curl-7.71.1/_install/lib/ -lcurl