You can scale a BufferedImage in Java using the AffineTransformOp class from the java.awt.image package. Here's an example of how to scale a BufferedImage:
import javax.imageio.ImageIO; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageScaler { public static void main(String[] args) { try { // Load the original image BufferedImage originalImage = ImageIO.read(new File("original.jpg")); // Specify the scaling factor (e.g., 0.5 for 50% reduction) double scale = 0.5; // Calculate the new dimensions int newWidth = (int) (originalImage.getWidth() * scale); int newHeight = (int) (originalImage.getHeight() * scale); // Create a scaled BufferedImage with the new dimensions BufferedImage scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); // Get the graphics context of the scaled image Graphics2D g2d = scaledImage.createGraphics(); // Set rendering hints for better quality (optional) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Create an AffineTransform for scaling AffineTransform at = AffineTransform.getScaleInstance(scale, scale); // Apply the transformation to the image AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); op.filter(originalImage, scaledImage); // Dispose of the graphics context g2d.dispose(); // Save the scaled image ImageIO.write(scaledImage, "PNG", new File("scaled.png")); System.out.println("Image scaled successfully."); } catch (IOException e) { e.printStackTrace(); } } } In this example:
ImageIO.read().scale value of 0.5 will reduce the image to 50% of its original size.BufferedImage with the specified dimensions.AffineTransform to specify the scaling transformation.AffineTransformOp.ImageIO.write().Make sure to replace "original.jpg" with the path to your own input image and "scaled.png" with the desired output file name and format.
rubygems pgp object big-o svg-sprite pyqtgraph browser-automation avplayer celery nightwatch.js