July 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Create a Chronological Photo Album with Java

  • September 21, 2006
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Chronological Album Sort: the Back-End Engine

The brains of this project and the logic that will be used by the front-end application uses a Stack and recursion to analyze the root folder, create Album objects, and calculate Album attributes. In addition, I use the SortedMap object to sort the Albums by their calculated master date.

Here is the file system view of my test picture folders. Note their alphabetical order on the hard drive.



Click here for a larger image.

Here is the output from the back-end engine, which is chronologically correct, sorted by the true times the events occurred.



Click here for a larger image.

(Time: 953ms)

Because I do not read the creation date for every file in every subfolder, but instead use selective sampling, very little I/O is generated and the calculation engine is extremely fast. The selective sampling works even if pictures are not in alphabetical order. For example, over 800 Mb of pictures can be analyzed in 953 MS on a standard hard drive (7200 RMP), with RAID optimized I/O; with 10000 RPM hard drives, this time can be decreased even further.

Now, look at how the engine works.

The GetResourceFiles object (called from the main method) is passed a location of the root folder.

It then calls a getRootView method that recursively traverses the folders and pushes them on the Stack. For every folder that is pushed on the stack, a random thumbnail image is calculated every time the engine is called (see the pushAlbum method). This is the property that can be accessed on the front end to display a different picture for an album on every refresh.

In addition, if the folder has subfolders, they are all analyzed, sorted by their calculated date, and also pushed on the Stack. The output indentation you see is created by the relative position of the object in the stack. Therefore, child folders are correctly indented from the parent folders (see the indent method) But, for the Web representation, the indent method would need to be changed to account for the HTML or other output.

public void getRootView(File dir) {
   if (dir.exists() && dir.isDirectory()) {
      Album a = new Album(dir);
      pushAlbum(a);

      // one level down only
      Album[] innerAlbums = a.getInnerAlbums();

      if (innerAlbums != null && innerAlbums.length > 0) {
         // sort in the order of newest first here
         // before expecting them
         SortedMap sortedInnerAlbums =
            AlbumUtils.sortAlbums(innerAlbums);
         Collection sAlbums = sortedInnerAlbums.values();
         innerAlbums =
            (Album[]) sAlbums.toArray(new Album[sAlbums.size()]);

         sortedAlbums.putAll(sortedInnerAlbums);

         for (int i = 0; i < innerAlbums.length; i++) {
            // recursive call to continue traversing the dir tree
            getRootView(innerAlbums[i].getDir());
         }
      }
      popAlbum();
   }
}

private String indent(String tag) { StringBuffer res = new StringBuffer(); int indent = stack.size() - 1; for (int i = 0; i < indent; i++) { res.append("t"); } res.append(tag); return res.toString(); }

Here is the execution chain for the recursive logic:

  1. Call getRootView.
  2. Push this folder on the Stack
    1. Output the folder info and thumbnail picture info.
  3. Get all SubFolders and sort them by the "master" date.
    1. Call AlbumUtils.sortAlbums(innerAlbums);
    2. Call setAndGetDate per Album, which calls the calcAlbumDate method, which calls AlbumUtils.getAlbumDate.
  4. Call getRootView per subfolder—recursive call—back to Step 1.
  5. In no more subfolders—exit recursion—pop this folder from the stack.

Sharing with the World

To share your own album site, using this backend engine, you'll need an application server and a Web server. The front end would call the engine and display the output in some nicely formatted way. When you place a new folder under the root folder, visitors to the site will automatically see it on the top if its pictures were taken last. If you place a new folder as a subfolder, visitors will see it in the correct chronological order in relation to other subfolders. All of the folder names should be links pointing to the thumbnails directory, or if you are using Google's Picasa, for instance, you can pre-generate HTML Web album pages for the links.

If you only have an Apache Web server, you can write the output of the back-end engine as an HTML file, with album links pointing to the pre-generated HTML Web albums and place that file under the htdocs folder. However, the problem with this simple approach is that you lose the dynamic aspect of the site, and will need to re-generate the main HTML every time you add an Album folder. On the other hand, you do not need an Application server, and no JSPs or Servlets are involved.

Conclusion

In this article, I described how to implement a back-end engine to create your own picture sharing site. I talked about the exif metadata standard, reading file date, using selective sampling to minimize I/O, and, most importantly, how to use Stack and recursion to chronologically sort picture Albums. Now, the only thing left is to download the source code project and start sharing! As always, your comments are welcome.

Download the Source

You can download the source.zip file here.

References

About the Author

Vlad Kofman is a Senior System Architect. He has implemented enterprise-scale projects for the major Wall Street firms, defense contracts, and the U.S. government. His main interests are object-oriented programming methodologies and design patterns. You can contact him at


Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel