OCR平台接口

本文旨在解决通过http接口,上传数据到OCR平台,并接收反馈结果,本文使用翔云OCR平台。

curl安装

apt-get install -y opensll

查看本机的curl版本

curl -V

安装对应版本的curl

tar -zxvf curl-7.55.1.tar.gz

cd curl-7.55.1

./configure  --prefix=/root/mycurl --with-ssl

make && make install

测试

cd /root/mycurl/bin && ./curl www.baidu.com

curl库

curl_global_init(long flags);
功能:全局初始化libcurl
flags:
CURL_GLOBAL_ALL             //初始化所有的可能的调用。
CURL_GLOBAL_SSL             //初始化支持 安全套接字层。
CURL_GLOBAL_WIN32           //初始化win32套接字库。
CURL_GLOBAL_NOTHING         //没有额外的初始化。

curl_global_init()接口进行全局初始化,一个进程调用一次。如果一次都未调用,curl_easy_init()接口内部会自动调curl_global_init()。

CURL *curl_easy_init();
功能:获取句柄
返回一个easy_handle(CURL*对象), 用与easy函数,主线程调用

多个线程同时调用curl_easy_init(),会出现一个线程没有初始化进行网络传输导致崩溃,建议调用curl_global_init()进行libcurl库进行全局初始化。


char *curl_version();
功能:打印当前libcurl库的版本

CURLcode curl_easy_setopt(CURL *handler, CURLoption option, param);
功能:设置配置
参数:                 option:参数类型        param:对应类型的参数
post的参数             CURLOPT_POSTFIELDS     post_str
设置访问URL(post,get)  CURLOPT_URL w          ww.baidu.com 
回调函数 (post,get)    CURLOPT_WRITEFUNCTION  callback
回电函数:size_t callback( void *ptr, size_t size, size_t nmemb, void *stream); 

curl_easy_perform(CURL *handler);
功能:执行配置
handler:句柄

void curl_easy_cleanup(CURL *handle);
功能:结束会话,与curl_easy_init配合使用

void curl_global_cleanup(void);
功能:结束调库,对curl_global_init做清理

     OCR平台接口 

                车牌识别接口代码示例

#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

size_t printf_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
        char data_buf[1024] = {'\0'};
        strncpy(data_buf,ptr,1024);
        printf("%s\n",data_buf);
}

char *base64_file(char file_name[])
{
        char cmd[128] = {'\0'};
        int fd;
        int size;
        char *buf;
        sprintf(cmd,"base64 %s > tmp_file",file_name);
        system(cmd);
        fd = open("./tmp_file",O_RDWR);
        size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        buf = (char *)malloc(size);
        memset(buf,'\0',size);
        read(fd,buf,size);
        close(fd);
        system("rm -rf ./tmp_file");
        return buf;
}


bool getUrl(char file_name[])
{
        CURL *curl;
        CURLcode res;
        char *post_str;
        char *key = "***********";
        char *secret = "**************";
        int type_id = 19;
        char *format = "xml";
        char *image;
        int len;

        image = base64_file(file_name);
        len = strlen(key) + strlen(secret) + strlen(image) + 128;
        post_str = (char *)malloc(len);
        sprintf(post_str,"&img=%s&key=%s&secret=%s&typeId=%d&format=%s",image,key,secret,type_id,format);

        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
                curl_easy_setopt(curl, CURLOPT_URL,"https://netocr.com/api/recogliu.do");
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,printf_data);
                res = curl_easy_perform(curl); 
                if (res != 0) 
                {

                        curl_easy_cleanup(curl);
                }
                return true;
        }
        else
        {
                printf("init libcrul fail\n");
                return false;
        }
}

int main(int argc,char **argv)
{
        if(argc != 2)
        {
                printf("param is error\n");
                exit(-1);
        }
        curl_global_init(CURL_GLOBAL_ALL);
        getUrl(argv[1]);
        void curl_global_cleanup();
        return 0;
}

结果测试示例

         人脸识别代码接口示例

#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

size_t printf_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
        char data_buf[1024] = {'\0'};
        strncpy(data_buf,ptr,1024);
        printf("%s\n",data_buf);
}

char *base64_file(char file_name[])
{
        char cmd[128] = {'\0'};
        int fd;
        int size;
        char *buf;
        sprintf(cmd,"base64 %s > tmp_file",file_name);
        system(cmd);
        fd = open("./tmp_file",O_RDWR);
        size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
        buf = (char *)malloc(size);
        memset(buf,'\0',size);
        read(fd,buf,size);
        close(fd);
        system("rm -rf ./tmp_file");
        return buf;
}


bool getUrl(char first_file_name[],char second_file_name[])
{
        CURL *curl;
        CURLcode res;
        char *post_str;
        char *key = "***********";
        char *secret = "************";
        int type_id = 21;
        char *format = "xml";
        char *image1;
        char *image2;
        int len;

        image1 = base64_file(first_file_name);
        image2 = base64_file(second_file_name);
        len = strlen(key) + strlen(secret) + strlen(image1) + strlen(image2) + 128;
        post_str = (char *)malloc(len);
        sprintf(post_str,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",image1,image2,key,secret,type_id,format);

        curl = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
                curl_easy_setopt(curl, CURLOPT_URL,"https://netocr.com/api/faceliu.do");
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,printf_data);
                res = curl_easy_perform(curl); 
                if (res != 0) 
                {

                        curl_easy_cleanup(curl);
                }
                return true;
        }
        else
        {
                printf("init libcrul fail\n");
                return false;
        }
}

int main(int argc,char **argv)
{
        if(argc != 3)
        {
                printf("param is error\n");
                exit(-1);
        }
        curl_global_init(CURL_GLOBAL_ALL);
        getUrl(argv[1],argv[2]);
        void curl_global_cleanup();
        return 0;
}

测试结果示例

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值