Java Graphics2D获得旋转图像边界和新协调

时间:2015-07-28 14:36:52

标签: java rotation graphics2d

我在Graphics2D中有一个我需要旋转的图像,然后获得图像角的新坐标新边界框的尺寸。

enter image description here

我最初尝试使用图像本身,但我认为使用矩形(或多边形)更容易为自己提供更大的灵活性。我最初只是使用AffineTransform.rotate()在图像上执行旋转。但是,如果有一种方法可以单独转换每个角点,那将会更清晰,这将给出A1,B1,C1和A1的值。 D1。 Graphics2D中有没有办法旋转各个角落?

我发现了一些与旋转矩形的边界框尺寸有关的问题,但我似乎无法使用Graphics2D在Java中使用它们。

1 个答案:

答案 0 :(得分:2)

您只需自己旋转图像角落即可。包java.awt.geom通过将旋转变换应用于各个点来提供类Point2DAffineTransform。旋转的边界框的宽度和高度可以计算为最大和最大旋转x和y坐标之间的差异,最小x和y坐标为偏移。

以下程序实现了该算法,并以30°的步长显示从0°到360°的几次旋转的结果:

package stackoverflow;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * Demonstration of an implementation to rotate rectangles.
 * @author Franz D.
 */
public class ImageRotate
{
    /**
     * Rotates a rectangle with offset (0,0).
     * @param originalWidth original rectangle width
     * @param originalHeight original rectangle height
     * @param angleRadians rotation angle in radians
     * @param rotatedCorners output buffer for the four rotated corners
     * @return the bounding box of the rotated rectangle
     * @throws NullPointerException if {@code rotatedCorners == null}.
     * @throws ArrayIndexOutOfBoundsException if {@code rotatedCorners.length < 4}.
     */
    public static Rectangle2D rotateRectangle(int originalWidth, int originalHeight,
            double angleRadians,
            Point2D[] rotatedCorners) {
        // create original corner points
        Point2D a0 = new Point2D.Double(0, 0);
        Point2D b0 = new Point2D.Double(originalWidth, 0);
        Point2D c0 = new Point2D.Double(0, originalHeight);
        Point2D d0 = new Point2D.Double(originalWidth, originalHeight);
        Point2D[] originalCorners = { a0, b0, c0, d0 };

        // create affine rotation transform
        AffineTransform transform = AffineTransform.getRotateInstance(angleRadians);

        // transform original corners to rotated corners
        transform.transform(originalCorners, 0, rotatedCorners, 0, originalCorners.length);

        // determine rotated width and height as difference between maximum and
        // minimum rotated coordinates
        double minRotatedX = Double.POSITIVE_INFINITY;
        double maxRotatedX = Double.NEGATIVE_INFINITY;
        double minRotatedY = Double.POSITIVE_INFINITY;
        double maxRotatedY = Double.NEGATIVE_INFINITY;

        for (Point2D rotatedCorner: rotatedCorners) {
            minRotatedX = Math.min(minRotatedX, rotatedCorner.getX());
            maxRotatedX = Math.max(maxRotatedX, rotatedCorner.getX());
            minRotatedY = Math.min(minRotatedY, rotatedCorner.getY());
            maxRotatedY = Math.max(maxRotatedY, rotatedCorner.getY());
        }

        // the bounding box is the rectangle with minimum rotated X and Y as offset
        double rotatedWidth = maxRotatedX - minRotatedX;
        double rotatedHeight = maxRotatedY - minRotatedY;

        Rectangle2D rotatedBounds = new Rectangle2D.Double(
                minRotatedX, minRotatedY,
                rotatedWidth, rotatedHeight);

        return rotatedBounds;
    }

    /**
     * Simple test for {@link #rotateRectangle(int, int, double, java.awt.geom.Point2D[])}.
     * @param args ignored
     */
    public static void main(String[] args) {
        // setup original width
        int originalWidth = 500;
        int originalHeight = 400;

        // create buffer for rotated corners
        Point2D[] rotatedCorners = new Point2D[4];

        // rotate rectangle from 0° to 360° in 30° steps
        for (int angleDegrees = 0; angleDegrees < 360; angleDegrees += 30) {
            // convert angle to radians
            double angleRadians = Math.toRadians(angleDegrees);

            // rotate rectangle
            Rectangle2D rotatedBounds = rotateRectangle(
                    originalWidth, originalHeight,
                    angleRadians,
                    rotatedCorners);

            // dump results
            System.out.println("--- Rotate " + originalWidth + "x" + originalHeight + " by " + angleDegrees + "° ---");
            System.out.println("Bounds: " + rotatedBounds);
            for (Point2D rotatedCorner: rotatedCorners) {
                System.out.println("Corner " + rotatedCorner);
            }
        }
    }
}

如果图像未放置在偏移(0,0)处,则可以简单地修改方法以将偏移量作为输入参数,并将偏移坐标添加到原始点。

此外,此方法围绕原点(0,0)旋转图像(或矩形)。如果您需要其他轮换中心,AffineTransform会提供getRotateInstace()的重载变体,允许您指定轮换中心(在API文档中称为“锚点”)。