有没有一种简单的方法可以使图片绕其中心旋转?我用了一个仿射变换运算第一的。这看起来很简单,而且需要为矩阵找到正确的参数,应该在一个漂亮而整洁的谷歌会话中完成。所以我认为...
我的结果是这样的:
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) 到 (宽度,高度)”图片尺寸的合理范围之外) 。
通过所有我没有受过训练的数学,以及计算机在计算时犯的所有错误,以及其他所有不那么容易进入我脑海的事情,我不知道如何进一步进行。请给建议。我想围绕其中心旋转图片,我想了解 AffineTransformOp。