JAVA开发经验(二):常用工具类2.5-IO-图片压缩工具类(ThumbnailsUtil)

摘要说明:

ThumbnailsUtil主要是整合Thumbnailator库中的图片压缩类;主要包括图片压缩,指定大小,比例,旋转,水印等

Thumbnailator主要提供一系列可配置的项进行组合生成高质量的缩略图;

Maven依赖:

	<dependencies>
	<!-- Java的缩略图生成库 -->
		<dependency>
			<groupId>net.coobird</groupId>
			<artifactId>thumbnailator</artifactId>
			<version>0.4.8</version>
		</dependency>
	</dependencies>

java工具类:

ThumbnailsUtil工具类主要方法如下:

  • compressOfSize:按照尺寸对图片进行缩略
  • compressOfScale: 按照比例对图片进行压缩
  • watermark:给图片添加水印,生成新图片
  • watermarkSelf:给图片添加水印并覆盖原图片
package com.tit.taf.io.img;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

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

/**
 * @模块名:taf
 * @包名:com.tit.taf.io.img
 * @类名称: ThumbnailsUtil
 * @类描述:【类描述】图片压缩工具类;更多可参考:https://coobird.github.io/thumbnailator/javadoc/0.4.8/
 * @版本:1.0
 * @创建人:cc
 * @创建时间:2018年8月29日上午10:51:18
 */

public class ThumbnailsUtil {

