spring 集成图片压缩工具Thumbnails

第一步:jar 文件依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.digipower</groupId>
		<artifactId>digipower-ucas-core</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>digipower-ucas-core-picture</artifactId>
	
	 <organization>
		<name> The first choice provider for information construction of  archives</name>
		<url>http://www.digipower.cn/</url>
	</organization>

	<!-- 集中定义管理依赖版本号 -->
	<properties>
		<thumbnailator.version>0.4.8</thumbnailator.version>
	</properties>

	<!-- 图片压缩工具依赖jar 文件 -->
	<dependencies>
		<dependency>
			<groupId>net.coobird</groupId>
			<artifactId>thumbnailator</artifactId>
			<version>${thumbnailator.version}</version>
		</dependency>
	</dependencies>
</project>

第二步:图片压缩功能代码

package com.digipower.ucas.picture.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.io.output.ByteArrayOutputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;

/**
 * 
 * @ClassName:  PictureUtil   
 * @Description: 图片压缩工具类  
 * @author: **** -zzg
 * @date:   2019年4月24日 上午9:17:48   
 *     
 * @Copyright: 2019 www.digipower.cn 
 * 注意:本内容仅限于深圳市***开发有限公司内部使用,禁止用于其他的商业目的
 */ 

 
public class PictureUtil {
	/**
	 * 指定大小进行缩放 若图片横比width小,高比height小,不变 若图片横比width小,高比height大,高缩小到height,图片比例不变
	 * 若图片横比width大,高比height小,横缩小到width,图片比例不变
	 * 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param suffix
	 * 			  图片后缀名:jpg、png
	 * @throws IOException
	 */
	public static void imgThumb(String source, String output, int width,
			int height, float quality, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height).outputQuality(quality)
				.outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 
	 * @Title: imgThumbOutputStream   
	 * @Description: 图片压缩转换成输出流进行输出   
	 * @param: @param source
	 * @param: @param width
	 * @param: @param height
	 * @param: @param quality
	 * @param: @param suffix
	 * @param: @return
	 * @param: @throws IOException      
	 * @return: OutputStream      
	 * @throws
	 */
	public static OutputStream imgThumbOutputStream(String source, int width,
			int height, float quality , String suffix) throws IOException {
 
		OutputStream os = new ByteArrayOutputStream();
		Thumbnails.of(source).size(width, height).outputQuality(quality)
				.outputFormat(suffix).toOutputStream(os);
		return os;
	}
 
