Example code for BAEL-6840: How to scale a BufferedImage in Java? (#16202)

This commit is contained in:
pentakon 2024-03-28 06:41:06 +02:00 committed by GitHub
parent 9e21e95e6f
commit f6a766d8ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.image;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
public class Application {
public static void main(String[] args) throws Exception {
BufferedImage srcImg = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg"));
float scaleW = 2.0f, scaleH = 2.0f;
int w = srcImg.getWidth() * (int) scaleW;
int h = srcImg.getHeight() * (int) scaleH;
BufferedImage dstImg = new BufferedImage(w, h, srcImg.getType());
AffineTransform scalingTransform = new AffineTransform();
scalingTransform.scale(scaleW, scaleH);
AffineTransformOp scaleOp = new AffineTransformOp(scalingTransform, AffineTransformOp.TYPE_BILINEAR);
dstImg = scaleOp.filter(srcImg, dstImg);
ImageIO.write(dstImg, "jpg", new File("src/main/resources/images/resized.jpg"));
}
}