Generate Thumbnail in Java using Thumbnailator library





3207

We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.

Generate Thumbnail in Java using Thumbnailatorlibrary

In our work there will be situation where we need to resize the image, generate thumbnails and so on. Users need to have little bit of image processing knowledge to achieve it. We have Java ImageIO APIs to achieve these functionalities. As said, we need to be aware of or spend time in learning these APIs. To help us, Thumbnailator library provides easy fluent style API and generates thumbnail in simple three lines of code.

Thumbnailator library is a wrapper to Java imageio API. It provides functionality to generate thumbnail, resize images. image conversion from one format to other.

Maven Dependency:

<dependency>

<groupId>net.coobird</groupId>

<artifactId>thumbnailator</artifactId>

<version>[0.4, 0.5)</version>

</dependency>

Below code helps to generate thumbnail. The output can be of same format or different.

Thumbnails.of(new File(INPUT_PATH, "test1.png"))

.size(250, 250)

.toFile(new File(OUTPUT_PATH, "output.png"));

Thumbnails.of(new File(INPUT_PATH, "test1.png"))

.size(250, 250)

.toFile(new File(OUTPUT_PATH, "output.jpg"));

If you want to rotate the image

Thumbnails.of(new File(INPUT_PATH, "test1.png"))

.size(250, 250)

.rotate(90)

.toFile(new File(INPUT_PATH, "output.png"));

To add watermark

Thumbnails.of(new File(INPUT_PATH, "test1.png"))

.size(250, 250)

.watermark(Positions.CENTER, ImageIO.read(new File(BASE_PATH, "watermark.png")), 0.5f)

.toFile(new File(OUTPUT_PATH, "output.png"));

if you bunch of files in a directory and you want to convert generate thumbnail for all of them

Thumbnails.fromFiles(Arrays.asList(new File(INPUT_PATH).listFiles()))

.size(250, 250)

.asFiles(new File(OUTPUT_PATH), Rename.PREFIX_DOT_THUMBNAIL);

Thumbnailator is simple image processing library where you can use to do thumbnail related task. It is not for heavy image processing but it can help you for smaller task.

Reference:

https://github.com/coobird/thumbnailator

http://www.findbestopensource.com/tagged/image-library

http://www.findbestopensource.com/tagged/image-processing