Java graphics processing framework thumbnail

Posted by definewebsites on Sat, 19 Feb 2022 18:03:23 +0100

Thumbnail is a thumbnail generation library for Java. Supported processing operations: image scaling, region clipping, watermark, rotation, maintaining proportion.

1. Why use thumbnail

Making high-quality thumbnails in Java is a very difficult task.

Learn how to use image I/O API, Java 2D API, image processing, image scaling technology,... But don't be afraid! Thumbnailator will take care of all these things for you!

Thumbnailator is a separate JAR file that does not rely on external libraries, which makes development and deployment simple and easy. It is also available in the Maven central repository for inclusion in the Maven project.

2. How simple is a thumbnail?

The smooth interface of thumbnail can be used to perform quite complex thumbnail processing tasks in a simple step.

For example, create JPEG thumbnail image files in a directory, and all sizes are adjusted to the maximum 640 pixels × 480 pixels while maintaining the aspect ratio of the original image can be performed by the following methods:

Thumbnails.of(new File("path/to/directory").listFiles())
    .size(640, 480)

The smooth interface provided by thumbnail simplifies the task of converting thumbnails into a single method call!

There is no need to access the image I/O API and manually operate buffereimages through the Graphics2D object. Thumbnailator will do this for you.

3. Thumbnailator property

  • Create high-quality thumbnails from existing images.
  • Option to embed watermarks (such as logos) in thumbnails.
  • The transparency of the watermark can be adjusted from transparent (0%) to opaque (100%).
  • Supports the rotation of thumbnails.
  • A smooth interface to simplify the process of making thumbnails.
    For example, the code in the previous example of rotating thumbnails is as follows:
for (int i : new int[] {0, 90, 180, 270, 45}) {
    Thumbnails.of(new File("coobird.png"))
            .size(100, 100)
            .toFile(new File("image-rotated-" + i + ".png"));
  • Thumbnail generation in multiple quality modes.
  • The resulting thumbnail of the retained aspect ratio.

4. Thumbnail example

4.1 creating thumbnails from image files

Thumbnails.of(new File("original.jpg"))
        .size(160, 160)
        .toFile(new File("thumbnail.jpg"));

In this example, the image in the original JPG is resized and saved as thumbnail jpg.

Alternatively, the thumbnail will accept the File name as a string. You do not need to use the File object to specify the image File:

        .size(160, 160)

This form is useful when writing rapid prototyping code, or when using thumbnail from a scripting language.

4.2 create a thumbnail with rotation and watermark

Thumbnails.of(new File("original.jpg"))
        .size(160, 160)
        .watermark(Positions.BOTTOM_RIGHT, File("watermark.png")), 0.5f)
        .toFile(new File("image-with-watermark.jpg"));

In this example, the original jpg image is resized, then rotated 90 degrees clockwise, and then the watermark is placed in the lower right corner, which is translucent, and then saved to image with watermark jpg and 80% compression quality settings.

4.3 create a thumbnail and write an output stream

OutputStream os = ...;
        .size(200, 200)

In this example, from the file large picture The JPG image is adjusted to a maximum size of 200 x 200 (maintaining the aspect ratio of the original image) and written to the specified OutputStream as a PNG image.

4.4 create fixed size thumbnails

BufferedImage originalImage = File("original.png"));

BufferedImage thumbnail = Thumbnails.of(originalImage)
        .size(200, 200)

The above code takes an image in the originalImage and creates a 200 image using thumbnail × 200 pixel thumbnails and store the results in thumbnail.

4.5 zoom the image by a given factor

BufferedImage originalImage = File("original.png"));

BufferedImage thumbnail = Thumbnails.of(originalImage)

The above code takes the image in originalImage as an example, creates 25% thumbnails of the original image, and uses the default scaling technology to generate thumbnails stored in thumbnail.

4.6 rotating images when creating thumbnails

BufferedImage originalImage = File("original.jpg"));

BufferedImage thumbnail = Thumbnails.of(originalImage)
        .size(200, 200)

The above code creates a thumbnail rotated 90 degrees clockwise based on the original image.

4.7 creating thumbnails with watermarks

BufferedImage originalImage = File("original.jpg"));
BufferedImage watermarkImage = File("watermark.png"));

BufferedImage thumbnail = Thumbnails.of(originalImage)
        .size(200, 200)
        .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)

As shown in the figure, the watermark can be added to the thumbnail by calling the watermark method.

The location can be selected from the location enumeration.

The opacity of the thumbnail (or conversely, the transparency) can be adjusted by changing the last parameter, where 0.0f is that the thumbnail is completely transparent and 1.0f is that the watermark is completely opaque.

4.8 writing thumbnails to a specific directory

File destinationDir = new File("path/to/output");

Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
        .size(200, 200)
        .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);

This example will take the source images, write their thumbnails as files to the destinationdir (path / to / output directory), and name them with thumbnail. Appended to the file name.

Therefore, the thumbnail will be written as a file in:

  • path/to/output/
  • path/to/output/thumbnail.banana.jpg
  • path/to/output/thumbnail.cherry.jpg

You can also keep the original file name when writing to the specified directory:

File destinationDir = new File("path/to/output");

Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
        .size(200, 200)
        .toFiles(destinationDir, Rename.NO_CHANGE);

In the above code, the thumbnail will be written as:

  • path/to/output/apple.jpg
  • path/to/output/banana.jpg
  • path/to/output/cherry.jpg

Since: Thumbnailator 0.4.7

Topics: Java