multi Scale BufferedImage To Width - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Scale

Description

multi Scale BufferedImage To Width

Demo Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.Image;
import java.awt.RenderingHints;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;

public class Main {
    public static BufferedImage multiScaleToWidth(BufferedImage img,
            int width) {
        return multiScale(img, (double) width / img.getWidth());
    }//w ww  .  j  av a  2  s  . c om

    /**
     * Convenience method for getScaledInstance with typical default; see below
     */
    public static BufferedImage multiScale(BufferedImage img, double scale) {
        int w = (int) (img.getWidth() * scale);
        int h = (int) (img.getHeight() * scale);
        if (scale < 1) {
            return getScaledInstance(img, w, h,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
        } else if (scale == 1) {
            return img;
        } else {
            return rescaleXMaintainAspectRatio(img, w);
        }
    }

    /**
     * This method uses multi-step scaling techniques for down scaling for better image quality.
     * see http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
     * see also SwingLabs GraphicsUtilities
     * <p/>
     * Convenience method that returns a scaled instance of the provided {@code BufferedImage}.
     * <p/>
     * IMPORTANT: This method does not scale up - consider other methods in this class if you
     * need upscaling.
     *
     * @param img                    the original image to be scaled
     * @param targetWidth            the desired width of the scaled instance,
     *                               in pixels
     * @param targetHeight           the desired height of the scaled instance,
     *                               in pixels
     * @param interpolationHintValue one of the rendering hints that corresponds to
     *                               {@code RenderingHints.KEY_INTERPOLATION} (e.g.
     *                               {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
     *                               {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
     *                               {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
     * @param higherQuality          if true, this method will use a multi-step
     *                               scaling technique that provides higher quality than the usual
     *                               one-step technique (only useful in down scaling cases, where
     *                               {@code targetWidth} or {@code targetHeight} is
     *                               smaller than the original dimensions, and generally only when
     *                               the {@code BILINEAR} hint is specified)
     * @return a scaled version of the original {@code BufferedImage}
     */
    public static BufferedImage getScaledInstance(BufferedImage img,
            int targetWidth, int targetHeight,
            Object interpolationHintValue, boolean higherQuality) {
        int type = hasAlpha(img) ? BufferedImage.TYPE_INT_ARGB
                : BufferedImage.TYPE_INT_RGB;
        BufferedImage ret = img;
        int w, h;
        if (higherQuality) {
            // Use multi-step technique: start with original size, then
            // scale down in multiple passes with drawImage()
            // until the target size is reached
            w = img.getWidth();
            h = img.getHeight();
        } else {
            // Use one-step technique: scale directly from original
            // size to target size with a single drawImage() call
            w = targetWidth;
            h = targetHeight;
        }

        do {
            if (higherQuality && w > targetWidth) {
                w /= 2;
                if (w < targetWidth) {
                    w = targetWidth;
                }
            }

            if (higherQuality && h > targetHeight) {
                h /= 2;
                if (h < targetHeight) {
                    h = targetHeight;
                }
            }

            BufferedImage tmp = new BufferedImage(w, h, type);
            Graphics2D g2 = tmp.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    interpolationHintValue);
            g2.drawImage(ret, 0, 0, w, h, null);
            g2.dispose();

            ret = tmp;
            //            System.out.println( "w = " + w +", h="+h);
        } while (w > targetWidth || h > targetHeight);

        return ret;
    }

    /**
     * @param im
     * @param width
     * @return the resized image
     */
    public static BufferedImage rescaleXMaintainAspectRatio(
            BufferedImage im, int width) {
        if (im.getWidth() == width) {
            return im;
        }
        double inx = im.getWidth();
        double dx = width / inx;
        return rescaleFractional(im, dx, dx);
    }

    public static boolean hasAlpha(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bimage = (BufferedImage) image;
            return bimage.getColorModel().hasAlpha();
        }

        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's color model
        ColorModel cm = pg.getColorModel();
        return cm.hasAlpha();
    }

    /**
     * Rescale an image based on scale factors for width and height.
     *
     * @param in - original buffered image.
     * @param dx - multiplier for the width (x direction)
     * @param dy - multiplier for the height (y direction)
     * @return - scaled buffered image
     */
    public static BufferedImage rescaleFractional(BufferedImage in,
            double dx, double dy) {
        int width = (int) (in.getWidth() * dx);
        int height = (int) (in.getHeight() * dy);
        BufferedImage newImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2 = newImage.createGraphics();
        AffineTransform at = AffineTransform.getScaleInstance(dx, dy);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g2.drawRenderedImage(in, at);
        return newImage;
    }
}

Related Tutorials