Java Image Scale getScaledImage(ImageIcon srcImg, int h, int maxWidth)

Here you can find the source of getScaledImage(ImageIcon srcImg, int h, int maxWidth)

Description

Method used to resize images to fit the cover into the label

License

Open Source License

Parameter

Parameter Description
srcImg source image icon
h height of the image
maxWidth a parameter

Return

resized image icon

Declaration

public static ImageIcon getScaledImage(ImageIcon srcImg, int h, int maxWidth) 

Method Source Code

//package com.java2s;
/*//from  w w w .jav a2 s. com
 * PS3 Media Server, for streaming any medias to your PS3.
 * Copyright (C) 2012  Ph.Waeber
 *
 * 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; version 2
 * of the License only.
 *
 * 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.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Method used to resize images to fit the cover into the label
     * @param srcImg source image icon
     * @param h height of the image
     * @param maxWidth 
     * @return resized image icon
     */
    public static ImageIcon getScaledImage(ImageIcon srcImg, int h, int maxWidth) {
        int w = srcImg.getIconWidth() * h / srcImg.getIconHeight();

        //respect the max width constraint
        if (w > maxWidth) {
            w = maxWidth;
            h = srcImg.getIconHeight() * w / srcImg.getIconWidth();
        }

        //don't make the image bigger then its original size
        if (w > srcImg.getIconWidth() || h > srcImg.getIconHeight()) {
            w = srcImg.getIconWidth();
            h = srcImg.getIconHeight();
        }

        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg.getImage(), 0, 0, w, h, null);
        g2.dispose();
        return new ImageIcon(resizedImg);
    }
}

Related

  1. getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)
  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)
  6. getScaleToFit(JPanel componentToFit, File imageFile)
  7. scaleImage(Image image, float scale)
  8. scaleImage(Image image, float widthPercentage, float heightPercentage)
  9. scaleImage(Image sourceImage, int width, int height)