1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;
public class ResizingImage {
static BufferedImage imgNew = null;
public static BufferedImage resize(int newWidth, int newHeight, String imgPath) {
try {
File imgFile = new File(imgPath);
BufferedImage imgOld = ImageIO.read(imgFile);
imgNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
/// Resizing of the old image ////////////////////////
Graphics2D g = (Graphics2D) imgNew.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g.drawImage(imgOld, 0, 0, newWidth, newHeight, null);
g.dispose();
/////////////////////////////////////////////////////
} catch (IOException e) {
e.printStackTrace();
}
return imgNew;
}
public static BufferedImage resize(int newWidth, int newHeight, String imgPath, String strg) {
imgNew = resize(newWidth, newHeight, imgPath);
try {
ImageIO.write(imgNew, "jpeg", new File(strg));
} catch (IOException e) {
e.printStackTrace();
}
return imgNew;
}
public static void main (String[] args) {
ResizingImage.resize(320, 240, "D:/Esia/logo.jpg", "D:/test.jpg");
}
} |
Partager