Ask Your Question

Anuba's profile - activity

2016-12-01 04:50:53 -0600 commented question Can I upload OpenCV Mat with curl?

Yes, solved

2016-12-01 03:43:07 -0600 received badge  Student (source)
2016-11-29 12:47:25 -0600 received badge  Editor (source)
2016-11-29 11:26:23 -0600 asked a question Can I upload OpenCV Mat with curl?

Can I upload OpenCV Mat with curl? Without save on drive? I have Mat image and want upload him like I sent a picture from drive with C++ curl:

curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "picture",
    CURLFORM_COPYCONTENTS, "sample.jpg"
    CURLFORM_END);

Solution:

int writer(char *data, size_t size, size_t nmemb, string *writerData)
{
    if (writerData == NULL)
        return 0;
    int len = size*nmemb;
    writerData->append(data, len);
    return len;
}

 void upLoad(Mat img){
vector<uchar> vec_Img;

vector<int> vecCompression_params;

vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
vecCompression_params.push_back(90);
imencode(".jpg", img, vec_Img, vecCompression_params);

CURL *curl;
CURLcode res;

string buffer;

struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;

curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "URL");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Accept:");

    chunk = curl_slist_append(chunk, "Authorization: Token ");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);


    curl_formadd(&post, &last, 
        CURLFORM_PTRNAME, "photo",
        CURLFORM_BUFFER, "photo.jpg",
        CURLFORM_BUFFERPTR, vec_Img.data(),
        CURLFORM_BUFFERLENGTH, vec_Img.size(),
        CURLFORM_CONTENTTYPE, "image/jpg",
            CURLFORM_END);


    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

    curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    curl_easy_cleanup(curl);
}