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.net.URL;
/***
*通图片URL进行设置图片宽高
* @param imgUrl 图片URL
* @param newWidth 新的宽度
* @param newHeight 新的高度
* @param path 图片存放路径
* @return
*/
public static String image(String imgUrl,int newWidth,int newHeight,String path){
BufferedOutputStream outputStream = null;
String FileName = RandomUtils.random17()+".jpg";
try{
URL url = new URL(imgUrl);
// 读取图片
Image image = ImageIO.read(url);
// 创建图片流
BufferedImage bufferedImage = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);
// 绘制图片大小
bufferedImage.getGraphics().drawImage(image,0,0,newWidth,newHeight,null);
// 进行图片输出
outputStream = new BufferedOutputStream(new FileOutputStream(path+FileName));
ImageIO.write(bufferedImage,"JPEG",outputStream);
}catch (Exception e){
LOGGER.error(e.getMessage());
}finally {
try{
if(outputStream!=null){
outputStream.flush();
outputStream.flush();
}
}catch (IOException e){
LOGGER.error(e.getMessage());
}
}
return path+FileName;
}
网友评论