Image processing library——Thumbnailator

Foreword:

Introduction:

Quick Start:

Introduce dependencies:

API example:

Image Compression

Method 1: Equal-proportional compression

Method 2: Specify equal-proportional compression of the px value (it will not destroy the image ratio, so if the px value is not proportional, there will be a small gap with expectations)

Method 3: Non-proportional compression (will destroy the image proportions, that is, the image will be deformed by the specified proportion)

Picture rotation

add watermark 

Modify file format

Do not change width and height, compress size 

Comprehensive actual combat

Foreword:

        For Java web server developers, the management of image resources is always an unavoidable part. Many websites provide the function of uploading pictures, and modern digital equipment shoots high-definition pictures with very high resolution and takes up a lot of space. The physical storage problem is relatively easy to solve, but the network bandwidth is too tight. A page moves only tens of MB. The loading speed is enough if you think about it. Therefore, image compression is essential. For image processing, both the front and back ends can be implemented. Here we explain the tools for image processing on the back end. Library - Thumbnailator


Introduction:

        Thumbnailator is a Java class library used to generate image thumbnails . It can generate image thumbnails through very simple code, or directly generate thumbnails for an entire directory of images.

        What can Thumbnailator do?

        Thumbnailator is a high-performance java thumbnail class library. Supported processing operations: image scaling, area cropping, watermarking, rotation, maintaining proportion.


Quick Start:

Introduce dependencies:

<dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
  <version>0.4.8</version>
</dependency>

API example:

Image Compression

Method 1: Equal-proportional compression
Thumbnails.of("原图文件的路径")
        .scale(0.5) // 压缩的比例
        .toFile("压缩后文件的路径");
Method 2: Specify equal-proportional compression of the px value (it will not destroy the image ratio, so if the px value is not proportional, there will be a small gap with expectations)
Thumbnails.of("原图文件的路径")
    .size(100, 75) // 长宽的px值
    .toFile("压缩后文件的路径"); 
Method 3: Non- proportional compression ( will destroy the image proportions, that is, the image will be deformed by the specified proportion )
Thumbnails.of("原图文件的路径")
    .scale(1.0, 0.5)  // 长宽的比例,非等比例,会导致图片变型
    .toFile("压缩后文件的路径");

Picture rotation

        Note: The image rotation here must be accompanied by a compression variable of the image. If you only implement image rotation, you can write the compression ratio as 1

Thumbnails.of("原图文件的路径")
    .scale(0.8) // 等比例缩放的比例
    .rotate(90) // 旋转的角度,顺时针
    .toFile("转换后文件的路径");
Thumbnails.of(“原图文件的路径”)
    .size(40,40) // 等比例缩放的具体长宽px值
    .rotate(90)  // 顺时针旋转的角度
    .toFile(“转换后文件的路径”);

scale,size must have a value


add watermark 

File image = new File("水印图片地址");
Thumbnails.of("原图文件的路径")
    .scale(0.8)  // 原图压缩的比例
    // Watermark:添加水印 watermark(位置,水印图,透明度)
    .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(image), 0.5f)
    .toFile("转换后文件的路径");

Modify file format

Thumbnails.of(“原图文件的路径”)
    .scale(1f) // 原图等比例缩放的比例,1f表示不缩放,不能省略
    .outputFormat(“jpg”) // 转换后的格式
    .toFile(“转换后文件的路径”);  // 这里要注意哦,路径最后要写上“生成的文件名.后缀”

        outputFormat: Set the converted image format. I heard before that when this attribute is set to png, the scale scaling attribute is invalid. The test version 0.4.8 has been able to scale normally.


Do not change width and height, compress size 

Thumbnails.of("原图文件的路径")
        .scale(1f) // 原图等比例缩放的比例,1f表示不缩放,不能省略 
        .outputQuality(0.5f) // 输出的图片质量,范围:0.0~1.0,1为最高质量,大小最大
        .toFile("压缩后文件的路径");

Comprehensive actual combat:

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;
import net.coobird.thumbnailator.resizers.configurations.ScalingMode;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author wzx
 * @version 1.0
 * @description 生成缩略图和水印非常好用,具体帮助文档 https://github.com/coobird/thumbnailator/wiki/Maven
 *              缩略图
 *              水印
 *              选择
 *              格式转换
 * @Create 2023/8/22
 */
public class ThumbnailatorDmo {

    /**
     * 使用给定的图片生成指定大小的图片
     */
    private static void generateFixedSizeImage(){
        try {
            Thumbnails.of("data/meinv.jpg").size(80,80).toFile("data/newmeinv.jpg");
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }

    /**
     * 对原图加水印,然后顺时针旋转90度,最后压缩为80%保存
     */
    private static void generateRotationWatermark(){
        try {
            Thumbnails.of("data/2016010208.jpg").
                    size(160,160). // 缩放大小
                    rotate(90). // 顺时针旋转90度
                    watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("data/newmeinv.jpg")),0.5f). //水印位于右下角,半透明
                    outputQuality(0.8). // 图片压缩80%质量
                    toFile("data/2016010208_new.jpg");
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }

    /**
     * 转换图片格式,将流写入到输出流
     */
    private static void generateOutputstream(){
        try(OutputStream outputStream = new FileOutputStream("data/2016010208_outputstream.png")) { //自动关闭流
            Thumbnails.of("data/2016010208.jpg").
                    size(500,500).
                    outputFormat("png"). // 转换格式
                    toOutputStream(outputStream); // 写入输出流
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }

    /**
     * 按比例缩放图片
     */
    private static void generateScale(){
        try {
            Thumbnails.of("data/2016010208.jpg").
                    //scalingMode(ScalingMode.BICUBIC).
                    scale(0.8). // 图片缩放80%, 不能和size()一起使用
                    outputQuality(0.8). // 图片质量压缩80%
                    toFile("data/2016010208_scale.jpg");
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }

    /**
     * 生成缩略图到指定的目录
     */
    private static void generateThumbnail2Directory(){
        try {
            Thumbnails.of("data/2016010208.jpg","data/meinv.jpg").
                    //scalingMode(ScalingMode.BICUBIC).
                            scale(0.8). // 图片缩放80%, 不能和size()一起使用
                   toFiles(new File("data/new/"), Rename.NO_CHANGE);//指定的目录一定要存在,否则报错
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }

    /**
     * 将指定目录下所有图片生成缩略图
     */
    private static void generateDirectoryThumbnail(){
        try {
            Thumbnails.of(new File("data/new").listFiles()).
                    //scalingMode(ScalingMode.BICUBIC).
                            scale(0.8). // 图片缩放80%, 不能和size()一起使用
                    toFiles(new File("data/new/"), Rename.SUFFIX_HYPHEN_THUMBNAIL);//指定的目录一定要存在,否则报错
        } catch (IOException e) {
            System.out.println("原因: " + e.getMessage());
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_73077810/article/details/132427849