0

When I try to rotate an image it appears that a part of the background turns black (my images are transparents)

Background white
Part of background turns black

Here's the code that rotate the image :

public BufferedImage rotate(int height, int width, BufferedImage originalImg, int angle) {
    BufferedImage rotateImage = null;
    try {
        rotateImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
        AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), height / 2, width / 2);
        AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
        op90.filter(originalImg, rotateImage);
    }
    catch (Exception e) {
        System.err.println(e);
    }
    return rotateImage;
}
Kariamoss
  • 542
  • 1
  • 9
  • 29
  • 2
    All APIs that accept both width and height will have the width argument come before the height argument. I'm not sure if you have switched them around deliberately. Regardless, the BufferedImage constructor expects width followed by height, and the AffineTransform.getRotateInstance method expects an X coordinate followed by a Y coordinate. – VGR Dec 07 '15 at 21:21
  • Well my images are all square so it doesn't change much and the getRotateInstance needs an anchor to rotate around so I sent the middle of my image with height/2 and width/2 – Kariamoss Dec 07 '15 at 21:37
  • 1
    This concerns me: `BufferedImage.TYPE_INT_RGB`. Shouldn't that be `BufferedImage.TYPE_INT_ARGB`? You've got transparency, meaning you need alpha. – Hovercraft Full Of Eels Dec 07 '15 at 21:53
  • It works ! Thanks a lot I didn't see that – Kariamoss Dec 07 '15 at 22:05

1 Answers1

4

So, I downloaded you "original" image (which is not square), modified it so it was square, run your code, got a java.awt.image.ImagingOpException: Unable to transform src image exception, changed BufferedImage.TYPE_INT_RGB to BufferedImage.TYPE_INT_ARGB and got...

Rotated

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        try {
            BufferedImage img = ImageIO.read(Main.class.getResource("/Block.jpg"));
            BufferedImage rotate = rotate(img.getHeight(), img.getWidth(), img, 90);

            JPanel panel = new JPanel();
            panel.add(new JLabel(new ImageIcon(img)));
            panel.add(new JLabel(new ImageIcon(rotate)));

            JOptionPane.showMessageDialog(null, panel);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static BufferedImage rotate(int height, int width, BufferedImage originalImg, int angle) {
        BufferedImage rotateImage = null;
        try {
            rotateImage = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);
            AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), height / 2, width / 2);
            AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
            op90.filter(originalImg, rotateImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rotateImage;
    }

}

My, "gut" feeling is to also modify the rotate method, as it shouldn't need width and height values;

public static BufferedImage rotate(BufferedImage originalImg, int angle) {
    BufferedImage rotateImage = null;
    try {
        rotateImage = new BufferedImage(originalImg.getWidth(), originalImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
        AffineTransform a90 = AffineTransform.getRotateInstance(Math.toRadians(angle), originalImg.getWidth() / 2, originalImg.getHeight() / 2);
        AffineTransformOp op90 = new AffineTransformOp(a90, AffineTransformOp.TYPE_BILINEAR);
        op90.filter(originalImg, rotateImage);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotateImage;
}

it should be using the original image's dimensions directly. This is will highlight possible errors in your images. This also assumes that you only want to rotate the image by increments of 90 degrees

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366