Java Resize Images

package com.bospp;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

public class ImageResize {

    public static void createResizedImage(String imageActualPath, String imagetargetPath, int imgWidth, int imgHeight) {
        try {
            BufferedImage originalImage = ImageIO.read(new File(imageActualPath));
            BufferedImage resizeImagePng = Scalr.resize(originalImage, imgWidth);

            File file = new File(imagetargetPath);
            file.getParentFile().mkdirs();
            String fileName = file.getName();
            String fileExtention = fileName.contains(".")
                    ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : "png";
            ImageIO.write(resizeImagePng, fileExtention, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createDir(String fileDirStr) {
        File fileDir = new File(fileDirStr);
        if (!fileDir.exists() && !fileDir.mkdirs()) {
            System.out.println("Can't create directory. Check permissions");
        }
    }
   
    public static void main(String args[]){
        createResizedImage("/home/rahul/Documents/assets/photo.jpg", "/home/rahul/Documents/assets/yes.jpg", 100, 100);
    }
}

No comments:

Post a Comment