Can I upload OpenCV Mat with curl? [closed]

asked 2016-11-29 11:03:33 -0600

Anuba gravatar image

updated 2017-08-02 16:27:21 -0600

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);
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2016-12-01 04:56:05.597387

Comments

2

sure, you can. but please show, what you tried so far.

(so folks here can just correct a line or 2, instead of re-writing the whole thing)

berak gravatar imageberak ( 2016-11-29 11:27:40 -0600 )edit

cute ! is it solved, now ?

berak gravatar imageberak ( 2016-12-01 03:43:45 -0600 )edit
1

Yes, solved

Anuba gravatar imageAnuba ( 2016-12-01 04:50:53 -0600 )edit