不依赖第三方的 图片缩放工具类

private static int height;

private static BufferedImage img;

/**

  • 强制压缩、放大

  • @param destFile

  • @param w

  • @param h

  • @throws IOException

*/

private static void resize(String destFile, int w, int h) throws IOException {

BufferedImage bufferedImage = new BufferedImage(w, h 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 , BufferedImage.TYPE_INT_RGB);

bufferedImage.getGraphics().fillRect(0, 0, w, h);

bufferedImage.getGraphics().drawImage(img, 0, 0, w, h, null);//将原图片画到新图片上

//输出新图片

FileOutputStream out = new FileOutputStream(destFile);

ImageIO.write(bufferedImage, destFile.substring(destFile.lastIndexOf(‘.’) + 1), out);

out.close();

}

/**

  • 按照固定的比例缩放图片

  • @param sourceFile 源图片

  • @param destFile 目标图片

  • @param scale 缩放比例

  • @throws IOException

*/

public static void resize(String sourceFile, String destFile, double scale) throws IOException {

img = ImageIO.read(new File(sourceFile));

width = img.getWidth();

height = img.getHeight();

int w = (int) (width * scale);

int h = (int) (height * scale);

resize(destFile, w, h);

}

/**

  • 以高度为基准,等比例缩放图片

  • @param sourceFile 源图片

  • @param destFile 目标图片

  • @param h 作为基准的高度

  • @throws IOException

*/

public static void resizeByHeight(String sourceFile, String destFile, int h) throws IOException {

img = ImageIO.read(new File(sourceFile));

width = img.getWidth();

height = img.getHeight();

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值