curl_opencv.cpp
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   /* There might be a realloc() out there that doesn't like reallocing
00018      NULL pointers, so we take care of it here */
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;       // CURL objects
00046   CURLcode res;
00047   cv::Mat imgTmp; // image object
00048   memoryStruct buffer; // memory buffer
00049 
00050   curl = curl_easy_init(); // init CURL library object/structure
00051 
00052   if(curl) {
00053 
00054     // set up the write to memory buffer
00055     // (buffer starts off empty)
00056 
00057     buffer.memory = NULL;
00058     buffer.size = 0;
00059 
00060     // (N.B. check this URL still works in browser in case image has moved)
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); // tell us what is happening
00064 
00065     // tell libcurl where to write the image (to a dynamic memory buffer)
00066 
00067     curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
00068     curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *) &buffer);
00069 
00070     // get the image from the specified URL
00071 
00072     res = curl_easy_perform(curl);
00073 
00074     // decode memory buffer using OpenCV
00075 
00076     imgTmp = cv::imdecode(cv::Mat(1, buffer.size, CV_8UC1, buffer.memory), CV_LOAD_IMAGE_UNCHANGED);
00077 
00078     // display image (if we got / decoded it correctly)
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     // always cleanup
00089 
00090     curl_easy_cleanup(curl);
00091     free(buffer.memory);
00092 
00093   }
00094   return 0;
00095 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


zbar_barcode_reader_node
Author(s): Dejan Pangercic
autogenerated on Thu May 23 2013 13:21:06