    /**
     * 
     * @方法名:compressOfSize
     * @方法描述【方法功能描述】按照尺寸对图片进行缩略
     * @param file1 原始文件
     * @param file2 压缩文件
     * @param width 宽度
     * @param height 高度
     * @param keep 是否保持比例
     * @param allowOverwrite 是否覆盖已存在的目标文件
     * @param rotate 顺时针旋转度数,可为空,如90
     * @param quality 压缩质量,可为空,如0.8
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 下午1:40:02
     * @修改人:cc
     * @修改时间:2018年8月29日 下午1:40:02
     */
    public static void compressOfSize(File file1, File file2, int width, int height, boolean keep,
            boolean allowOverwrite, Double rotate, Double quality) throws IOException {
        if (rotate == null && quality == null) {
            Thumbnails.of(file1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite).toFile(file2);
        }
        else if (rotate == null) {
            Thumbnails.of(file1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .outputQuality(quality).toFile(file2);
        }
        else if (quality == null) {
            Thumbnails.of(file1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .rotate(rotate).toFile(file2);
        }
        else {
            Thumbnails.of(file1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .rotate(rotate).outputQuality(quality).toFile(file2);
        }

    }

    /**
     * 
     * @方法名:compressOfSize
     * @方法描述【方法功能描述】按照尺寸对图片进行缩略
     * @param filePath1 原始文件路径
     * @param filePath2 压缩文件路径
     * @param width 宽度
     * @param height 高度
     * @param keep 是否保持比例
     * @param allowOverwrite 是否覆盖已存在的目标文件
     * @param rotate 顺时针旋转度数,可为空,如90
     * @param quality 压缩质量,可为空,如0.8
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 上午11:01:57
     * @修改人:cc
     * @修改时间:2018年8月29日 上午11:01:57
     */
    public static void compressOfSize(String filePath1, String filePath2, int width, int height, boolean keep,
            boolean allowOverwrite, Double rotate, Double quality) throws IOException {
        if (rotate == null && quality == null) {
            Thumbnails.of(filePath1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .toFile(filePath2);
        }
        else if (rotate == null) {
            Thumbnails.of(filePath1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .outputQuality(quality).toFile(filePath2);
        }
        else if (quality == null) {
            Thumbnails.of(filePath1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .rotate(rotate).toFile(filePath2);
        }
        else {
            Thumbnails.of(filePath1).size(width, height).keepAspectRatio(keep).allowOverwrite(allowOverwrite)
                    .rotate(rotate).outputQuality(quality).toFile(filePath2);
        }
    }

    /**
     * 
     * @方法名:compressOfScale
     * @方法描述【方法功能描述】按照比例对图片进行压缩
     * @param file1 原文件
     * @param file2 压缩文件
     * @param scale 比例
     * @param rotate 顺时针旋转度数,可为空,如90
     * @param quality 压缩质量,可为空,如0.8
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 上午11:04:50
     * @修改人:cc
     * @修改时间:2018年8月29日 上午11:04:50
     */
    public static void compressOfScale(File file1, File file2, Double scale, Double rotate, Double quality)
            throws IOException {
        if (rotate == null && quality == null) {
            Thumbnails.of(file1).scale(scale).toFile(file2);
        }
        else if (rotate == null) {
            Thumbnails.of(file1).scale(scale).outputQuality(quality).toFile(file2);
        }
        else if (quality == null) {
            Thumbnails.of(file1).scale(scale).rotate(rotate).toFile(file2);
        }
        else {
            Thumbnails.of(file1).scale(scale).rotate(rotate).outputQuality(quality).toFile(file2);
        }
    }

    /**
     * 
     * @方法名:compressOfScale
     * @方法描述【方法功能描述】按照比例对图片进行压缩
     * @param filePath1 原文件路径
     * @param filePath2 压缩文件路径
     * @param scale 比例
     * @param rotate 顺时针旋转度数,可为空,如90
     * @param quality 压缩质量,可为空,如0.8
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 上午11:05:54
     * @修改人:cc
     * @修改时间:2018年8月29日 上午11:05:54
     */
    public static void compressOfScale(String filePath1, String filePath2, double scale, Double rotate, Double quality)
            throws IOException {
        if (rotate == null && quality == null) {
            Thumbnails.of(filePath1).scale(scale).toFile(filePath2);
        }
        else if (rotate == null) {
            Thumbnails.of(filePath1).scale(scale).outputQuality(quality).toFile(filePath2);
        }
        else if (quality == null) {
            Thumbnails.of(filePath1).scale(scale).rotate(rotate).toFile(filePath2);
        }
        else {
            Thumbnails.of(filePath1).scale(scale).rotate(rotate).outputQuality(quality).toFile(filePath2);
        }
    }

    /**
     * 
     * @方法名:watermark
     * @方法描述【方法功能描述】给图片添加水印,生成新图片
     * @param file1 原图片
     * @param file2 结果图片
     * @param file3 水印图片
     * @param positions 水印位置,如Positions.CENTER
     * @param opacity 透明度:0.5f
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 下午2:09:43
     * @修改人:cc
     * @修改时间:2018年8月29日 下午2:09:43
     */
    public static void watermark(File file1, File file2, File file3, Positions positions, float opacity)
            throws IOException {
        Thumbnails.of(file1).scale(1).watermark(positions, ImageIO.read(file3), opacity).toFile(file2);
    }

    /**
     * 
     * @方法名:watermark
     * @方法描述【方法功能描述】给图片添加水印并覆盖原图片
     * @param file1 原图片
     * @param file2 水印图片
     * @param positions 水印位置,如Positions.CENTER
     * @param opacity 透明度:0.5f
     * @throws IOException
     * @修改描述【修改描述】
     * @版本:1.0
     * @创建人:cc
     * @创建时间:2018年8月29日 下午2:09:43
     * @修改人:cc
     * @修改时间:2018年8月29日 下午2:09:43
     */
    public static void watermarkSelf(File file1, File file2, Positions positions, float opacity) throws IOException {
        Thumbnails.of(file1).scale(1).watermark(positions, ImageIO.read(file2), opacity).toFile(file1);
    }

    public static void main(String[] args) {
        try {
            ThumbnailsUtil
                    .watermarkSelf(new File("d://logo-7.jpg"), new File("d://logo-7.jpg"), Positions.CENTER, 0.5f);
        }
        catch (IOException e) {
        }
    }
}

猜你喜欢

转载自blog.csdn.net/u010904188/article/details/82182543
今日推荐