- 方法代码
package org.manage.util; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.Base64; import java.util.HashMap; import java.util.Map; public class QRCodeGeneratorUtils { private static void resizeImage(String inputImagePath, String outputImagePath, int newWidth) { try { File inputFile = new File(inputImagePath); BufferedImage inputImage = ImageIO.read(inputFile); // 计算等比例缩放后的高度 int originalWidth = inputImage.getWidth(); int originalHeight = inputImage.getHeight(); int newHeight = (int) Math.round((double) newWidth / originalWidth * originalHeight); // 创建一个新的BufferedImage,并进行等比例缩放 BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, inputImage.getType()); Graphics2D g2d = resizedImage.createGraphics(); g2d.drawImage(inputImage, 0, 0, newWidth, newHeight, null); g2d.dispose(); // 保存缩放后的图片 ImageIO.write(resizedImage, "png", new File(outputImagePath)); } catch (IOException e) { e.printStackTrace(); } } }
- 测试
public static void main(String[] args) { String inputImagePath = "E:\\images\\appLogo.png"; String outputImagePath = "E:\\images\\output_resized.png"; int newWidth = 50; // 替换为你想要的新宽度 resizeImage(inputImagePath, outputImagePath, newWidth); }
Java等比例缩小图片尺寸
最新推荐文章于 2024-07-13 03:09:01 发布