0

I try to rotate a BufferedImage and it works, but the rotated image has a black border around some sides and I don't know why... I also feel like it gets smaller after rotating.

import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class example {

   static void main(String[] args) throws IOException {

        BufferedImage imgResc = ImageIO.read(new File("landscape.jpg"));        

        AffineTransform tx = new AffineTransform();
        tx.rotate(Math.PI / 2, imgResc.getWidth() / 2, imgResc.getHeight() / 2);//(radian,arbit_X,arbit_Y)
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage rotatedImage = new BufferedImage(imgResc.getHeight(),imgResc.getWidth(),imgResc.getType());
        op.filter(imgResc, rotatedImage);
        ImageIO.write(rotatedImage, "JPEG", new File("rotated_90_right.jpg"));
    } 
}

Above is the code snippet for rotating the image like I found it online.

Help would be great!

EDIT:

This is how it should look like and what I get after rotating: (1920 x 1200)

enter image description here

ViktorG
  • 505
  • 1
  • 7
  • 30
  • 1
    Is it possible that your width or height is odd? If so, your divisions by two will introduce rounding error, shrinking the image by a pixel. Is that the size of the border? – Andrew Nov 16 '16 at 20:07
  • I edited the question and added an img of whats the result. – ViktorG Nov 16 '16 at 20:10
  • It also isn't rotated in what you posted... – Andrew Nov 16 '16 at 20:15
  • @Andrew what dou you mean ? – ViktorG Nov 16 '16 at 20:15
  • The images I see have the same dimensions and orientation; the second one is just shrunk with a border but it is not rotated. Also looks like other people have had this problem too. See: http://stackoverflow.com/questions/34143111/java-rotate-image-turns-part-of-background-black?rq=1 – Andrew Nov 16 '16 at 20:17
  • @Andre Ah I see, seems like a misunderstanding. I is basically what the Image should look like when rotated and the other is what i actually get. – ViktorG Nov 16 '16 at 20:19
  • @ViktorG Has the link that Andrew posted provides you with your answer? If so, let us know so we can close this question, okay? – ControlAltDel Nov 16 '16 at 20:25
  • @ControlAltDel Actually not... I tried the solution from the link but I get the same result. – ViktorG Nov 16 '16 at 20:33
  • @Jean-BaptisteYunès That Helped me. Thank you :) – ViktorG Nov 16 '16 at 21:03

1 Answers1

1

If you rotate by PI/2 then the new image has width the height of the source and height the width of the source, so:

BufferedImage rotatedImage = new BufferedImage(imgResc.getHeight(),
                                               imgResc.getWidth(),
                                               imgResc.getType());

would be better.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • This is good advice, but it doesn't answer the question asked, as the OP described a black border around all sides, and the image possibly shrinking – ControlAltDel Nov 16 '16 at 20:04
  • You are right, thank you. The problem with the black border around still remains, though. – ViktorG Nov 16 '16 at 20:05