-
-
Save kharmaodo/26eef9b92059bc8aa06488c23adac3f4 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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