Java BufferedImage Transform transformImage2(BufferedImage image, AffineTransform transform)

Here you can find the source of transformImage2(BufferedImage image, AffineTransform transform)

Description

transform Image

License

Open Source License

Declaration

public static BufferedImage transformImage2(BufferedImage image,
            AffineTransform transform) 

Method Source Code

//package com.java2s;

import java.awt.Graphics2D;

import java.awt.Transparency;
import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage transformImage2(BufferedImage image,
            AffineTransform transform) {

        /*//from ww  w. j ava 2s .c o m
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);

        //BufferedImage destinationImage = op.createCompatibleDestImage(image, (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? null : image.getColorModel());
        BufferedImage destinationImage = op.createCompatibleDestImage(image, (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? null : image.getColorModel());

        Graphics2D g = destinationImage.createGraphics();

        g.setBackground(Color.WHITE);
        //Color transparent = new Color(0,0,0,0);
        //g.setBackground(transparent);

        g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
         */

        /*
        BufferedImage destinationImage = scaleCanvas(image, image.getWidth(), image.getHeight());

        destinationImage = op.filter(image, destinationImage);

        return destinationImage;
         */

        /*
        int type = (image.getTransparency() == Transparency.OPAQUE) ?
            BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
                
        BufferedImage after = new BufferedImage(image.getWidth(), image.getHeight(), type);
         */

        /*
        BufferedImage after = op.filter(image, null);
        return after;
         */

        // Create our target image we will render the rotated result to.
        BufferedImage result = createOptimalImage(image, image.getWidth(),
                image.getHeight());
        Graphics2D g2d = (Graphics2D) result.createGraphics();

        /*
         * Render the resultant image to our new rotatedImage buffer, applying
         * the AffineTransform that we calculated above during rendering so the
         * pixels from the old position are transposed to the new positions in
         * the resulting image correctly.
         */
        g2d.drawImage(image, transform, null);
        g2d.dispose();

        return result;
    }

    protected static BufferedImage createOptimalImage(BufferedImage src,
            int width, int height) throws IllegalArgumentException {

        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("width [" + width
                    + "] and height [" + height + "] must be >= 0");
        }

        return new BufferedImage(
                width,
                height,
                (src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
                        : BufferedImage.TYPE_INT_ARGB));
    }
}

Related

  1. TransformGrayToTransparency(BufferedImage image)
  2. transformGrayToTransparency(BufferedImage image)
  3. transformImage(BufferedImage image, AffineTransform transform)
  4. transformImage(BufferedImage image, AffineTransform transform)
  5. transformImage(BufferedImage image, AffineTransform transform, int newWidth, int newHeight)
  6. transformToolIcon(BufferedImage source, int width, int height)
  7. translateToTopLeftMargin(Graphics2D g2d, PageFormat pageFormat)