Java Image Resize getResize(Image image, Dimension windowSize)

Here you can find the source of getResize(Image image, Dimension windowSize)

Description

get Resize

License

Open Source License

Parameter

Parameter Description
image the source image
windowSize maximum space to fit the image

Return

{width, height, used scale}

Declaration

public static double[] getResize(Image image, Dimension windowSize) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Dimension;

import java.awt.Image;

public class Main {
    /**//from  w w w. j  a v a 2 s  .  co m
     * @param image the source image
     * @param windowSize maximum space to fit the image
     * @return {width, height, used scale}
     */
    public static double[] getResize(Image image, Dimension windowSize) {
        double w = image.getWidth(null);
        double h = image.getHeight(null);
        double f = 1;
        double fw = w / windowSize.getWidth();
        double fh = h / windowSize.getHeight();
        if (fw > 1 || fh > 1) {
            if (fh > fw) {
                w /= fh;
                h = windowSize.getHeight();
                f = fh;
            } else {
                w = windowSize.getWidth();
                h /= fw;
                f = fw;
            }
        }
        return new double[] { w, h, 1 / f };
    }
}

Related

  1. getResizedImage(Image img, int iNewWidth, int iNewHeight)
  2. getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)
  3. getResizedImage(Image originalImage, Dimension newSize)
  4. getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)