{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current &#61; ( (unsigned char)(bindata[i] <<4 ) ) & ( (unsigned char)0x30 ) ;
if ( i &#43; 1 >&#61; binlength )
{
base64[j&#43;&#43;] &#61; base64char[(int)current];
base64[j&#43;&#43;] &#61; &#39;&#61;&#39;;
base64[j&#43;&#43;] &#61; &#39;&#61;&#39;;
break;
}
current |&#61; ( (unsigned char)(bindata[i&#43;1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j&#43;&#43;] &#61; base64char[(int)current];
current &#61; ( (unsigned char)(bindata[i&#43;1] <<2) ) & ( (unsigned char)0x3C ) ;
if ( i &#43; 2 >&#61; binlength )
{
base64[j&#43;&#43;] &#61; base64char[(int)current];
base64[j&#43;&#43;] &#61; &#39;&#61;&#39;;
break;
}
current |&#61; ( (unsigned char)(bindata[i&#43;2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j&#43;&#43;] &#61; base64char[(int)current];
current &#61; ( (unsigned char)bindata[i&#43;2] ) & ( (unsigned char)0x3F ) ;
base64[j&#43;&#43;] &#61; base64char[(int)current];
}
base64[j] &#61; &#39;\0&#39;;
return base64;
}
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i &#61; 0, j &#61; 0; base64[i] !&#61; &#39;\0&#39; ; i &#43;&#61; 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k &#61; 0 ; k <64 ; k &#43;&#43; )
{
if ( base64char[k] &#61;&#61; base64[i] )
temp[0]&#61; k;
}
for ( k &#61; 0 ; k <64 ; k &#43;&#43; )
{
if ( base64char[k] &#61;&#61; base64[i&#43;1] )
temp[1]&#61; k;
}
for ( k &#61; 0 ; k <64 ; k &#43;&#43; )
{
if ( base64char[k] &#61;&#61; base64[i&#43;2] )
temp[2]&#61; k;
}
for ( k &#61; 0 ; k <64 ; k &#43;&#43; )
{
if ( base64char[k] &#61;&#61; base64[i&#43;3] )
temp[3]&#61; k;
}
bindata[j&#43;&#43;] &#61; ((unsigned char)(((unsigned char)(temp[0] <<2))&0xFC)) |
((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
if ( base64[i&#43;2] &#61;&#61; &#39;&#61;&#39; )
break;
bindata[j&#43;&#43;] &#61; ((unsigned char)(((unsigned char)(temp[1] <<4))&0xF0)) |
((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
if ( base64[i&#43;3] &#61;&#61; &#39;&#61;&#39; )
break;
bindata[j&#43;&#43;] &#61; ((unsigned char)(((unsigned char)(temp[2] <<6))&0xF0)) |
((unsigned char)(temp[3]&0x3F));
}
return j;
}
void encode(FILE * fp_in, FILE * fp_out)
{
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
bytes &#61; fread( bindata, 1, 2049, fp_in );
base64_encode( bindata, base64, bytes );
fprintf( fp_out, "%s", base64 );
}
}
void decode(FILE * fp_in, FILE * fp_out)
{
int i;
unsigned char bindata[2050];
char base64[4096];
size_t bytes;
while ( !feof( fp_in ) )
{
for ( i &#61; 0 ; i <2048 ; i &#43;&#43; )
{
base64[i] &#61; fgetc(fp_in);
if ( base64[i] &#61;&#61; EOF )
break;
else if ( base64[i] &#61;&#61; &#39;\n&#39; || base64[i] &#61;&#61; &#39;\r&#39; )
i --;
}
bytes &#61; base64_decode( base64, bindata );
fwrite( bindata, bytes, 1, fp_out );
}
}
FILE *fp;
//这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的
//完成数据保存功能
size_t WriteData(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written &#61; fwrite(ptr, size, nmemb, (FILE *)fp);
return written;
}
int postUrl()
{
CURL *curl;
CURLcode res;
char * imagedatapoint &#61; NULL;
char szJsonData[65535] &#61; {0};
sprintf(szJsonData,"{\"image\":\"%s\",\"top_num\":5}",imagedatapoint);//imagedatapoint 是base64编码好的图像数据编码函数上面有
char strCurlOpt_url[] &#61;"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/ncpsb?access_token&#61;24.6871ee410bf772d.......";
//等号后面换成你自己的access_token. 接口地址记得替换成自己的哦
if((fp&#61;fopen("/usr/local/x86_curl/1.txt","w"))&#61;&#61;NULL)
{
printf("fopen( /opt/1.txt) fail\n");
exit(1);
}
struct curl_slist * header &#61; NULL;
header &#61; curl_slist_append(header,"Content-Type: application/json;charset&#61;UTF-8");
if(NULL &#61;&#61; header)
{
printf("append is error\n");
exit(-1);
}
curl &#61; curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_URL,strCurlOpt_url); // 指定url
curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,szJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
res &#61; curl_easy_perform(curl);
printf("%d %s\n",res, curl_easy_strerror(res));
curl_easy_cleanup(curl);
curl_slist_free_all(header);
}
fclose(fp);
return 1;
}
int main(void)
{
curl_global_init(CURL_GLOBAL_SSL);
postUrl();
}