Java BufferedImage Flip flipImageHorizontally(BufferedImage image)

Here you can find the source of flipImageHorizontally(BufferedImage image)

Description

A method which will invert an image with respect to its y-axis

License

Open Source License

Parameter

Parameter Description
image image to be flipped

Return

a flipped image

Declaration

public static BufferedImage flipImageHorizontally(BufferedImage image) 

Method Source Code


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

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main {
    /**//  w w w  .j av  a 2s  .c o m
     * A method which will invert an image with respect to its y-axis
     *
     * @param image     image to be flipped
     * @return          a flipped image
     */
    public static BufferedImage flipImageHorizontally(BufferedImage image) {

        /* Create a new clean image of the same size/type */
        BufferedImage flipped = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());

        /* Instantiate Affine transformation for flipping and translating */
        AffineTransform tran = AffineTransform.getTranslateInstance(image.getWidth(), 0);
        AffineTransform flip = AffineTransform.getScaleInstance(-1d, 1d);

        /* Merge these */
        tran.concatenate(flip);

        /* Creates a Graphics2D object linked  */
        Graphics2D g = flipped.createGraphics();

        /* Set the transformation on the graphic */
        g.setTransform(tran);

        /* Draw the image onto the graphic */
        g.drawImage(image, 0, 0, null);

        /* Now dispose of the graphic */
        g.dispose();

        /* Return the flipped image */
        return flipped;
    }
}

Related

  1. flipHorizontal(BufferedImage bufferedImage)
  2. flipHorizontally(BufferedImage srcImage)
  3. flipImage(BufferedImage image)
  4. flipImage(final BufferedImage image, final boolean horizontal, final boolean vertical)
  5. flipImage(Object original, boolean flipX, boolean flipY)
  6. flipImageVertically(BufferedImage theImage)
  7. flipVertical(BufferedImage im)
  8. flipVertically(BufferedImage image)
  9. flipVertically(BufferedImage image)