
Originariamente inviata da
andbin
Nel framework di Java SE c'è principalmente il getScaledInstance di java.awt.Image.
E da quanto rileggo ora ha una logica del tipo: If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions.
Quindi può fare i casi che chiedi.
ciao andbin.
allora, ho provato a guardare quanto mi hai suggerito.
ma ho un problemino:
codice:
public void resizeImage(File inputImage, String dir, int width, int height) throws IOException {
String ext = FilenameUtils.getExtension(inputImage.getName());
ImageIcon ii = new ImageIcon(inputImage.getPath());
Image image = ii.getImage();
Image newImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage buf = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buf.createGraphics();
g2.drawImage(newImage, 0, 0, null);
g2.dispose();
ImageIO.write(buf, ext, new File(dir + inputImage.getName() + "." + ext));
}
ho passato -1 come width.
ho una eccezione sul BufferedImage:
codice:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
mi pare di capire che non accetta parametri negativi.
però a questo punto non riesco bene a capire però.
cercando in giro, ho trovato solo documentazio ed esempi usando BufferedImage per salvare la Image.