Un paio di mesi fa mi occupai proprio della ridimensione dell'immagine.
Ti posto il metodo:
Codice PHP:
BufferedImage img;
/**
* Crea l'anteprima di un' immagine.
*
* @param ImgIn
* Percorso dell'immagine originale da ridimensionare
* @param ImgOut
* Percorso dell'immagine che e' stata ridimensionata
* @param targetWidth
* La larghezza, in pixel, con cui scalare l'immagine.
* [B]NB:[/B] Non e' necessario impostare anche l'altezza, in quanto quest'ultima
* verra' impostata in automatico in modo da rispettare le proporzioni.
* @param hint
* uno dei [I]rendering hints[/I] che corrispondono a {@code
* RenderingHints.KEY_INTERPOLATION} (es.: {@code
* RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR}, {@code
* RenderingHints.VALUE_INTERPOLATION_BILINEAR}, {@code
* RenderingHints.VALUE_INTERPOLATION_BICUBIC})
* @param higherQuality
* Se impostata a [I]true[/I], questo metodo usera' la tecnica del ridimensionamento
* multi-step che fornisce una qualita' migliore rispetto alla
* tecnica del one-step (risulta utile solo nel caso in cui si vuole rimpicciolire
* l'immagine, in cui {@code targetWidth} e' minore rispetto alle dimensioni
* originali, ed in generale solo quando e' specificato l'hint {@code BILINEAR}
*/
public void createThumbnail(String ImgIn, String ImgOut, int targetWidth,
Object hint, boolean higherQuality) {
// Imposto la codifica di output in base all'estensione dell'immagine
String encodeImage = null;
if(ImgIn.endsWith(".jpg") || ImgIn.endsWith(".jpeg")){
encodeImage = "jpg";
}else if (ImgIn.endsWith(".png")) {
encodeImage = "png";
}else{
System.out.println("ATTENZIONE: Errore nel formato dell'immagine passato!");
}
try {
img = ImageIO.read(new File(ImgIn));
} catch (IOException e) {
e.printStackTrace();
}
int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
: BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage) img;
int w, h;
int targetHeight = (img.getHeight() * targetWidth) / img.getWidth();
if (higherQuality) {
// Usa la tecnica del multi-step: parte con le dimensioni reali, per
// poi scalare a poco a poco finche' non si raggiunge
// la dimensione desiderata
w = img.getWidth();
h = img.getHeight();
} else {
// Usa la tecnica del one-step: scala direttamente dalle dimensioni dell'originale
// a quella desiderata con una singola chiamata al metodo single drawImage()
w = targetWidth;
h = targetHeight;
}
do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
ret = tmp;
} while (w != targetWidth || h != targetHeight);
File f = new File(ImgOut);
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
ImageIO.write(ret, encodeImage, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}