Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <curl/curl.h>
00003
00004 #include <cv.h>
00005 #include <highgui.h>
00006 using namespace cv;
00007
00008
00009
00010 struct memoryStruct {
00011 char *memory;
00012 size_t size;
00013 };
00014
00015 static void* CURL_realloc(void *ptr, size_t size)
00016 {
00017
00018
00019 if(ptr)
00020 return realloc(ptr, size);
00021 else
00022 return malloc(size);
00023 }
00024
00025 size_t WriteMemoryCallback
00026 (void *ptr, size_t size, size_t nmemb, void *data)
00027 {
00028 size_t realsize = size * nmemb;
00029 struct memoryStruct *mem = (struct memoryStruct *)data;
00030
00031 mem->memory = (char *)
00032 CURL_realloc(mem->memory, mem->size + realsize + 1);
00033 if (mem->memory) {
00034 memcpy(&(mem->memory[mem->size]), ptr, realsize);
00035 mem->size += realsize;
00036 mem->memory[mem->size] = 0;
00037 }
00038 return realsize;
00039 }
00040
00041
00042
00043 int main(void)
00044 {
00045 CURL *curl;
00046 CURLcode res;
00047 cv::Mat imgTmp;
00048 memoryStruct buffer;
00049
00050 curl = curl_easy_init();
00051
00052 if(curl) {
00053
00054
00055
00056
00057 buffer.memory = NULL;
00058 buffer.size = 0;
00059
00060
00061
00062 curl_easy_setopt(curl, CURLOPT_URL, "http://www.froodies.de/FroodiesImages/images/products/large/29278.jpg");
00063 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
00064
00065
00066
00067 curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00068 curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *) &buffer);
00069
00070
00071
00072 res = curl_easy_perform(curl);
00073
00074
00075
00076 imgTmp = cv::imdecode(cv::Mat(1, buffer.size, CV_8UC1, buffer.memory), CV_LOAD_IMAGE_UNCHANGED);
00077
00078
00079
00080 namedWindow("Image from URL", CV_WINDOW_AUTOSIZE);
00081 if (!(imgTmp.empty()))
00082 {
00083 imshow("Image from URL", imgTmp);
00084 }
00085 waitKey(0);
00086
00087
00088
00089
00090 curl_easy_cleanup(curl);
00091 free(buffer.memory);
00092
00093 }
00094 return 0;
00095 }