Sorun kaynak resim tam olarak kuadratik değil mi: Bu dönme verilen açıda görüntüyü göstermek için gerekli boyutu yeniden hesaplar. son satır çalıştırmak zaman
at.translate(width/2,height/2);
at.rotate(rads);
at.translate(-width/2,-height/2);
, yani, bu şöyle açıklanabilir: Eğer at.rotate ile AffineTransform dönme uygulamak (genişlik -rad,/2, yükseklik/2) ;, o aynıdır Menşei. Ve eğer genişlik y'den büyükse (veya tersi), dönüşümün kaynağından daha büyük uzunluktan daha küçük bir mesafeye çevrilecektir.
Örneğin, genişliğiniz 30 ve boyunuz 60 ise, başlangıç noktasından, dönüşümün orijinal ayarlandığı yerden (-15, -30) olarak ayarlanacaktır.Yani, çevirdiğinizde, 90 derece, görüntü "genişlik" 60 ve "yükseklik" 30 ile sona erecektir, ancak başlangıç noktasına göre, görüntü orijinal alt (-30,0) 'de çizilecektir, Böylece, AffinTransformu -15'te X ekseninde taşar. Sonra görüntünün bu kısmı kesilecek.
Bunu düzeltmek için, bunun yerine aşağıdaki kodu kullanabilirsiniz:
double degreesToRotate = 90;
double locationX =bufferedImage.getWidth()/2;
double locationY = bufferedImage.getHeight()/2;
double diff = Math.abs(bufferedImage.getWidth() - bufferedImage.getHeight());
//To correct the set of origin point and the overflow
double rotationRequired = Math.toRadians(degreesToRotate);
double unitX = Math.abs(Math.cos(rotationRequired));
double unitY = Math.abs(Math.sin(rotationRequired));
double correctUx = unitX;
double correctUy = unitY;
//if the height is greater than the width, so you have to 'change' the axis to correct the overflow
if(bufferedImage.getWidth() < bufferedImage.getHeight()){
correctUx = unitY;
correctUy = unitX;
}
int posAffineTransformOpX = posX-(int)(locationX)-(int)(correctUx*diff);
int posAffineTransformOpY = posY-(int)(locationY)-(int)(correctUy*diff);
//translate the image center to same diff that dislocates the origin, to correct its point set
AffineTransform objTrans = new AffineTransform();
objTrans.translate(correctUx*diff, correctUy*diff);
objTrans.rotate(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(objTrans, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
graphic2dObj.drawImage(op.filter(bufferedImage, null), posAffineTransformOpX, posAffineTransformOpY, null);
faydası Umut.