
Handling Flickr
Flickr (https://www.flickr.com/) is an online photo management and sharing application. It is a possible source for images and videos. The Flickr Developer Guide (https://www.flickr.com/services/developer/) is a good starting point to learn more about Flickr's API.
One of the first steps to using the Flickr API is to request an API key. This key is used to sign your API requests. The process to obtain a key starts at https://www.flickr.com/services/apps/create/. Both commercial and noncommercial keys are available. When you obtain a key you will also get a "secret." Both of these are required to use the API.
We will illustrate the process of locating and downloading images from Flickr. The process involves:
- Creating a Flickr class instance
- Specifying the search parameters for a query
- Performing the search
- Downloading the image
A FlickrException or IOException may be thrown during this process. There are several APIs that support Flickr access. We will be using Flickr4Java, found at https://github.com/callmeal/Flickr4Java. The Flickr4Java Javadocs is found at http://flickrj.sourceforge.net/api/. We will start with a try block and the apikey and secret declarations:
try {
String apikey = "Your API key";
String secret = "Your secret";
} catch (FlickrException | IOException ex) {
// Handle exceptions
}
The Flickr instance is created next, where the apikey and secret are supplied as the first two parameters. The last parameter specifies the transfer technique used to access Flickr servers. Currently, the REST transport is supported using the REST class:
Flickr flickr = new Flickr(apikey, secret, new REST());
To search for images, we will use the SearchParameters class. This class supports a number of criteria that will narrow down the number of images returned from a query and includes such criteria as latitude, longitude, media type, and user ID. In the following sequence, the setBBox method specifies the longitude and latitude for the search. The parameters are (in order): minimum longitude, minimum latitude, maximum longitude, and maximum latitude. The setMedia method specifies the type of media. There are three possible arguments---"all", "photos", and "videos":
SearchParameters searchParameters = new SearchParameters();
searchParameters.setBBox("-180", "-90", "180", "90");
searchParameters.setMedia("photos");
The PhotosInterface class possesses a search method that uses the SearchParameters instance to retrieve a list of photos. The getPhotosInterface method returns an instance of the PhotosInterface class, as shown next. The SearchParameters instance is the first parameter. The second parameter determines how many photos are retrieved per page and the third parameter is the offset. A PhotoList class instance is returned:
PhotosInterface pi = new PhotosInterface(apikey, secret,
new REST());
PhotoList<Photo> list = pi.search(searchParameters, 10, 0);
The next sequence illustrates the use of several methods to get information about the images retrieved. Each Photo instance is accessed using the get method. The title, image format, public flag, and photo URL are displayed:
out.println("Image List");
for (int i = 0; i < list.size(); i++) {
Photo photo = list.get(i);
out.println("Image: " + i +
`"\nTitle: " + photo.getTitle() +
"\nMedia: " + photo.getOriginalFormat() +
"\nPublic: " + photo.isPublicFlag() +
"\nUrl: " + photo.getUrl() +
"\n");
}
out.println();
A partial listing is shown here where many of the specific values have been modified to protect the original data:
Image List
Image: 0
Title: XYZ Image
Media: jpg
Public: true
Url: https://flickr.com/photos/7723...@N02/269...
Image: 1
Title: IMG_5555.jpg
Media: jpg
Public: true
Url: https://flickr.com/photos/2665...@N07/264...
Image: 2
Title: DSC05555
Media: jpg
Public: true
Url: https://flickr.com/photos/1179...@N04/264...
The list of images returned by this example will vary since we used a fairly wide search range and images are being added all of the time.
There are two approaches that we can use to download an image. The first uses the image's URL and the second uses a Photo object. The image's URL can be obtained from a number of sources. We use the Photo class getUrl method for this example.
In the following sequence, we obtain an instance of PhotosInterface using its constructor to illustrate an alternate approach:
PhotosInterface pi = new PhotosInterface(apikey, secret,
new REST());
We get the first Photo instance from the previous list and then its getUrl to get the image's URL. The PhotosInterface class's getImage method returns a BufferedImage object representing the image as shown here:
Photo currentPhoto = list.get(0);
BufferedImage bufferedImage =
pi.getImage(currentPhoto.getUrl());
The image is then saved to a file using the ImageIO class:
File outputfile = new File("image.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);
The getImage method is overloaded. Here, the Photo instance and the size of the image desired are used as arguments to get the BufferedImage instance:
bufferedImage = pi.getImage(currentPhoto, Size.SMALL);
The image can be saved to a file using the previous technique.
The Flickr4Java API supports a number of other techniques for working with Flickr images.