import com.ruoyi.common.utils.sign.Base64;
import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageUtils {
/**
* 压缩base64编码至4M以内
*
* @param base64Img
* @return
*/
public static String resizeImageTo4M(String base64Img) {
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64Img);
InputStream stream = new ByteArrayInputStream(bytes1);
BufferedImage src = ImageIO.read(stream);
//BufferedImage output = Thumbnails.of(src).size(src.getWidth() / 3, src.getHeight() / 3).asBufferedImage();
BufferedImage output = Thumbnails.of(src).size(1000, 600).asBufferedImage();
String base64 = imageToBase64(output);
if (base64.length() - base64.length() / 8 * 2 > (4*1024*1024)) {
output = Thumbnails.of(output).scale(1 / (base64.length() / (4*1024*1024))).asBufferedImage();
base64 = imageToBase64(output);
}
return base64;
} catch (Exception e) {
return base64Img;
}
}
/**
* BufferedImage转换成base64,在这里需要设置图片格式,因为我需要jpg格式就设置了jpg
*/
public static String imageToBase64(BufferedImage bufferedImage) {
Base64 encoder = new Base64();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, "jpg", baos);
} catch (IOException e) {
}
return new String(encoder.encode((baos.toByteArray())));
}
/**
* 获取图片base64格式,去掉头,压缩到4M
*/
public static String getBase64(String imageUrl) {
InputStream in = null;
final ByteArrayOutputStream data = new ByteArrayOutputStream();
//读取图片字节数组
try {
URL url = new URL(imageUrl);
final byte[] by = new byte[1024];
// 创建链接获取图片
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
in = conn.getInputStream();
int len = -1;
while ((len = in.read(by)) != -1) {
data.write(by, 0, len);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
//返回Base64编码过的字节数组字符串
String encode = encoder.encode(data.toByteArray());
//判断大小,限制在4M
if (encode.length() > 4 * 1024 * 1024) {
encode = ImageUtils.resizeImageTo4M(encode);
}
/*encode = encode.replaceAll("[\\s*\t\n\r]", "");*/
return encode.substring( encode.indexOf( "," ) + 1 );
}
//获取网络图片
public static void ImageRequest(String ImageName,String picUrl) throws Exception {
//new一个URL对象
URL url = new URL(picUrl);
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
//new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File(ImageName+".jpg");
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
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();
}
}
图片转换、压缩、获取工具类
最新推荐文章于 2024-03-07 14:17:44 发布