import com.alibaba.fastjson.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.*;
import java.math.BigDecimal;
import java.util.UUID;
@Slf4j
public class ImageThumbnailUtils {
final private static String rootPath;
final private static String folder = "tmp";
/**
* 默认缩放宽
*/
final public static Integer compression_width = 258;
/**
* 默认缩放高
*/
final public static Integer compression_height = 258;
static {
rootPath = System.getProperty("user.dir");
System.out.println("rootPath:" + rootPath);
}
public static File compression(MultipartFile originFile) throws Exception{
String type = getImageType(originFile);
if (type == null) {
return null;
}
InputStream in = null;
File tmpFile = null;
try {
in = originFile.getInputStream();
StringBuilder filePath = new StringBuilder(rootPath);
filePath.append(File.separator).append(folder).append(File.separator).append(UUID.randomUUID()).append(".").append(type);
tmpFile = FileUtil.createFile(filePath.toString());
reSize(originFile, tmpFile, compression_width, compression_height, true, type);
}catch (Exception e){
log.error("压缩图片报错", e);
throw e;
}
return tmpFile;
}
/**
* @param srcImg 原图片
* @param destImg 目标位置
* @param width 期望宽
* @param height 期望高
* @param equalScale 是否等比例缩放
*/
private static void reSize(MultipartFile srcImg, File destImg, int width,
int height, boolean equalScale, String type) {
if (width < 0 || height < 0) {
return;
}
BufferedImage srcImage = null;
try {
srcImage = ImageIO.read(srcImg.getInputStream());
log.info("srcImg width={}, height={}", srcImage.getWidth() ,srcImage.getHeight());
} catch (IOException e) {
e.printStackTrace();
return;
}
if (srcImage != null) {
// targetW,targetH分别表示目标长和宽
BufferedImage target = null;
double sx = (double) width / srcImage.getWidth();
double sy = (double) height / srcImage.getHeight();
// 等比缩放
if (equalScale) {
if (sx > sy) {
sx = sy;
width = (int) (sx * srcImage.getWidth());
} else {
sy = sx;
height = (int) (sy * srcImage.getHeight());
}
}
log.info("destImg width={},height={}", width, height);
ColorModel cm = srcImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
Graphics2D g = target.createGraphics();
// smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
// 将转换后的图片保存
ByteArrayOutputStream baos = null;
FileOutputStream fos = null;
try {
baos = new ByteArrayOutputStream();
ImageIO.write(target, type, baos);
fos = new FileOutputStream(destImg);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtils.close(baos);
IOUtils.close(fos);
}
}
}
/**
* 获取文件后缀不带.
*
* @param file 文件后缀名
* @return
*/
private static String getImageType(MultipartFile file) {
if (file != null) {
String fileName = file.getOriginalFilename();
int index = fileName.lastIndexOf(".");
if (index != -1 && index < fileName.length() - 1) {
return fileName.substring(index + 1);
}
}
return null;
}
}
java 压缩图片
最新推荐文章于 2023-08-29 17:39:09 发布