flip BufferedImage along X - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Rotate

Description

flip BufferedImage along X

Demo Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage flipX(BufferedImage source) {
        int type = source.getType();
        if (source.getType() == 0) {
            type = BufferedImage.TYPE_INT_ARGB; //This is a hack that works.
        }/*from  www.j a v a  2 s .c o  m*/
        BufferedImage output = new BufferedImage(source.getWidth(),
                source.getHeight(), type);
        Graphics2D g2 = output.createGraphics();
        AffineTransform tx = createTransformFlipX(source);
        g2.drawRenderedImage(source, tx);
        return output;
    }

    private static AffineTransform createTransformFlipX(BufferedImage source) {
        AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
        tx.translate(-source.getWidth(), 0);
        return tx;
    }
}

Related Tutorials