Java - Image Rotation

前端 未结 4 1130
野的像风
野的像风 2020-12-06 06:58

I am trying to rotate image. I am using this Java code:

BufferedImage oldImage = ImageIO.read(new FileInputStream(\"C:\\\\workspace\\\\test\\\\src\\\\10.JPG\         


        
相关标签:
4条回答
  • 2020-12-06 07:01

    It is not enough to switch the width and height of the image. You are rotating using the center of the image as the origin of rotation. Just try the same with a sheet of paper and you will see it works the same way. You must also move the paper a little bit, which means to apply a transform to fix this. So, immediately after the rotate call, do this:

      graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);
    
    0 讨论(0)
  • 2020-12-06 07:01

    Try getting bounds of your panel on which you do your drawing

    Rectangle rect = this.getBounds();
    

    And then do:

    graphics.rotate(Math.toRadians(90), (rect.width - newImage.getWidth()) / 2, (rect.height - newImage.getHeight()) / 2);
    

    Hope that could help Cheers!

    0 讨论(0)
  • The new image has different sizes because of the rotate. try this: BufferedImage newImage = new BufferedImage( oldImage.getWidth(),oldImage.getHeight(),oldImage.getType());

    0 讨论(0)
  • 2020-12-06 07:21

    You can write like this it will be work.

    BufferedImage newImage = new BufferedImage(oldImage.getWidth(), oldImage.getHeight(), oldImage.getType());
    

    I think the place for width and height is wrong in your code.

    0 讨论(0)
提交回复
热议问题