Scaling a BufferedImage: Delving into Resampling, Not Cropping
Your attempt to scale a BufferedImage using the Graphics2D class may not yield the desired results due to the limitations imposed by the method. The scale() method simply transforms the current graphics context without modifying the underlying image data.
AffineTransformOp: The Preferred Solution for Resampling
For resampling, the AffineTransformOp class provides greater flexibility by allowing you to specify the interpolation type. Interpolation determines how intermediate pixel values are calculated when the image is scaled. Here's an example using AffineTransformOp to scale an image:
BufferedImage before = getBufferedImage(encoded); int w = before.getWidth(); int h = before.getHeight(); BufferedImage after = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); AffineTransform at = new AffineTransform(); at.scale(2.0, 2.0); AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); after = scaleOp.filter(before, after);
In this example:
Key Points
The above is the detailed content of How to Properly Scale a BufferedImage in Java: Resampling vs. Cropping?. For more information, please follow other related articles on the PHP Chinese website!