Use of the net.coobird.thumbnailator.Thumbnails library

1. Brief introduction

net.coobird.thumbnailator.Thumbnailsis a Java library for creating image thumbnails. The library provides a simple API that allows you to create high-quality thumbnails in a few lines of code, and supports various image formats. Here are some key features of the library:

  • Ease of use: ThumbnailsThe class provides a simple API that allows you to create high-quality thumbnails in a few lines of code.

  • Various scaling options: The library provides various scaling options, including proportional scaling, specified width and height scaling, and specified maximum width and height scaling.

  • High Quality Thumbnails: Thumbnails created using the library are of high quality, using some advanced techniques such as subsampling and smooth scaling algorithms.

  • Support for multiple image formats: The library supports various common image formats such as JPEG, PNG, and GIF.

  • Flexible configuration options: The library provides a variety of configuration options, such as setting thumbnail quality, setting output format, and setting output location, etc.

  • Java-based: The library is written in Java and can be easily integrated with Java projects.

2. Purpose

net.coobird.thumbnailator.ThumbnailsLibrary can be used to create image thumbnails in Java applications, common uses include:

  1. Image Presentation: Scale down the original image to fit the screen space to improve user experience.

  2. Image processing: When processing a large number of images, thumbnails can be used to quickly preview and select images, reducing processing time and resource consumption.

  3. Image Upload: When uploading images to a web server, use thumbnails to reduce upload time and bandwidth usage.

  4. Website design: In website design, thumbnails can be used to showcase content such as image galleries or product catalogs to improve user experience and page loading speed.

In summary, net.coobird.thumbnailator.Thumbnailsthe library provides a convenient way to create high-quality thumbnails in Java applications, which can be used in various application scenarios to improve user experience and reduce resource consumption.

 3. Examples

1. Scale the image proportionally:

Thumbnails.of("input.jpg")
    .scale(0.5)
    .toFile("output.jpg");

This example input.jpgshrinks the file to half its original size and saves the result as output.jpga file.

2. Specify the width and height of the thumbnail:

Thumbnails.of("input.jpg")
    .size(200, 200)
    .toFile("output.jpg");

 This example input.jpgshrinks the file to a size of 200 pixels in width and height and saves the result as output.jpga file.

3. Specify the maximum width and height and keep the ratio:

Thumbnails.of("input.jpg")
    .size(800, 800)
    .keepAspectRatio(true)
    .toFile("output.jpg");

This example input.jpgshrinks the file to a size that does not exceed 800 pixels in width and height, maintaining the aspect ratio, and saves the result as output.jpga file.

4. Set the output format and quality:

Thumbnails.of("input.jpg")
    .size(200, 200)
    .outputFormat("png")
    .outputQuality(0.8)
    .toFile("output.png");

This example input.jpgscales down the file to a size of 200 pixels in width and height, sets the output format to PNG format, and sets the output quality to 80%, and saves the result as a output.pngfile.

5. Specify the width, but the height is the same as the original image

Thumbnails.of(coverPath).size(376, Thumbnails.of(coverPath).height(null)).toFile(coverPath);

In this code, size()the first parameter of the method specifies the desired width of 376 pixels, and the second parameter uses to Thumbnails.of(coverPath).height(null)get the height of the original image and specifies it as the height of the thumbnail. This will make the thumbnail the same height as the original image, but the width will be changed to 376 pixels. In the end, the thumbnail will overwrite the original file, because toFile()the parameter of the method is the path to the original file.

6. Specify a width of 376 pixels, and make the height automatically calculated with the height of the original image

Thumbnails.of(coverPath).size(376, 0).toFile(coverPath);

In this code, size()the first parameter of the method specifies the desired width as 376 pixels, and the second parameter is 0, which tells the library to automatically calculate the required height to maintain the aspect ratio of the original image. In the end, the thumbnail will overwrite the original file, because toFile()the parameter of the method is the path to the original file.

Guess you like

Origin blog.csdn.net/CSH__/article/details/129633849