Java Image Scale getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)

Here you can find the source of getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)

Description

Gets a scaled icon and if it doesn't exist it creates one and scales it

License

Open Source License

Parameter

Parameter Description
icon image to be scaled
iconSize the icon size (Std)
scaledIconSize the new scaled size in pixels

Return

the scaled icon

Declaration

public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight,
        final boolean maintainRatio) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2015, University of Kansas Center for Research
 * /*w w  w  . j a va  2  s  .com*/
 * Specify Software Project, specify@ku.edu, Biodiversity Institute,
 * 1345 Jayhawk Boulevard, Lawrence, Kansas, 66045, USA
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Gets a scaled icon and if it doesn't exist it creates one and scales it
     * @param icon image to be scaled
     * @param iconSize the icon size (Std)
     * @param scaledIconSize the new scaled size in pixels
     * @return the scaled icon
     */
    public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight,
            final boolean maintainRatio) {
        if (icon != null) {
            int dstWidth = newMaxWidth;
            int dstHeight = newMaxHeight;

            int srcWidth = icon.getIconWidth();
            int srcHeight = icon.getIconHeight();

            if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size
                dstWidth = icon.getIconWidth();
                dstHeight = icon.getIconHeight();
            }

            if (maintainRatio) {
                double longSideForSource = Math.max(srcWidth, srcHeight);
                double longSideForDest = Math.max(dstWidth, dstHeight);

                double multiplier = longSideForDest / longSideForSource;
                dstWidth = (int) (srcWidth * multiplier);
                dstHeight = (int) (srcHeight * multiplier);
            }

            Image imgMemory = icon.getImage();

            //make sure all pixels in the image were loaded
            imgMemory = new ImageIcon(imgMemory).getImage();

            BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB);

            Graphics2D graphics2D = thumbImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null);
            graphics2D.dispose();
            return thumbImage;

        }
        return null;
    }
}

Related

  1. getScaledIcon(final Image image, final double scale)
  2. getScaledImage(Image image, int maxWidth, int maxHeight)
  3. getScaledImage(Image imagen, double scaleW, double scaleH)
  4. getScaledImage(Image srcImg, int w, int h)
  5. getScaledImage(ImageIcon icon, int w, int h)