开发者

rotate a picture around it's center

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

My Result is this:

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

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

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).

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.


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