base64 Linux提供的图片的BCM转base64流的工具
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.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[1024] = {'\0'};
strcpy(buf, ptr);
if (strstr(ptr, "是")) {
printf("yes\n");
} else {
printf("no\n");
}
}
char* getImg(char *img)
{
int fd;
char *buf;
char cmd[36] = {'\0'};
sprintf(cmd, "base64 %s > imgFife", img);
system(cmd);
fd = open("imgFife", O_RDONLY);
int fileLen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
buf = (char*)malloc(fileLen + 5);
read(fd, buf, fileLen + 5);
return buf;
}
bool postUrl()
{
CURL *curl;
CURLcode res;
char *postString;
char *key = " "; //自行补充
char *secret = " "; //自行补充
int typeId = 21;
char *format = "xml";
system("raspistill -t 200 -tl 3000 -o img2.jpg"); //拍照
char *img1 = getImg("img1.jpg");
char *img2 = getImg("img2.jpg");
system("rm img2.jpg");
postString = (char*)malloc(sizeof(typeId) + strlen(key) + strlen(secret)
+ strlen(format) + strlen(img1) + strlen(img2));
memset(postString, '\0', strlen(postString));
sprintf(postString, "&img1=%s&img&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",
img1, img2, 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);
curl_easy_cleanup(curl);
}
return true;
}
int main(void)
{
postUrl();
}