// flickr_api_array - November 2011 - rtwomey@u.washington.edu // // flickr image search demo with array and preloading // // flickr API details here: // http://www.flickr.com/services/api/ // // register for a developers key here: // http://www.flickr.com/services/apps/create/apply/ PImage[] images; // array to hold images int imageIndex; // which image are we currently showing void setup() { size(500, 500); String baseURL = "http://api.flickr.com/services/rest/?"; // address for flickr web services String apiMethod = "flickr.photos.search"; // Return a list of photos matching some criteria. // Only photos visible to the calling user will be returned. // To return private or semi-private photos, the caller must be authenticated // with 'read' permissions, and have permission to view the photos. // Unauthenticated calls will only return public photos. String apiKey = "bd7435660785b923332b224789d33c93"; // this key was floating around processing.org, bad form to use // you should register for your own online // http://www.flickr.com/services/apps/create/apply/ String tagString="monkey"; // what tag are we searching for? int perPage=40; // how many results? String query = baseURL+"method="+apiMethod+"&tags="+tagString+"&api_key="+apiKey+"&per_page="+perPage; // this query uses "tags" and "per_page" arguments to specify search. // additional arguments to pass to flickr search are here: // http://www.flickr.com/services/api/flickr.photos.search.html //println(query); // do actual query call XMLElement xml = new XMLElement(this, query); // holds the results //println(xml); XMLElement[] photoTag = xml.getChildren("photos/photo"); // get just the photos println(photoTag.length + " images"); images = new PImage[photoTag.length]; // loop over results and store in array for (int i = 0; i < photoTag.length; i++) { String farm = photoTag[i].getString("farm"); String server = photoTag[i].getString("server"); String id = photoTag[i].getString("id"); String secret = photoTag[i].getString("secret"); String img = "http://farm"+farm+".static.flickr.com/"+server+"/"+id+"_"+secret+".jpg"; images[i] = requestImage(img); } textAlign(CENTER, CENTER); smooth(); } void draw() { background(0); if (images[imageIndex] != null) { image(images[imageIndex], 0, 0); } fill(0); ellipse(15, 15, 20, 20); fill(255); text(imageIndex, 16, 14); if (frameCount % 60 == 0) { imageIndex++; if (imageIndex >= images.length) { imageIndex = 0; } } }