Java Image Resize getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)

Here you can find the source of getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)

Description

get Resized Image

License

Open Source License

Declaration

public static Image getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio) 

Method Source Code


//package com.java2s;
/*/*from   ww w  . java  2s.com*/
 * #%L
 * Cytoscape Ding View/Presentation Impl (ding-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.awt.Image;

public class Main {
    public static Image getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio) {
        if (original == null)
            throw new IllegalArgumentException("Original image cannot be null.");

        if (w == null && h == null)
            return original;

        final int currentW = original.getWidth(null);
        final int currentH = original.getHeight(null);
        float ratio;
        int converted;

        if (keepAspectRatio == false)
            return original.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING);
        else if (h == null) {
            ratio = ((float) currentH) / ((float) currentW);
            converted = (int) (w * ratio);
            return original.getScaledInstance(w, converted, Image.SCALE_AREA_AVERAGING);
        } else {
            ratio = ((float) currentW) / ((float) currentH);
            converted = (int) (h * ratio);
            return original.getScaledInstance(converted, h, Image.SCALE_AREA_AVERAGING);
        }

    }
}

Related

  1. getResize(Image image, Dimension windowSize)
  2. getResizedImage(Image img, int iNewWidth, int iNewHeight)
  3. getResizedImage(Image originalImage, Dimension newSize)
  4. getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)
  5. getResizedImage(String path, int height, int width)
  6. resize(Image img, int newWidth, float quality)