Java如何实现图片缩放
时间: 2024-05-03 16:20:35 浏览: 83
java实现图片缩放
5星 · 资源好评率100%
Java可以使用Java2D API来实现图片缩放。可以通过缩放因子(scale factor),新的宽度和高度来实现,也可以通过矩阵变换(Matrix transformation)来实现。其中,缩放因子可以使用AffineTransformation类来实现。下面是一个示例代码:
```
BufferedImage originalImage = ImageIO.read(new File("original.png"));
double scaleFactor = 0.5;
int newWidth = (int) (originalImage.getWidth() * scaleFactor);
int newHeight = (int) (originalImage.getHeight() * scaleFactor);
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D g2d = resizedImage.createGraphics();
AffineTransform transform = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
g2d.drawImage(originalImage, transform, null);
g2d.dispose();
ImageIO.write(resizedImage, "png", new File("resized.png"));
```
阅读全文