/**
* 将图片压缩到指定大小以内
*
* @param inputStream 源图片数据
* @param maxSize 目的图片大小
* @return 压缩后的图片数据
*/
public static byte[] compress2UnderSize(InputStream inputStream, long maxSize) {
byte[] bytes = null;
try {
bytes = IOUtils.toByteArray(inputStream);
}catch (IOException e){
throw new ServiceException("输入流转字节数组失败!");
}
return compress2UnderSize(bytes,maxSize);
}
/**
* 将图片压缩到指定大小以内
*
* @param inputStream 源图片数据
* @param specLong 长边压缩规格
* @param specShort 短边压缩规格
* @return
*/
public static byte[] compress2UnderSize(InputStream inputStream, int specLong, int specShort) {
byte[] bytes = null;
try {
bytes = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new ServiceException("输入流转字节数组失败!");
}
try {
return compress(bytes, specLong, specShort);
} catch (IOException e) {
throw new ServiceException("压缩图片失败!");
}
}
/**
* 将图片压缩到指定大小以内
*
* @param srcImgData 源图片数据
* @param maxSize 目的图片大小
* @return 压缩后的图片数据
*/
public static byte[] compress2UnderSize(byte[] srcImgData, long maxSize) {
double scale = 0.9;
byte[] imgData = Arrays.copyOf(srcImgData, srcImgData.length);
int count=0;
if (imgData.length > maxSize) {
do {
try {
imgData = compress(imgData, scale);
count++;
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("压缩图片过程中出错!");
}
} while (imgData.length > maxSize && count < 20);
}
return imgData;
}
/**
* 按照 宽高 比例压缩
*
* @param scale 压缩刻度
* @return 压缩后图片数据
* @throws IOException 压缩图片过程中出错
*/
public static byte[] compress(byte[] srcImgData, double scale) throws IOException {
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
int width = (int) (bi.getWidth() * scale); // 源图宽度
int height = (int) (bi.getHeight() * scale); // 源图高度
Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.setColor(Color.RED);
g.drawImage(image, 0, 0, null); // 绘制处理后的图
g.dispose();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ImageIO.write(tag, "JPEG", bOut);
return bOut.toByteArray();
}
/**
*
* @param srcImgData 源图片数据
* @param specLong 长边压缩规格
* @param specShort 短边压缩规格
* @return
* @throws IOException
*/
public static byte[] compress(byte[] srcImgData, int specLong, int specShort) throws IOException {
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(srcImgData));
int width = bi.getWidth(); // 源图宽度
int height = bi.getHeight(); // 源图高度
int targetWidth;
int targetHeight;
if(bi.getWidth()>=bi.getHeight()){
targetWidth=specLong;
targetHeight=specShort;
}else {
targetWidth=specShort;
targetHeight=specLong;
}
float widthScale = targetWidth / (float) width;// 源图宽度
float heightScale = targetHeight / (float) height;// 源图高度
float targetScale = Math.min(widthScale, heightScale);//最终缩小比例
targetWidth = (int) ((float) width * targetScale);
targetHeight = (int) ((float) height * targetScale);
Image image = bi.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.setColor(Color.RED);
g.drawImage(image, 0, 0, null); // 绘制处理后的图
g.dispose();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ImageIO.write(tag, "JPEG", bOut);
return bOut.toByteArray();
}
压缩图片小工具
于 2024-03-25 21:52:55 首次发布