Java BufferedImage Resize getResizedImage(BufferedImage image)

Here you can find the source of getResizedImage(BufferedImage image)

Description

get Resized Image

License

Open Source License

Declaration

public static BufferedImage getResizedImage(BufferedImage image) 

Method Source Code


//package com.java2s;
//License from project: GNU General Public License 

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {
    public static BufferedImage getResizedImage(BufferedImage image) {
        if (image == null) {
            return null;
        }/* www. j  a  va 2 s .co  m*/

        if (image.getWidth() != image.getHeight()) {
            BufferedImage nImage = new BufferedImage(Math.max(image.getWidth(), image.getHeight()),
                    Math.max(image.getWidth(), image.getHeight()), 2);
            nImage.getGraphics().drawImage(image, nImage.getWidth() / 2 - image.getWidth() / 2,
                    nImage.getHeight() / 2 - image.getHeight() / 2, null);
            image = nImage;
        }

        BufferedImage img = new BufferedImage(128, 128, 2);
        img.getGraphics().drawImage(image.getScaledInstance(128, 128, 1), 0, 0, null);

        return img;
    }

    public static BufferedImage getResizedImage(File file) {
        return getResizedImage(getImage(file));
    }

    public static BufferedImage getImage(File file) {
        if (file == null || !file.exists()) {
            return null;
        }

        BufferedImage img = null;

        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return img;
    }
}

Related

  1. downsizeImage(Image imgSource, int w, int h)
  2. fitImage(JLabel label, BufferedImage image)
  3. getResizeFactor(BufferedImage source, int maxWidth, int maxHeight)
  4. imageResize(BufferedImage image, int width, int height)
  5. linearResizeImage(BufferedImage origin, int width, int height)
  6. resizeToContainer(BufferedImage imagen, int wCont, int hCont)