Java Image Scale scaleImage(Image image, float scale)

Here you can find the source of scaleImage(Image image, float scale)

Description

scale Image

License

Open Source License

Declaration

public static Image scaleImage(Image image, float scale) 

Method Source Code


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

import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;

public class Main {
    public static Image scaleImage(Image image, float scale) {
        if (image == null)
            return null;

        BufferedImage sourceImage = toBufferedImage(image);
        int scaledWidth = (int) (sourceImage.getWidth() * scale);
        if (scaledWidth <= 0)
            return null;
        int scaledHeight = (int) (sourceImage.getHeight() * scale);
        if (scaledHeight <= 0)
            return null;

        BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, sourceImage.getType());
        AffineTransform at = new AffineTransform();
        at.scale(scale, scale);//from  w w w . j  a  v a 2s.c  o m
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        scaledImage = scaleOp.filter(sourceImage, scaledImage);

        return scaledImage;
    }

    public static BufferedImage toBufferedImage(Image image) {
        if (image == null)
            return null;

        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        if (image.getWidth(null) < 0 || image.getHeight(null) < 0) {
            // This code ensures that all the pixels in the image are loaded
            image = new ImageIcon(image).getImage();
        }

        int imgType = hasAlpha(image) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
        BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getHeight(null), imgType);
        Graphics g = buffImg.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return buffImg;
    }

    public static boolean hasAlpha(Image image) {
        if (image == null)
            return false;

        if (image instanceof BufferedImage) {
            BufferedImage buffImg = (BufferedImage) image;
            return buffImg.getColorModel().hasAlpha();
        }

        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
            return pg.getColorModel().hasAlpha();
        } catch (InterruptedException e) {

        }

        return true;
    }
}

Related

  1. getScaledImage(Image imagen, double scaleW, double scaleH)
  2. getScaledImage(Image srcImg, int w, int h)
  3. getScaledImage(ImageIcon icon, int w, int h)
  4. getScaledImage(ImageIcon srcImg, int h, int maxWidth)
  5. getScaleToFit(JPanel componentToFit, File imageFile)
  6. scaleImage(Image image, float widthPercentage, float heightPercentage)
  7. scaleImage(Image sourceImage, int width, int height)
  8. scaleImage(Image src, int width, int height)
  9. ScaleToSize(Image srcImg, int w, int h)