Java Graphics How to - Rescale JPG








Question

We would like to know how to rescale JPG.

Answer

/*from  w  ww .jav  a 2 s.  c  o m*/
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
  public static void main(String[] args) throws Exception {
    BufferedImage bufferedImage = ImageIO.read(new File("a.jpg"));
    BufferedImage destinationBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = destinationBufferedImage.createGraphics();
    AffineTransform at = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(bufferedImage, at);
    ImageIO.write(destinationBufferedImage, "JPG", new File("b.jpg"));
  }
}