code import org.springframework.util.Assert;import javax.imageio.ImageIO;import java.awt.*;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.*;/** * 图片格式转
import org.springframework.util.Assert;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* 图片格式转换 压缩 工具类
*
*/
public final class ImageUtil {
private ImageUtil(){
throw new AssertionError("403 forbidden");
}
/**
* 将图片{@link BufferedImage}按指定像素压缩
* @param image 源图片文件
* @param maxPixel 转换的像素(如:maxPixel=720)
* @param format:压缩后想要转换的图片类型
* @param
*/
public static byte[] writeBytes(BufferedImage image, int maxPixel, String format) {
Assert.notNull(image, "image can not be null or empty.");
Assert.isTrue(maxPixel > 0, "maxPixel > 0 required.");
// 生成图片转化对象
AffineTransform transform = new AffineTransform();
int imageWidth = image.getWidth();//原图片的高度
int imageHeight = image.getHeight();//原图片的宽度
int changeWidth = 0;//压缩后图片的高度
int changeHeight = 0;//压缩后图片的宽度
double scale = 0;// 定义小图片和原图片比例
if (maxPixel != 0) {
if (imageWidth > imageHeight) {
changeWidth = maxPixel;
scale = (double) changeWidth / (double) imageWidth;
changeHeight = (int) (imageHeight * scale);
} else {
changeHeight = maxPixel;
scale = (double) changeHeight / (double) imageHeight;
changeWidth = (int) (imageWidth * scale);
}
}
// 生成转换比例
transform.setToScale(scale, scale);
// 生成转换操作对象
AffineTransformOp transOp = new AffineTransformOp(transform, null);
//生成压缩图片缓冲对象
BufferedImage newImage = new BufferedImage(changeWidth, changeHeight,
BufferedImage.TYPE_3BYTE_BGR);
//生成缩小图片
transOp.filter(image, newImage);
return writeBytes(newImage, format);
}
/**
*
* 将{@link BufferedImage}生成formatName指定格式的图像数据
* @param source
* @param formatName 图像格式名,图像格式名错误则抛出异常
* @return
*/
public static byte[] writeBytes(BufferedImage source,String formatName){
Assert.notNull(source, "source can not be null or empty.");
Assert.notNull(formatName, "formatName can not be null or empty.");
ByteArrayOutputStream output = new ByteArrayOutputStream();
Graphics2D g = null;
try {
for(BufferedImage image = source; !ImageIO.write(image, formatName, output);){
if(null!=g)
throw new IllegalArgumentException(String.format("not found writer for '%s'",formatName));
image = new BufferedImage(source.getWidth(),
source.getHeight(), BufferedImage.TYPE_INT_RGB);
g = image.createGraphics();
g.drawImage(source, 0, 0,null);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (null != g)
g.dispose();
}
return output.toByteArray();
}
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("E:\\document\\001.jpg"));
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:\\document\\002.jpg"));
fileOutputStream.write(writeBytes(image, 400, "JPEG"));
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
