We will see how to re-size image using Java program. Below program will explains you about resizing image using Java code. Method resizeMyImage() will take necessary parameters like re-size image name, original image and resize image sizes (width and height).
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResize {
public static void main(String[] args) {
// resize_image_name, original_image, resize_image_sizes
new ImageResize().resizeMyImage("e:\\rat_resized.jpg", "e:\\rat.jpg", "200", "93");
}
private void resizeMyImage(String resizeImg, String filePath, String w, String h){
if(new File(filePath).exists()){
imageResize(filePath, Integer.parseInt(w) , Integer.parseInt(h), resizeImg);
System.out.println("Resize completed....");
}else{
System.out.println("Original file not exist....");
}
}
public void imageResize(String oriImgUrl, int width, int heigth, String saveLocFilePath){
try {
File f = new File(oriImgUrl);
BufferedImage image = ImageIO.read(f);
BufferedImage resizeImage = resize(image, width, heigth);
ImageIO.write(resizeImage, "jpg", new File(saveLocFilePath));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
}
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageResize {
public static void main(String[] args) {
// resize_image_name, original_image, resize_image_sizes
new ImageResize().resizeMyImage("e:\\rat_resized.jpg", "e:\\rat.jpg", "200", "93");
}
private void resizeMyImage(String resizeImg, String filePath, String w, String h){
if(new File(filePath).exists()){
imageResize(filePath, Integer.parseInt(w) , Integer.parseInt(h), resizeImg);
System.out.println("Resize completed....");
}else{
System.out.println("Original file not exist....");
}
}
public void imageResize(String oriImgUrl, int width, int heigth, String saveLocFilePath){
try {
File f = new File(oriImgUrl);
BufferedImage image = ImageIO.read(f);
BufferedImage resizeImage = resize(image, width, heigth);
ImageIO.write(resizeImage, "jpg", new File(saveLocFilePath));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
}
No comments:
Write comments