Java 围绕它旋转图片';s中心
有没有一种简单的方法可以使图片围绕中心旋转?我用了第一个。这看起来很简单,而且需要,为矩阵找到正确的参数应该在一个漂亮整洁的google会话中完成。所以我想 我的结果是:Java 围绕它旋转图片';s中心,java,awt,Java,Awt,有没有一种简单的方法可以使图片围绕中心旋转?我用了第一个。这看起来很简单,而且需要,为矩阵找到正确的参数应该在一个漂亮整洁的google会话中完成。所以我想 我的结果是: public class RotateOp implements BufferedImageOp { private double angle; AffineTransformOp transform; public RotateOp(double angle) { this.ang
public class RotateOp implements BufferedImageOp {
private double angle;
AffineTransformOp transform;
public RotateOp(double angle) {
this.angle = angle;
double rads = Math.toRadians(angle);
double sin = Math.sin(rads);
double cos = Math.cos(rads);
// how to use the last 2 parameters?
transform = new AffineTransformOp(new AffineTransform(cos, sin, -sin,
cos, 0, 0), AffineTransformOp.TYPE_BILINEAR);
}
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
return transform.filter(src, dst);
}
}
如果忽略旋转90度的倍数的情况(sin()和cos()无法正确处理这一情况),则非常简单。该解决方案的问题是,它围绕图片左上角的(0,0)坐标点进行变换,而不是围绕图片中心进行变换,这通常是预期的。因此,我在过滤器中添加了一些内容:
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
//don't let all that confuse you
//with the documentation it is all (as) sound and clear (as this library gets)
AffineTransformOp moveCenterToPointZero = new AffineTransformOp(
new AffineTransform(1, 0, 0, 1, (int)(-(src.getWidth()+1)/2), (int)(-(src.getHeight()+1)/2)), AffineTransformOp.TYPE_BILINEAR);
AffineTransformOp moveCenterBack = new AffineTransformOp(
new AffineTransform(1, 0, 0, 1, (int)((src.getWidth()+1)/2), (int)((src.getHeight()+1)/2)), AffineTransformOp.TYPE_BILINEAR);
return moveCenterBack.filter(transform.filter(moveCenterToPointZero.filter(src,dst), dst), dst);
}
我在这里的想法是,改变形式的矩阵应该是单位矩阵(这是正确的英语单词吗?),移动整个图片的向量是最后两个条目。我的解决方案是先将图片放大,然后再缩小(其实没什么大不了的-原因未知!!!),还将图片的3/4部分切掉(重要的是-原因可能是图片超出了“从(0,0)到(宽度,高度)”图片尺寸标注的合理范围)
尽管我没有受过那么多的数学训练,计算机在计算时犯下的所有错误,以及其他不那么容易进入我脑海的事情,我不知道如何进一步。请给出建议。我想围绕图片的中心旋转图片,我想理解仿射变换。如果我正确理解了您的问题,您可以将其转换为原点、旋转并向后平移,如图所示 在您使用时,这可能更恰当。特别是,请注意最后指定的第一个应用顺序,其中操作是串联的;它们是不可交换的。方法将矩阵设置为乘法恒等式。