[Source] Création de thumbnails à partir de BufferedImage
Bonjour,
Voici 2 méthodes pour créer des Thumbnails à partir d'un BufferedImage.
On utilise un SCALE_SMOOTH pour avoir une image réduite "potable" niveau visualisation.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static BufferedImage reduceImage(BufferedImage input, int newwidth,
int newheight) {
BufferedImage out = null;
//on choisie le type de couleur suivant le nombre de composants
if(input.getColorModel().getNumColorComponents() == 1)
out = new BufferedImage(newwidth, newheight, BufferedImage.TYPE_BYTE_GRAY);
else
out = new BufferedImage(newwidth, newheight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = out.createGraphics();
System.err.println("Temp color model : " + input.getColorModel());
Image scaled = input.getScaledInstance(newwidth, newheight, Image.SCALE_SMOOTH); //on utilise un SCALE_SMOOTH pour obtenir une petite image correcte
g.drawImage(scaled, 0, 0, out.getWidth(), out.getHeight(), null);
g.dispose();
return out;
}
public static BufferedImage reduceImage(BufferedImage input, float factor) {
int newwidth = (int) (((double) input.getWidth()) * factor);
int newheight = (int) (((double) input.getHeight()) * factor);
return reduceImage(input, newwidth, newheight);
} |