Create a Chronological Photo Album with Java
With the proliferation of the online picture sharing sites, such as flickr.com, snapfish.com, or kodakgallery.com, many developers are wondering how to achieve the same on their own. With the right, generally free, tools and a few clever Java algorithms, you can be hosting your own photo album in no time, without paying any subscription fees. In this article, I will teach you how to create a personal photo album that will be automatically sorted and ordered in correct chronological order, and independent from the file system names and locations. With the sample code, you will be able to drop the new folder of pictures into some directory and have them correctly show up in relation to the other picture folders you are sharing.
The back-end engine algorithms are in Java and are generic enough to have any front-end technology such as JSPs or Servlets. I developed the back-end engine; it uses stack data types and recursion. The algorithms for reading exif metadata, such as the date the picture was taken, uses a metadata extractor library developed by Drew Noakes (see references). Additionally, due to Java's shortcoming of returning the file modification time instead of the creation time with the standard File I/O APIs, I use a FileTimes utility to correctly retrieve the file creation time. This utility is free for a single person, single site, or single server. The front-end technologies are beyond the scope of this article. However, I will discuss a very simple way to have your pictures shared on an Apache Web server.
Setting Up Your Own Photo Album
To implement a photo album project, I have established a very simple directory structure. You may ultimately use something different, but if you want to use the included source with minimal changes, follow this structure:
- All photo album folders will reside in some main "root" directory.
- Every album folder must contain two sub folders, called "images" and "thumbnails" (more on this later).
- Album folders can be nested.
C:photos: is my main root folder
C:photosSomeAlbum: photo album folder
The images folder contains actual photos in JPEG format, and the thumbnails folder contains small versions of the main pictures for front-end display purposes. This folder can be created with many free tools available online, such as Google's Picasa or Jalbum. The thumbnails are usually 160x120 pixels, for faster downloads over the wire. I will talk about thumbnails later, but for now, suffice it to say that my algorithm also will pick a random thumbnail to display next to the album information. This function would make it more interesting to the viewers, who will see a different image representing the whole album every time they refresh (or come back to) the page.
Here is a screen shot of several nested albums. Fictional folders demonstrate the required structure:
Actual folders were used for testing: note their alphabetical order on the hard drive is chronologically incorrect and different from when the pictures where taken versus when the folder was created.
Picture Metadata Standard
All the new digital cameras follow the exif or iptc standard for metadata embedded with every picture. Exif is a standard for image files created with digital cameras and other input devices. The standard is set by the Japan Electronic Industry Development Association, and formally it is called the Digital Still Camera Image File Format Standard. Exif files contain either uncompressed TIFF images or compressed JPEG images. In addition, these files can contain many comment fields of various types.
Here is a sample of the metadata that can be extracted from a jpeg file:
(Image properties shown in Windows XP)
Besides camera manufacturers, different software and hardware vendors endorse and support the standard as well. Therefore, you can be assured that the code to extract this data will work for most photos and will not break in the future.
For the sake of simplicity, this project uses a standard property file, and not an XML file. The two properties
are defined in the a.property file, which is located in the root project folder. If the project will be converted to the J2EE structure, this file should be placed somewhere under the WEB-INF/ folder.
For further expandability and maintainability, I separated the back-end into several packages. The beans package will hold a representation of the real work objects (such as Album), the sys holds the main execution and initialization classes, and the filters and util packages hold the helper classes. See the complete project tree below.
The back-end engine algorithms that encapsulate the date and sort logic are contained in several beans, several utility classes, and a few helper classes. The main method is called from a GetResourceFiles class.
The InitSystem class initiates all the properties (from the a.property file) associated with the project such as logs, names of image and thumbnail folders, and so forth.