1

I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation. How could I remove it?

public static BufferedImage rotate(BufferedImage img, int angle) {
        rotate_checked = false;
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg =new BufferedImage(w, h,  BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g = dimg.createGraphics();
        g.rotate(Math.toRadians(angle), w/2, h/2);
        g.drawImage(img, null, 0, 0);
       return dimg;
} 
maxlego
  • 4,864
  • 3
  • 31
  • 38

1 Answers1

0

You need to create a transparent image:

BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);

where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.

Richard Wilkes
  • 309
  • 2
  • 10
  • have this performed well with you? I tried it and it was not effective – user2453120 Jun 04 '13 at 21:52
  • Yes, I've used this quite often and have seen no performance problems with it on Windows, Linux, and Mac OS X. If you'd like to share the code that was slow for you, I'd be happy to comment on it. – Richard Wilkes Jun 06 '13 at 06:29