vue项目接口域名动态的获取方法
364
2023-04-24
java对图片进行压缩和resize缩放的方法
序
这里展示一下如何对图片进行压缩和resize。分享给大家,具体如下:
压缩
public static boolean compress(String src,String to, float quality) {
boolean rs = true;
// Build param
JPEGEncodeParam param = null;
// Build encoder
File destination = new File(to);
FileOutputStream os = null;
try {
BufferedImage image = ImageIO.read(new File(src));
param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, false);
os = FileUtils.openOutputStream(destination);
JPEGImageEncoder encoder;
if (param != null) {
encoder = JPEGCodec.createJPEGEncoder(os, param);
} else {
return false;
}
encoder.encode(image);
} catch(Exception e){
e.printStackTrace();
rs = false;
}finally {
IOUtils.closeQuietly(os);
}
return rs;
}
resize
WrOckF
public static boolean resize(String src,String to,int newWidth,int newHeight) {
try {
File srcFile = new File(src);
File toFile = new File(to);
BufferedImage img = ImageIO.read(srcFile);
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(newWidth, newHeight, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newWidth, newHeight, 0, 0, w, h, null);
g.dispose();
ImageIO.write(dimg, "jpg", toFile);
} catch (Exception e) {
e.http://printStackTrace();
return false;
}
return true;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~