package com.example.springbootsimple.util;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
@Slf4j
public class ImageCompressUtil {
public static byte[] compressImage(byte[] data)
{
return compressImage(data,600,400,0.85f);
}
public static byte[] compressImage(byte[] data,int width,int height)
{
return compressImage(data,width,height,0.85f);
}
public static byte[] compressImage(byte[] data,int width,int height,float quality)
{
width = width>600?600:width;
height = height>400?400:height;
quality = quality<0.8f?0.8f:quality;
int maxLength = 32*1024;
if(data==null || data.length<maxLength )
{
return data;
}
try {
InputStream inputStream = new ByteArrayInputStream(data);
Image oriImg = ImageIO.read(inputStream);
int ori_width = oriImg.getWidth(null);
int ori_height = oriImg.getHeight(null);
if(width>ori_width || height>ori_height)
{
return data;
}
float ori_rate = (float)ori_width / (float)ori_height;
float rate = (float)width / (float)height;
if (ori_rate > rate) {
return compressByWidth(ori_width, ori_height, width, oriImg, quality);
} else {
return compressByHeight(ori_width, ori_height, height, oriImg, quality);
}
} catch (IOException e) {
log.error("ImageCompressUtil compressImage IOException failed,",e);
}
log.warn("ImageCompressUtil compressImage failed,now return the origin data.");
return data;
}
private static byte[] compressByWidth(int ori_width,int ori_height,int w,Image oriImg,float quality) throws IOException {
int h = (int) (ori_height * w / ori_width);
return compress(w, h, oriImg, quality);
}
private static byte[] compressByHeight(int ori_width,int ori_height,int h,Image oriImg,float quality) throws IOException {
int w = (int) (ori_width * h / ori_height);
return compress(w, h, oriImg, quality);
}
private static byte[] compress(int w, int h,Image oriImg,float quality) throws IOException {
BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
image.getGraphics().drawImage(oriImg.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0,0,null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image);
jep.setQuality(quality, true);
encoder.encode(image, jep);
return out.toByteArray();
}
public byte[] image2Bytes(String imgSrc)
{
FileInputStream fin;
byte[] bytes = null;
try {
fin = new FileInputStream(new File(imgSrc));
bytes = new byte[fin.available()];
fin.read(bytes);
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
}
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
@RequestMapping("/ins_task")
public String insTask(HttpServletRequest request, String content, int uid, String title, int type, int taskcount, @RequestParam(value = "file",required = false) MultipartFile file,
@RequestParam(value = "describe",required = false,defaultValue = "") String describe){
String img_path = "";
try {
if(type == 0){
byte[] bytes = file.getBytes();
byte[] bytes1 = ImageCompressUtil.compressImage(bytes, 300, 197);
InputStream inputStream = new ByteArrayInputStream(bytes1);
MultipartFile file1 = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
String path = PathUtil.getSavePath();
File parent = new File(path);
if (!parent.exists()) parent.mkdirs();
String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + ext;
try {
file1.transferTo(new File(parent, newFileName));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + path + "/" + newFileName);
img_path = PathUtil.GetDemain()+PathUtil.getRequestPath()+newFileName;
}