图片压缩工具
package cn.swiftpass.spay.salesman.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
/*
* 图片压缩工具
* */
public class ImgsUtils {
protected static Logger logger = LoggerFactory.getLogger(ImgsUtils.class);
public static byte[] compressUnderSize(byte[] srcImgData) throws IOException {
return compressUnderSize(srcImgData, 1048576);
}
public static byte[] compressUnderSize(byte[] srcImgData, long maxSize) throws IOException {
double scale = 0.9;
byte[] imgData = Arrays.copyOf(srcImgData, srcImgData.length);
logger.info("imgData.length {}", imgData.length);
if (imgData.length > maxSize) {
do {
try {
imgData = compress(imgData, scale);
} catch (IOException e) {
throw new IllegalStateException("压缩图片过程中出错,请及时联系管理员!", e);
}
} while (imgData.length > maxSize);
}
System.out.println(imgData.length);
return imgData;
}
/**
* 按照 宽高 比例压缩
*
* @param srcImgData 待压缩图片输入流
* @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 size 源图片大小
* @return 图片压缩质量比
*/
private static double getAccuracy(long size) {
double accuracy;
if (size < 900) {
accuracy = 0.85;
} else if (size < 2047) {
accuracy = 0.6;
} else if (size < 3275) {
accuracy = 0.44;
} else {
accuracy = 0.4;
}
return accuracy;
}
private static byte[] readInputStream(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
/**
* 下载网络图并压缩
* @param urlStr 网络图地址
* @param newFile 新文件地址
* @throws IOException
*/
public static void downUrlImgAndCompressMax512(String urlStr,String newFile) throws IOException{
//new一个URL对象
URL url = null;
url = new URL(urlStr);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
byte[] bytes = compressUnderSize(data);
//创建输出流
FileOutputStream outStream = new FileOutputStream(newFile);
//写入数据
outStream.write(bytes);
//关闭输出流
outStream.close();
}
/**
* 得到图片的二进制数据,以二进制封装得到数据,具有通用性
* @param inStream
* @return
* @throws Exception
*/
private static byte[] readInputStream(InputStream inStream) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
//使用一个输入流从buffer里把数据读取出来
while( (len=inStream.read(buffer)) != -1 ){
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
}
日夜颠倒头发少 ,单纯好骗恋爱脑 ,会背九九乘法表 ,下雨只会往家跑 ,搭讪只会说你好 ---- 2050781802@qq.com