Java – image rotation
•
Java
I tried to rotate the image I use this java code:
BufferedImage oldImage = ImageIO.read(new FileInputStream("C:\\workspace\\test\\src\\10.JPG")); BufferedImage newImage = new BufferedImage(oldImage.getHeight(),oldImage.getWidth(),oldImage.getType()); Graphics2D graphics = (Graphics2D) newImage.getGraphics(); graphics.rotate(Math.toradians(90),newImage.getWidth() / 2,newImage.getHeight() / 2); graphics.drawImage(oldImage,oldImage.getHeight(),null); ImageIO.write(newImage,"JPG",new FileOutputStream("C:\\workspace\\test\\src\\10_.JPG"));
But I saw strange results:
resources:
result:
**Result image:** http://s14.postimage.org/cjut935ip/image.jpg
Can you help me solve this problem?
Solution
Switching the width and height of the image is not enough You use the center of the image as the rotation origin Just try a piece of paper and you'll see that it works the same way You also have to move the paper a little, which means applying conversion to solve this problem Therefore, after the rotation call, do the following:
graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2,(newImage.getHeight() - oldImage.getHeight()) / 2);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码