旋转图片围绕它的中心 [英] rotate a picture around it's center

查看:116
本文介绍了旋转图片围绕它的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的办法解决它的中心旋转图片?我用了一个 AffineTransformOp 第一。它看似简单和需要,并找到一个矩阵正确的参数应该在一个不错的整洁的谷歌会议上完成。所以我想...

Is there an easy way to rotate a picture around it's center? I used an AffineTransformOp first. It seems simple and need and finding the right parameters for a matrix should be done in a nice and neat google session. So I thought...

我的结果是这样的:

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度的倍数的情况下,真正简单的(不能被罪()和cos(正确处理))。与该解决方案的问题是,它变换(0,0)周围的图像的左上角,而不是周围的图象,这将是通常期望的中心坐标点。所以我加了一些东西到我的过滤器:

Really simple if you ignore the cases of rotating multiples of 90 degrees (which can't be handled correctly by sin() and cos()). The problem with that solution is, that it transforms around the (0,0) coordinate point in the upper left corner of the picture and not around the center of the picture what would be normally expected. So I added some stuff to my filter:

    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);
    }

在这里我的想法是,形式不断变化的矩阵应该是单位矩阵(是正确的英文单词?)和移动全貌矢量周围是最后2项。我的解决办法首先使得画面更大,然后再小的(没有真正的问题那么多 - 原因不明!),并也减少周围画拿走了(什么是相当重要的3/4 - 原因可能是该图片是合理的地平线(0,0)到(宽,高)图片尺寸)以外的移动。

My thinking here was that the form changing matrix should be the unity matrix (is that the right english word?) and the vector that moves the whole picture around are the last 2 entries. My solution first makes the picture bigger and then smaller again (doesn't really matter that much - reason unknown!!!) and also cuts around 3/4 of the picture away (what matters a lot - reason is probably that the picture is moved outside of the reasonable horizon of the "from (0,0) to (width,height)" picture dimensioning).

通过我不是那么培养出来的所有的数学和所有在计算计算机是错误和其他一切不会进入我的头这么容易,我不知道该怎么走的更远。请给予建议。我想绕其中心的画面,我想了解AffineTransformOp。

Through all the mathematics I am not so trained in and all the errors the computer does while calculating and everything else that doesn't get into my head so easily, I don't know how to go further. Please give advice. I want to rotate the picture around its center and I want to understand AffineTransformOp.

推荐答案

如果我正确地理解你的问题,你可以转化为原点,旋转和转换回,​​如本的example

If I understand your question correctly, you can translate to the origin, rotate, and translate back, as shown in this example.

由于您使用的是 AffineTransformOp ,这例如可能会更加中肯。特别要注意的的最后指定第一应用的顺序操作被连接起来;他们的的交换。

As you are using AffineTransformOp, this example may be more apropos. In particular, note the last-specified-first-applied order in which operations are concatenated; they are not commutative.

这篇关于旋转图片围绕它的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