Java ImageIcon createScaledImageIcon(byte[] albumArtBytes)

Here you can find the source of createScaledImageIcon(byte[] albumArtBytes)

Description

create Scaled Image Icon

License

Open Source License

Declaration

public static ImageIcon createScaledImageIcon(byte[] albumArtBytes) 

Method Source Code


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

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon createScaledImageIcon(byte[] albumArtBytes) {
        if (albumArtBytes == null) {
            return null;
        } //if/*from   www .j  a v  a2  s .c om*/

        Image image = new ImageIcon(albumArtBytes).getImage();

        BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.drawImage(image, 0, 0, null);
        graphics.dispose();

        double scale = determineImageScale(bufferedImage.getWidth(), bufferedImage.getHeight(), 600, 600);

        return new ImageIcon(bufferedImage.getScaledInstance((int) (bufferedImage.getWidth() * scale),
                (int) (bufferedImage.getWidth() * scale), Image.SCALE_SMOOTH));
    }

    private static double determineImageScale(int sourceWidth, int sourceHeight, int targetWidth,
            int targetHeight) {
        double scaleX = (double) targetWidth / sourceWidth;
        double scaleY = (double) targetHeight / sourceHeight;
        return Math.min(scaleX, scaleY);
    }
}

Related

  1. createEmptyImageIcon(final int width, final int height)
  2. createFileImageIcon(final String path)
  3. createMonoColoredImageIcon(final Paint paint, final int width, final int height)
  4. createNonCachedImageIcon(File file)
  5. createOverlayIcon(Icon baseIcon, ImageIcon overlayIcon)
  6. CreateSizedImageIconScaledSmooth(URL filePath, int width, int height)
  7. createTempImage(ImageIcon icon, File oldFile)
  8. displayImage(final ImageIcon ii)
  9. drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)