	/**
	 * 指定大小进行缩放
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param suffix
	 *            图片后缀名称
	 * @throws IOException
	 */
	public static void imgThumb(File source, String output, int width,
			int height, float quality, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height).outputQuality(quality)
				.outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 按照比例进行缩放(图片路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param scale
	 *            缩放百分比
	 * @param suffix
	 *            图片后缀名
	 * @throws IOException
	 */
	public static void imgScale(String source, String output, double scale, String suffix)
			throws IOException {
		Thumbnails.of(source).scale(scale).outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 按照比例进行缩放(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param scale
	 *            缩放百分比
	 * @param suffix
	 *            图片后缀名
	 * @throws IOException
	 */
	public static void imgScale(File source, String output, double scale, String suffix)
			throws IOException {
		Thumbnails.of(source).scale(scale).outputFormat("jpg").toFile(output);
	}
 
	/**
	 * 按照比例进行缩放(输出流)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param scale
	 *            缩放百分比
	 * @param suffix
	 *            图片后缀名
	 * @throws IOException
	 */
	public static OutputStream imgScaleOutputStream(File source, double scale, String suffix)
			throws IOException {
		OutputStream os = new ByteArrayOutputStream();
		Thumbnails.of(source).scale(scale).outputFormat(suffix)
				.toOutputStream(os);
		return os;
	}
 
	/**
	 * 按照比例进行缩放(字节数组)
	 *
	 * @param source
	 *            输入字节数组
	 * @param scale
	 *            缩放百分比
	 * @param suffix
	 *            图片后缀名
	 * @throws IOException
	 */
	public static byte[] imgScaleByte(byte[] source, double scale, String suffix)
			throws IOException {
 
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		InputStream input = new ByteArrayInputStream(source);
		Thumbnails.of(input).scale(scale).outputFormat(suffix)
				.toOutputStream(os);
		return os.toByteArray();
	}
 
	/**
	 * 不按照比例,指定大小进行缩放(图片文件路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 * @param suffix
	 *            图片后缀名
	 */
	public static void imgNoScale(String source, String output, int width,
			int height, boolean keepAspectRatio, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height)
				.keepAspectRatio(keepAspectRatio).outputFormat(suffix)
				.toFile(output);
	}
	
	/**
	 * 不按照比例,指定大小进行缩放(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 * @param suffix
	 *            图片后缀名
	 */
	public static void imgNoScale(File source, String output, int width,
			int height, boolean keepAspectRatio, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height)
				.keepAspectRatio(keepAspectRatio).outputFormat(suffix)
				.toFile(output);
	}
 
	/**
	 * 旋转 ,正数:顺时针 负数:逆时针(图片文件路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param rotate
	 *            角度
	 * @param suffix
	 *            图片后缀名
	 */
	public static void imgRotate(String source, String output, int width,
			int height, double rotate, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height).rotate(rotate)
				.outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 旋转 ,正数:顺时针 负数:逆时针(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param rotate
	 *            角度
	 * @param suffix
	 *            图片后缀名
	 */
	public static void imgRotate(File source, String output, int width,
			int height, double rotate, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height).rotate(rotate)
				.outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 水印(图片文件路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输入源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param position
	 *            水印位置 Positions.BOTTOM_RIGHT o.5f
	 * @param watermark
	 *            水印图片地址
	 * @param transparency
	 *            透明度 0.5f
	 * @param quality
	 *            图片质量 0.8f
	 * @param suffix
	 *            图片后缀名
	 */
	public static void imgWatermark(String source, String output, int width,
			int height, Position position, String watermark,
			float transparency, float quality, String suffix) throws IOException {
		Thumbnails
				.of(source)
				.size(width, height)
				.watermark(position, ImageIO.read(new File(watermark)),
						transparency).outputQuality(0.8f).outputFormat(suffix)
				.toFile(output);
	}
	
	/**
	 * 水印(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输入源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param position
	 *            水印位置 Positions.BOTTOM_RIGHT o.5f
	 * @param watermark
	 *            水印图片地址
	 * @param transparency
	 *            透明度 0.5f
	 * @param quality
	 *            图片质量 0.8f
	 * @param suffix
	 *            图片后缀名称
	 */
 
	public static void imgWatermark(File source, String output, int width,
			int height, Position position, String watermark,
			float transparency, float quality, String suffix) throws IOException {
		Thumbnails
				.of(source)
				.size(width, height)
				.watermark(position, ImageIO.read(new File(watermark)),
						transparency).outputQuality(0.8f).outputFormat(suffix)
				.toFile(output);
	}
 
	/**
	 * 裁剪图片(图片文件路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param position
	 *            裁剪位置
	 * @param x
	 *            裁剪区域x
	 * @param y
	 *            裁剪区域y
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 * @param suffix
	 *            图片后缀名成
	 */
	public static void imgSourceRegion(String source, String output,
			Position position, int x, int y, int width, int height,
			boolean keepAspectRatio, String suffix) throws IOException {
		Thumbnails.of(source).sourceRegion(position, x, y).size(width, height)
				.keepAspectRatio(keepAspectRatio).outputFormat(suffix)
				.toFile(output);
	}
 
	
	/**
	 * 裁剪图片(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param position
	 *            裁剪位置
	 * @param x
	 *            裁剪区域x
	 * @param y
	 *            裁剪区域y
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 * @param suffix
	 *            图片后缀名成
	 */
	public static void imgSourceRegion(File source, String output,
			Position position, int x, int y, int width, int height,
			boolean keepAspectRatio, String suffix) throws IOException {
		Thumbnails.of(source).sourceRegion(position, x, y).size(width, height)
				.keepAspectRatio(keepAspectRatio).outputFormat(suffix)
				.toFile(output);
	}
 
	/**
	 * 按坐标裁剪(图片路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param x
	 *            起始x坐标
	 * @param y
	 *            起始y坐标
	 * @param x1
	 *            结束x坐标
	 * @param y1
	 *            结束y坐标
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 */
	public static void imgSourceRegion(String source, String output, int x,
			int y, int x1, int y1, int width, int height,
			boolean keepAspectRatio) throws IOException {
		Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height)
				.keepAspectRatio(keepAspectRatio).toFile(output);
	}
	
	/**
	 * 按坐标裁剪(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param x
	 *            起始x坐标
	 * @param y
	 *            起始y坐标
	 * @param x1
	 *            结束x坐标
	 * @param y1
	 *            结束y坐标
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param keepAspectRatio
	 *            默认是按照比例缩放的,值为false 时不按比例缩放
	 * @param suffix
	 *            图片后缀名称
	 */
	public static void imgSourceRegion(File source, String output, int x,
			int y, int x1, int y1, int width, int height,
			boolean keepAspectRatio, String suffix) throws IOException {
		Thumbnails.of(source).sourceRegion(x, y, x1, y1).size(width, height)
				.keepAspectRatio(keepAspectRatio).outputFormat(suffix)
				.toFile(output);
	}
 
	/**
	 * 转化图像格式(图片路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 */
	public static void imgFormat(String source, String output, int width,
			int height, String format) throws IOException {
		Thumbnails.of(source).size(width, height).outputFormat(format)
				.toFile(output);
	}
 
	/**
	 * 转化图像格式(图片路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 * @param suffix
	 *            图片后缀名称
	 */
	public static void imgFormat(File source, String output, int width,
			int height, String format, String suffix) throws IOException {
		Thumbnails.of(source).size(width, height).outputFormat(format)
				.outputFormat(suffix).toFile(output);
	}
 
	/**
	 * 输出到OutputStream(图片路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param suffix
	 *            图片后缀名称
	 * @return toOutputStream(流对象)
	 */
	public static OutputStream imgOutputStream(String source, String output,
			int width, int height, String suffix) throws IOException {
		OutputStream os = new FileOutputStream(output);
		Thumbnails.of(source).size(width, height).outputFormat(suffix)
				.toOutputStream(os);
		return os;
	}
 
	/**
	 * 输出到OutputStream(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param suffix
	 *            图片后缀名称
	 * @return toOutputStream(流对象)
	 */
	public static OutputStream imgOutputStream(File source, String output,
			int width, int height, String suffix) throws IOException {
		OutputStream os = new FileOutputStream(output);
		Thumbnails.of(source).size(width, height).outputFormat(suffix)
				.toOutputStream(os);
		return os;
	}
 
	/**
	 * 输出到BufferedImage(图片文件路径)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 * @param suffix
	 *            图片后缀名称
	 * @return BufferedImage
	 */
	public static BufferedImage imgBufferedImage(String source, String output,
			int width, int height, String format, String suffix) throws IOException {
		BufferedImage buf = Thumbnails.of(source).size(width, height)
				.outputFormat(suffix).asBufferedImage();
		ImageIO.write(buf, format, new File(output));
		return buf;
	}
 
	/**
	 * 输出到BufferedImage(图片文件)
	 *
	 * @param source
	 *            输入源
	 * @param output
	 *            输出源
	 * @param width
	 *            宽
	 * @param height
	 *            高
	 * @param format
	 *            图片类型,gif、png、jpg
	 * @param suffix
	 *            图片后缀名称
	 * @return BufferedImage
	 */
	public static BufferedImage imgBufferedImage(File source, String output,
			int width, int height, String format, String suffix) throws IOException {
		BufferedImage buf = Thumbnails.of(source).size(width, height)
				.outputFormat(suffix).asBufferedImage();
		ImageIO.write(buf, format, new File(output));
		return buf;
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值