MyCLASS::writeData(char *ptr, size_t size, size_t nmemb, void *userdata) {
std::vector<uchar> *stream = (std::vector<uchar>*)userdata;
size_t count = size * nmemb;
stream->insert(stream->end(), ptr, ptr + count);
return count;
}
cv::Mat MyCLASS::curlImg(const char *img_url, int timeout) {
std::vector<uchar> stream;
curlCallbackStruct data;
data.instanse=this;
data.stream=&stream;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, staticWriteData); // pass the writefunction
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); // pass the stream ptr to the writefunction
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); // timeout if curl_easy hangs,
CURLcode res = curl_easy_perform(curl); // start curl
curl_easy_cleanup(curl); // cleanup
return cv::imdecode(stream, -1); // 'keep-as-is'
}
size_t MyCLASS::staticWriteData(char *ptr, size_t size, size_t nmemb, void *userdata) {
return ((*(curlCallbackStruct*)userdata).instanse)->writeData(ptr,size,nmemb,(*(curlCallbackStruct*)userdata).stream);
}
У курла для тредов надо использовать не easy-интерфейс,а shared
Обсуждают сегодня