Java image rotation with AffineTransform outputs black image, but works well when resized

10,150

Solution 1

Passing a new BufferedImage into the filter() method rather than letting it create its own works (not completely black).

Also the transform did not appear to work correctly, the image ended up being offset in the destination. I was able to fix it by manually applying the necessary translations, note these work in reverse order, and in the destination image the width = the old height, and height = the old width.

AffineTransform tx = new AffineTransform();

// last, width = height and height = width :)
tx.translate(originalImage.getHeight() / 2,originalImage.getWidth() / 2);
tx.rotate(Math.PI / 2);
// first - center image at the origin so rotate works OK
tx.translate(-originalImage.getWidth() / 2,-originalImage.getHeight() / 2);

AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

// new destination image where height = width and width = height.
BufferedImage newImage =new BufferedImage(originalImage.getHeight(), originalImage.getWidth(), originalImage.getType());
op.filter(originalImage, newImage);

The javadoc for filter() states that it will create a BufferedImage for you, I'm still unsure why this does not work, there must be an issue here.

 If the destination image is null, a BufferedImage is created with the source ColorModel.

Solution 2

If you were open to the idea of using a 3rd party lib (very small, just 2 classes) imgscalr can do this for you in a single line while working around all the filter gotchas that different image types can cause.

Using Scalr.rotate(...) would look something like this:

BufferedImage newImage = Scalr.rotate(originalImage, Rotation.CW_90);

If this rotation is part of a larger app that processes images, you can even do this asynchronously if you needed that (AsyncScalr class).

imgscalr is under an Apache 2 license and all source is available; if you'd rather do this yourself by-hand, read through the code for the rotate() method, I've documented all the gotchas that can spring up when working with filters in Java2D.

Hope that helps!

Share:
10,150
ahmet alp balkan
Author by

ahmet alp balkan

I am a software engineer on Twitter compute infrastructure team. Previously I've worked at Google Cloud on Kubernetes, Cloud Run and Knative, and at Microsoft Azure on various parts of the Docker open source ecosystem. Find me on my: (blog | twitter | github)

Updated on June 06, 2022

Comments

  • ahmet alp balkan
    ahmet alp balkan almost 2 years

    I am just trying to rotate a JPG file by 90 degrees. However my code outputs image (BufferedImage) that is completely black.

    Here's the way to reproduce: (Download 3.jpg here)

    private static BufferedImage transform(BufferedImage originalImage) {
        BufferedImage newImage = null;
        AffineTransform tx = new AffineTransform();
        tx.rotate(Math.PI / 2, originalImage.getWidth() / 2, originalImage.getHeight() / 2);
    
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
        newImage = op.filter(originalImage, newImage);
    
        return newImage;
    }
    
    public static void main(String[] args) throws Exception {
        BufferedImage bi = transform(ImageIO.read(new File(
                "3.jpg")));
        ImageIO.write(bi, "jpg", new File("out.jpg"));
    
    }
    

    What's wrong here?

    (if I give this black output BufferedImage to a image resizer library, it gets resized well, original image is still there.)