Java thumbnailator 图片处理,指定大小缩放、旋转、裁剪、格式转换

发布时间:2023年12月29日

依赖:

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

代码:



import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

import java.awt.image.BufferedImage;
import java.io.*;

public class ThumbnailUtils {


    /**
     * 图片指定大小缩放
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(InputStream inputStream, int width, int height, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(inputStream)
                .size(width, height)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toFile(outFilePath);
    }

    /**
     * 图片按照比例进行缩放
     * @param inputStream
     * @param ratio 比例
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(InputStream inputStream, double ratio, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(inputStream)
                .scale(ratio)
                .outputQuality(outputQuality)
                .toFile(outFilePath);
    }

    /**
     * 图片旋转
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param rotate 角度
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(InputStream inputStream, int width, int height, int rotate, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(inputStream)
                .size(width, height)
                .rotate(rotate)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toFile(outFilePath);
    }

    /**
     * 图片格式转换
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param outputFormat 图片类型
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(InputStream inputStream, int width, int height, String outputFormat, double outputQuality,String outFilePath) throws IOException {
        Thumbnails.of(inputStream)
                .size(width, height)
                .outputFormat(outputFormat)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toFile(outFilePath);
    }


    /**
     * 图片指定大小缩放
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(String filePath, int width, int height, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(filePath)
                .size(width, height)
                .outputQuality(outputQuality)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toFile(outFilePath);
    }

    /**
     * 图片按照比例进行缩放
     * @param filePath 文件路径
     * @param ratio 比例
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(String filePath, double ratio, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(filePath)
                .scale(ratio)
                .outputQuality(outputQuality)
                .toFile(outFilePath);
    }

    /**
     * 图片旋转
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param rotate 角度
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(String filePath, int width, int height, int rotate, double outputQuality, String outFilePath) throws IOException {
        Thumbnails.of(filePath)
                .size(width, height)
                .rotate(rotate)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toFile(outFilePath);
    }

    /**
     * 图片格式转换
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputFormat 图片类型
     * @param outputQuality 图片质量(0~1)
     * @param outFilePath 存储路径
     */
    public static void thumbnail(String filePath, int width, int height, String outputFormat, double outputQuality,String outFilePath) throws IOException {
        Thumbnails.of(filePath)
                .size(width, height)
                .outputFormat(outputFormat)
                .keepAspectRatio(false)
                .outputQuality(outputQuality)
                .toFile(outFilePath);
    }




    /**
     * 图片指定大小缩放
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(InputStream inputStream, int width, int height, double outputQuality) throws IOException {
        return Thumbnails.of(inputStream)
                .size(width, height)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .asBufferedImage();
    }

    /**
     * 图片按照比例进行缩放
     * @param inputStream
     * @param ratio 比例
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(InputStream inputStream, double ratio, double outputQuality) throws IOException {
        return Thumbnails.of(inputStream)
                .scale(ratio)
                .outputQuality(outputQuality)
                .asBufferedImage();
    }

    /**
     * 图片旋转
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param rotate 角度
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(InputStream inputStream, int width, int height, int rotate, double outputQuality) throws IOException {
        return Thumbnails.of(inputStream)
                .size(width, height)
                .rotate(rotate)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .asBufferedImage();
    }

    /**
     * 图片格式转换
     * @param inputStream
     * @param width 宽
     * @param height 高
     * @param outputFormat 图片类型
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(InputStream inputStream, int width, int height, double outputQuality, String outputFormat) throws IOException {
        return Thumbnails.of(inputStream)
                .size(width, height)
                .outputFormat(outputFormat)
                .keepAspectRatio(false)
                .outputQuality(outputQuality)
                .asBufferedImage();
    }


    /**
     * 图片指定大小缩放
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(String filePath, int width, int height, double outputQuality) throws IOException {
        return Thumbnails.of(filePath)
                .size(width, height)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .asBufferedImage();
    }

    /**
     * 图片按照比例进行缩放
     * @param filePath 文件路径
     * @param ratio 比例
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(String filePath, double ratio, double outputQuality) throws IOException {
        return Thumbnails.of(filePath)
                .scale(ratio)
                .outputQuality(outputQuality)
                .asBufferedImage();
    }

    /**
     * 图片旋转
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param rotate 角度
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(String filePath, int width, int height, int rotate, double outputQuality) throws IOException {
        return Thumbnails.of(filePath)
                .size(width, height)
                .rotate(rotate)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .asBufferedImage();
    }

    /**
     * 图片格式转换
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputFormat 图片类型
     * @param outputQuality 图片质量(0~1)
     */
    public static BufferedImage thumbnailBufferedImage(String filePath, int width, int height, double outputQuality, String outputFormat) throws IOException {
        return Thumbnails.of(filePath)
                .size(width, height)
                .outputFormat(outputFormat)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .asBufferedImage();
    }

    /**
     * 图片指定大小缩放
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputQuality 图片质量(0~1)
     */
    public static OutputStream thumbnailOutputStream(String filePath, int width, int height, double outputQuality) throws IOException {

        OutputStream os = new FileOutputStream(filePath);
        Thumbnails.of(filePath)
                .size(width, height)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toOutputStream(os);

        return os;
    }

    /**
     * 图片按照比例进行缩放
     * @param filePath 文件路径
     * @param ratio 比例
     * @param outputQuality 图片质量(0~1)
     */
    public static OutputStream thumbnailOutputStream(String filePath, double ratio, double outputQuality) throws IOException {

        OutputStream os = new FileOutputStream(filePath);

        Thumbnails.of(filePath)
                .scale(ratio)
                .outputQuality(outputQuality)
                .toOutputStream(os);

        return os;
    }

    /**
     * 图片旋转
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param rotate 角度
     * @param outputQuality 图片质量(0~1)
     */
    public static OutputStream thumbnailOutputStream(String filePath, int width, int height, int rotate, double outputQuality) throws IOException {

        OutputStream os = new FileOutputStream(filePath);

        Thumbnails.of(filePath)
                .size(width, height)
                .rotate(rotate)
                .outputQuality(outputQuality)
                .keepAspectRatio(false)
                .toOutputStream(os);

        return os;
    }

    /**
     * 图片格式转换
     * @param filePath 文件路径
     * @param width 宽
     * @param height 高
     * @param outputFormat 图片类型
     * @param outputQuality 图片质量(0~1)
     */
    public static OutputStream thumbnailOutputStream(String filePath, int width, int height, double outputQuality, String outputFormat) throws IOException {

        OutputStream os = new FileOutputStream(filePath);

        Thumbnails.of(filePath)
                .size(width, height)
                .outputFormat(outputFormat)
                .keepAspectRatio(false)
                .outputQuality(outputQuality)
                .toOutputStream(os);

        return os;
    }

    /**
     * 截取图片
     * @param inputStream 文件流
     * @param position 截取区域
     * @param sWidth 截取区域宽
     * @param sHeight 截取区域高
     * @param tWidth 目标图片宽
     * @param tHeight 目标图片高
     * @param outputQuality 图片质量(0~1)
     * @return
     * @throws IOException
     */
    public static BufferedImage cutImage(InputStream inputStream, Position position, int sWidth, int sHeight, int tWidth, int tHeight, double outputQuality) throws IOException {
        return Thumbnails.of(inputStream)
                .sourceRegion(position, sWidth, sHeight)
                .size(tWidth, tHeight)
                .keepAspectRatio(false)
                .outputQuality(outputQuality)
                .asBufferedImage();
    }
}
文章来源:https://blog.csdn.net/weixin_49050997/article/details/134373222
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。