2012-08-28 4 views
10

이미지를 회전하려고합니다. 나는이 자바 코드를 사용하고 있습니다 :자바 - 이미지 회전

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, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null); 
ImageIO.write(newImage, "JPG", new FileOutputStream("C:\\workspace\\test\\src\\10_.JPG")); 

을하지만 난 이상한 결과를 참조하십시오

출처 :

**Sourse image:**

결과 :

**Result image:** http://s14.postimage.org/cjut935ip/image.jpg

것은 당신이 도와주세요 수를 이 문제가있어?

+3

newImage가 아닌 oldImage의 중심을 중심으로 회전하면 안됩니까? –

+0

시도해보십시오. graphics.rotate (Math.toRadians (90), oldImage.getWidth()/2, oldImage.getHeight()/2); 도움이되지 않습니다. http://s13.postimage.org/7omxa1oef/image.jpg – user1631241

답변

14

이미지의 너비와 높이를 전환하는 것만으로는 충분하지 않습니다. 이미지 중심을 회전 원점으로 사용하여 회전합니다. 한장의 종이로 똑같은 것을 시도하면 똑같은 방식으로 작동하는 것을 볼 수 있습니다. 또한 용지를 약간 옮겨야하는데, 이는 변형을 적용하여이를 수정해야 함을 의미합니다. 따라서 전화를 거는 즉시, 다음과 같이하십시오.

graphics.translate((newImage.getWidth() - oldImage.getWidth())/2, (newImage.getHeight() - oldImage.getHeight())/2); 
+0

그것은 작동한다! 정말 고마워! – user1631241

+0

나는 알고있다 :) 너는 천만에. –

+0

나는 이것에 대해 즉시 생각하지 않는 것에 대해 부끄럽다. :-p – oldrinb

0

회전 때문에 새 이미지의 크기가 다릅니다. 이것을 시도하십시오 : BufferedImage newImage = new BufferedImage (oldImage.getWidth(), oldImage.getHeight(), oldImage.getType()); 당신의 패널의

+0

어, 그는 다른 크기가 있다는 것을 잘 알고 있습니다. 코드가 아무 것도 해결하지 못합니다. – oldrinb

+0

이미 시도 중입니다. 이것 이후 나는 가지고있다 : http://s11.postimage.org/4jvhjtewz/image.jpg – user1631241

0

시도 점점 경계가되는 당신이 다음 그림

Rectangle rect = this.getBounds(); 

을하고는 수행 건배를 도울 수

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

희망을!

+0

는 1 분 동안 늦었다. .. 잘 : P –

0

이렇게하면 쓸 수 있습니다.

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

나는 widthheight을위한 장소가 코드에서 잘못된 생각합니다.