Skip to content

Instantly share code, notes, and snippets.

@madan712
Created August 22, 2012 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save madan712/3422710 to your computer and use it in GitHub Desktop.
Save madan712/3422710 to your computer and use it in GitHub Desktop.
This is a simple Java program which will resize an image to any other desire size.
/* ImageResizer.java */
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResizer {
public static void resizer(File orignalImage,int width,int height,String extension) {
try {
BufferedImage origBuffImg = ImageIO.read(orignalImage);
int type = origBuffImg.getType() == 0? BufferedImage.TYPE_INT_ARGB : origBuffImg.getType();
BufferedImage resizedBuffImg = new BufferedImage(width, height, type);
Graphics2D g = resizedBuffImg.createGraphics();
g.drawImage(origBuffImg, 0, 0, width, height, null);
g.dispose();
String newFile = orignalImage.getAbsolutePath().substring(0,orignalImage.getAbsolutePath().lastIndexOf("."))+"_"+width+"x"+height+"."+extension;
ImageIO.write(resizedBuffImg, extension, new File(newFile));
System.out.println("File created : "+newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File orignalImage = new File("C:/Test/pool.jpg");
resizer(orignalImage,150,150,"GIF");
resizer(orignalImage,100,100,"JPEG");
resizer(orignalImage,50,50,"png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment