February 2010
S M T W T F S
« Dec    
 123456
78910111213
14151617181920
21222324252627
28  

Flickr 104

It is not the final one but functions like a logical break. This version will actually display the photos on screen. The Processing program is the same as the previous one, except that we have a bigger window and retrieve the thumbnails instead of the original photos. Each of the small thumbnail in Flickr is of size 75 x 75 pixels. We have a canvas of 600 x 600 pixels. It can display a 8 x 8 mosaic of the public photos from Flickr.

Here is a scaled down image.

Here we go for the source.


import proxml.*;

String userName = "???";
String apiKey = "???";
String nsid = "";
String url = "http://api.flickr.com/services/rest/";
XMLInOut xml = null;

void setup() {
  size(600,600);
  background(0);
  frameRate(15);
  xml = new XMLInOut(this);
} 

void draw() {
} 

void mousePressed() {
  nsid = "";
  findByUsername(userName);
}

void findByUsername(String _n) {
  String rest = url+"?method=flickr.people.findByUsername";
  rest += "&api_key=" + apiKey;
  rest += "&username=" + _n;
  xml.loadElement(rest);
}

void getPublicPhotos(String _n) {
  String rest = url+"?method=flickr.people.getPublicPhotos";
  rest += "&api_key=" + apiKey;
  rest += "&user_id=" + nsid;
  xml.loadElement(rest);
}

void xmlEvent(XMLElement _x) {
  parseXML(_x);
}

void parseXML(XMLElement _x) {
  String stat = _x.getAttribute("stat");
  if (!stat.equals("ok")) {
    println("Error from Flickr");
    return;
  }
  XMLElement node = _x.getChild(0);
  String type = node.getName();
  if (type.equals("user")) {
    nsid = node.getAttribute("nsid");
    println(nsid);
    getPublicPhotos(nsid);
  }
  else if (type.equals("photos")) {
    int num = node.countChildren();
    println(num);
    getPhotos(node);
  }
}

void getPhotos(XMLElement _n) {
  int cnt = _n.countChildren();
  cnt = min(cnt,64);
  for (int i=0;i<cnt;i++) {
    XMLElement ph = _n.getChild(i);
    String fm = ph.getAttribute("farm");
    String sv = ph.getAttribute("server");
    String id  = ph.getAttribute("id");
    String sc = ph.getAttribute("secret");
    String imgURL = "http://farm"+fm+".static.flickr.com/"+
      sv + "/" + id + "_" + sc + "_s.jpg";
    PImage img = loadImage(imgURL);
    int x = (i%8) * img.width;
    int y = (i/8) * img.height;
    image(img,x,y);
  }
}

I use the loadImage() function to load the photos from Flickr. It is not the perfect choice as it may stop the draw() function processing while waiting for the loading from the Internet. A better way is to use another thread to load the image in background without affecting the normal animation in the draw() function. More to come when I have time.

You must be logged in to post a comment.