java如何等比例缩放图片
时间: 2023-08-31 19:05:46 浏览: 125
Java可以使用Image类和Graphics类来实现等比例缩放图片。以下是一个简单的示例代码:
```java
public static BufferedImage scale(BufferedImage src, int w, int h) {
int width = src.getWidth();
int height = src.getHeight();
if (width == w && height == h) {
return src;
}
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
double sx = (double) w / width;
double sy = (double) h / height;
AffineTransform at = AffineTransform.getScaleInstance(sx, sy);
g2d.drawRenderedImage(src, at);
g2d.dispose();
return bi;
}
```
此方法将返回一个指定宽度和高度的BufferedImage对象,该对象是原始图像的等比例缩放版本。如果原始图像的宽度和高度与指定的宽度和高度相同,则此方法将直接返回原始图像。如果需要,可以将此代码与其他Java图像处理技术一起使用,例如缩放、裁剪、旋转等。
阅读全文
相关推荐
![js](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)