coobird.net icon

projects.coobird.net

projects.coobird.net - Thumbnailator - Samples

Sample Code using Thumbnailator

Information provided in this section is from a previous version of Thumbnailator, and does not reflect the current version of the software. For current information, please visit the Thumbnailator project.

Thumbnailator icon

Examples on how to use Thumbnailator is available in this section.

Creating a Thumbnail

Using Thumbnailator to create a thumbnail is simple. The following is a sample program that creates a thumbnail of size 160 x 120 from an image called source.jpg and outputs to a file called thumbnail.jpg:

import net.coobird.thumbnailator.*;

import java.io.*;

/**
 * Using Thumbnailator, this application will produce a thumbnail
 * from a JPEG file and write that out to another JPEG file.  
 * @author coobird
 */
public class MakeThumbnail
{
  public static void main(String[] args)
  {
    try
    {
      THelper.createThumbnail(new File("source.jpg"),
                              new File("thumbnail.jpg"),
                              160, 120);
    }
    catch (IOException e)
    {
      e.printStackTrace(System.out);
    }
  }
}

The THelper class provides convenience methods that simplifies the thumbnail generation process. Here, the THelper.createThumbnail method is used to create a thumbnail from a source image. Only one method call is required to make a thumbnail using Thumbnailator.

Creating a Thumbnail with a Watermark

Creating a thumbnail with a watermark or logo in the image can be achieved with one method call as well. In this example, the THelper.createThumbnail method is used to create a thumbnail of size 160 x 120, from the source image source.jpg with a watermark from watermark.png and outputs to thumbnail.jpg:

import net.coobird.thumbnailator.*;

import java.io.*;

/**
 * Using Thumbnailator, this application will produce a thumbnail
 * from a JPEG file and adds a watermark from a PNG. The resulting
 * thumbnail is then written to another JPEG file.  
 * @author coobird
 */
public class MakeThumbnailWithWatermark
{
  public static void main(String[] args)
  {
    try
    {
      THelper.createThumbnail(new File("source.jpg"),
                              new File("watermark.png"),
                              new File("thumbnail.jpg"),
                              160, 120,
                              TGenerator.NO_ROTATION,
                              TGenerator.RENDER_HIGHEST_QUALITY,
                              TGenerator.WM_BOTTOM_RIGHT,
                              1.0f);
    }
    catch (IOException e)
    {
      e.printStackTrace(System.out);
    }
  }
}

As with the previous example to create a thumbnail, a thumbnail with a watermark can be performed with one method call. Some additional parameters are passed to the method about the rotation of the thumbnail (in this case, none denoted by TGenerator.NO_ROTATION), specification of rendering quality (TGenerator.RENDER_HIGHEST_QUALITY), the location of the watermark (placing it at the bottom-right corner of the thumbnail, TGenerator.WM_BOTTOM_RIGHT) and the opacity of the watermark, in this case, 1.0f which is 100% opacity.


< Return to How to Use Thumbnailator