2015-09-29 52 views
-1

旋转图像在两侧缓冲图像,图像图标切断,JAVA

 BufferedImage img = ImageIO.read(new File(paramString)); 
    double locationX = img.getWidth(this)/2; 
    double locationY = img.getHeight(this)/2; 
    AffineTransform tx = AffineTransform.getRotateInstance(Math.toRadians(paramInt3), locationX, locationY); 
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); 
    BufferedImage bimg = new BufferedImage (img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_BYTE_INDEXED); 
    bimg = op.filter (img, null); 
    ImageIcon localImageIcon = new ImageIcon (bimg); 

为什么会一边被切断? 我正在使用一个需要最终结果为ImageIcon的库。

+0

问题在哪里? – jhamon

+0

我的代码旋转图像,但顶部和左侧的角落被切断 – ogprogrammingsquad

回答

0

你的目标图像在这里创造:

BufferedImage bimg = new BufferedImage (img.getWidth(this), img.getWidth(this), BufferedImage.TYPE_BYTE_INDEXED); 

这是一个正方形!如果你的源图像不是正方形你应该建立这样的目标图像:

BufferedImage bimg = new BufferedImage (img.getHeight(this), img.getWidth(this), BufferedImage.TYPE_BYTE_INDEXED); 
+0

谢谢,但图像是方形的,角落仍然在顶部和左侧被切割。 – ogprogrammingsquad

+0

虽然明显是一个错误,但这不是真正的问题,因为'bimg'引用被覆盖在下一行。也许@ogprogrammingsquad意味着'op.filter(img,bimg);'?当传递'null'作为输出参数时,目标被创建。 – haraldK

0

也许这只是一个四舍五入的问题:

double locationX = img.getWidth(this)/2; 
double locationY = img.getHeight(this)/2; 

宽度和高度都是整数,如果他们甚至没有你可能会遇到问题。试试这个:

double locationX = ((double)img.getWidth(this))/2; 
double locationY = ((double)img.getHeight(this))/2;