目录
1. Thumbnails简介
2. 导入依赖
3. 图片压缩工具类代码
4. 参数介绍
图像处理API,可以实现图像常用操作(图片信息、扩大、缩小、压缩等)
commons-fileupload
commons-fileupload
1.3.3
net.coobird
thumbnailator
0.4.8
package *;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.PostConstruct;
import java.io.*;
/**
* 压缩工具类
*
* @Author pzy
* @Description: TODO 需要多次测试调整参数
* @Version 0.1.0
*/
public class CompressionUtils {
private static MultipartFile getMulFileByFile(InputStream fis, String fieldName, String contentType, String fileName) {
FileItem fileItem = createFileItem(fis, fieldName, contentType, fileName);
return new CommonsMultipartFile(fileItem);
}
private static FileItem createFileItem(InputStream fis, String fieldName, String contentType, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem item = factory.createItem(fieldName, contentType, false, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
OutputStream os = item.getOutputStream();
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return item;
}
public static MultipartFile compressImg(MultipartFile multiFile) {
// 压缩图片
InputStream inputStream = null;
ByteArrayOutputStream bos = null;
MultipartFile multipartFile = null;
InputStream fileInput = null;
try {
inputStream = multiFile.getInputStream();
bos = new ByteArrayOutputStream();
// 压缩图片核心代码
Thumbnails.Builder extends InputStream> builder = Thumbnails.of(inputStream);
builder
.scale(1f) //等比缩放
.outputQuality(0.3f) //1高质量 -> 0 低质量
.toOutputStream(bos);
fileInput = new ByteArrayInputStream(bos.toByteArray());
// 转换 MultipartFile
String fieldName = multiFile.getName();
String fileName = multiFile.getOriginalFilename();
String contentType = multiFile.getContentType();
multipartFile = getMulFileByFile(fileInput, fieldName, contentType, fileName);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (bos != null) {
bos.close();
}
if (fileInput != null) {
fileInput.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return multipartFile;
}
}
scale 缩放比例 :大于1是放大,小于1是缩小
Builder
package ;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
/**
* 压缩图片处理的公用方法
* 理解成注释也行
*
*
* linux批处理指令 //find /thumbnail/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +49k -exec convert -resize "x300>" -quality 75 {} {} \;
*
* @Author pzy
* @Description: TODO
* @Version 0.1.0
*/
public class CompressionMethod {
//代码
//
// net.coobird
// thumbnailator
// 0.4.8
//
/**
* @param scale 缩放比例 :大于1是放大,小于1是缩小
* @param fromFile 源图片路径
* @param tofile 目的图片路径
* @throws IOException
* @Title: scale
* @Description: 按比例缩放
*/
public static void scale(double scale, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).scale(0.5).toFile(tofile);
}
/**
* @param height 图片高 px
* @param width 图片宽 px
* @param fromFile
* @param tofile
* @throws IOException
* @Title: size
* @Description: 按大小尺寸缩放
*/
public static void size(int height, int width, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).size(width, height).toFile(tofile);
}
/**
* @param height 图片缩小高度至 px
* @param fromFile
* @param tofile
* @throws IOException
* @Title: height
* @Description: 按指定高度缩小图片,等比例调整图片宽度
*/
public static void height(int height, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).height(height).toFile(tofile);
}
/**
* @param width 图片缩小宽度至 px
* @param fromFile
* @param tofile
* @throws IOException
* @Title: width
* @Description: 按指定宽度缩小图片,等比例调整图片高度
*/
public static void width(int width, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).width(width).toFile(tofile);
}
/**
* @param quality 图片质量比例,1 为高质量,越接近0图片质量越低
* @param fromFile
* @param tofile
* @throws IOException
* @Title: outputQuality
* @Description: 设置图片质量,一般与缩放功能同时使用
*/
public static void outputQuality(float quality, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).outputQuality(quality).toFile(tofile);
}
/**
* @param position 位置
* @param image 图片
* @param opacity 透明度
* @param fromFile
* @param tofile
* @throws IOException
* @Title: watermark
* @Description: 添加图片水印
*/
public static void watermark(Position position, BufferedImage image, float opacity, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).watermark(position, image, opacity).toFile(tofile);
}
/**
* @param angle 正数则为顺时针,负数则为逆时针
* @param fromFile
* @param tofile
* @throws IOException
* @Title: rotate
* @Description: 图片旋转
*/
public static void rotate(double angle, String fromFile, String tofile) throws IOException {
Thumbnails.of(fromFile).rotate(angle).toFile(tofile);
}
/**
* @param position 图片位置
* @param width 裁剪宽
* @param height 裁剪高
* @param fromFile
* @param tofile
* @throws IOException
* @Title: sourceRegion
* @Description: 裁剪图片
*/
public static void sourceRegion(Position position, int width, int height, String fromFile, String tofile) throws IOException {
// Thumbnails.of(fromFile).sourceRegion(Positions.TOP_CENTER, width, height).toFile(tofile);
Thumbnails.of(fromFile).sourceRegion(position, width, height).toFile(tofile);
}
/**
* @param fromDir
* @param tofile
* @throws IOException
* @Title: listfiles
* @Description: 批量处理文件,可与前几个方法结合起来处理
*/
public static void listfiles(String fromDir, String tofile) throws IOException {
Thumbnails.of(Objects.requireNonNull(new File(fromDir).listFiles())).toFile(tofile);
}
}