Libcurl图像上传内存中的帖子

时间:2015-09-25 03:35:08

标签: c++ opencv post curl libcurl

我正在编写一个程序,它从一个以JSON提供,解析,处理并发送回服务器的安静服务器中提取图像。我在发送图像时遇到问题。目前我将它存储在一个字符串中,并尝试使用CURLFORM_BUFFER将其发回。我通过将图像写入文件确认图像在字符串中。没有问题。我目前的代码如下。我目前正在帖子中遇到一个段错误11。我的代码如下。

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "dist/jsoncpp.cpp"
#include "dist/json/json.h"

using namespace std;


static size_t write_data(void *contents, size_t size, size_t nmemb,    
void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

void uploadImage(std::string readBuffer){

CURL *curl;
CURLcode res;

struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";

curl_global_init(CURL_GLOBAL_ALL);

printf("Image length: %d\n\n", readBuffer.length());
CURLFORMcode code = curl_formadd(&formpost,
             &lastptr,
   CURLFORM_COPYNAME, "processedImage",
   CURLFORM_BUFFER, "image.jpg",
   CURLFORM_BUFFERPTR, readBuffer,
   //CURLFORM_BUFFERLENGTH, readBuffer.length(),
   CURLFORM_END);

   if(code != 0){
     printf("Something went wrong in formadd.\n");
   }


curl = curl_easy_init();
/* initialize custom header list (stating that Expect: 100-continue is not
   wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
  /* what URL that receives this POST */
  curl_easy_setopt(curl, CURLOPT_URL, "uploadProcessedImageURL");

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, formpost);

  /* Perform the request, res will get the return code */
  res = curl_easy_perform(curl);
  /* Check for errors */
  if(res != CURLE_OK)
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));

  /* always cleanup */
  curl_easy_cleanup(curl);

  /* then cleanup the formpost chain */
  curl_formfree(formpost);
  /* free slist */
  curl_slist_free_all (headerlist);
}
}

int main(void)
{

CURL *curl;
CURLcode res;
const cv::_InputArray data;
std::string readBuffer;

char *url = "requestImageFileURL";
curl = curl_easy_init();
if (curl) {

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
readBuffer.clear();
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}

//printf("%s\n\n", readBuffer.c_str());
printf("Image retrieved.\n");
Json::Value values;
Json::Reader reader;
reader.parse(readBuffer, values);

Json::Value imageArray = values.get("userUploadedImage","default     
value");
Json::Value idNumber = values.get("id","default value");
Json::FastWriter fastWriter;
std::string output = fastWriter.write(imageArray);

//cout << output << endl;

std::vector<char> vectordata(output.begin(), output.end());

//for (auto i = vectordata.begin(); i != vectordata.end(); ++i)
//std::cout << *i;

cv::Mat data_mat(vectordata,true);
cv::Mat  image(cv::imdecode(data_mat, 1));
std::cout<<"Height: " << image.rows <<" Width: "<<image.cols<<endl;

//cv::namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
//cv::imshow( "Display Image",image);
//cv::waitKey(0);
uploadImage(readBuffer);

return 0;
}

0 个答案:

没有答案