2008年1月12日土曜日

[Java>Flickrj]Flickr Java API(Flickrj Library)

Access to the Flickr's photo via Flickr Java API(Flickrj Library).
The Api spec is maintained by the Flickr, but this flickrj library kit is not maintained or supported by Flickr. Please read the attention carefully before using this library.

References
  • Flickr's API documents
    http://www.flickr.com/services/api/
  • Flickrj web site
    http://flickrj.sourceforge.net/
  • Flickrj javadoc
    http://flickrj.sourceforge.net/
Install and Setup
  1. Get Flickr API keys to use the Flickr's API
    http://www.flickr.com/services/api/keys/
  2. Download flickrj library and set jar file into the library directory
    http://sourceforge.net/projects/flickrj/
Import Classes
import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.photos.SearchParameters;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;
import com.aetrion.flickr.photos.Photo;
Sample Program
  • Search photos with tag keywords and get result
         //Set api key
    String key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    String svr="www.flickr.com";
    REST rest=new REST();
    rest.setHost(svr);

    //initialize Flickr object with key and rest
    Flickr flickr=new Flickr(key,rest);
    Flickr.debugStream=false;

    //initialize SearchParameter object, this object stores the search keyword
    SearchParameters searchParams=new SearchParameters();
    searchParams.setSort(SearchParameters.INTERESTINGNESS_DESC);

    //Create tag keyword array
    String[] tags=new String[]{"Dog","Beagle"};
    searchParams.setTags(tags);

    //Initialize PhotosInterface object
    PhotosInterface photosInterface=flickr.getPhotosInterface();
    //Execute search with entered tags
    PhotoList photoList=photosInterface.search(searchParams,20,1);

    //get search result and fetch the photo object and get small square imag's url
    if(photoList!=null){
    //Get search result and check the size of photo result
    for(int i=0;i<photoList.size();i++){
    //get photo object
    Photo photo=(Photo)photoList.get(i);

    //Get small square url photo
    StringBuffer strBuf=new StringBuffer();
    strBuf.append("<a href=\"\">");
    strBuf.append("<img border=\"0\" src=\""+photo.getSmallSquareUrl()+"\">");
    strBuf.append("</a>\n");
    ....
    }
    }
  • Get photo and information with specified photo ID
         //Set api key
    String key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    String svr="www.flickr.com";
    REST rest=new REST();
    rest.setHost(svr);

    //initialize Flickr object with key and rest
    Flickr flickr=new Flickr(key,rest);
    Flickr.debugStream=false;

    //Initialize PhotosInterface object
    PhotosInterface photosInterface=flickr.getPhotosInterface();
    //Execute search with photo ID you want to get information
    Photo photo=photosInterface.getPhoto("[Photo ID]");
    if(photo!=null){
    StringBuffer strBuf=new StringBuffer();
    strBuf.append("<a href=\"\">");
    strBuf.append("<img border=\"0\" src=\""+photo.getThumbnailUrl()+"\">");
    strBuf.append("</a>\n");
    }

1 件のコメント:

John Ortiz Ordoñez さんのコメント...

Thanks a lot for this example. It is working for with minimum modifications. See you later