java - AffineTransform 截断图像

标签 java image-processing rotation image-rotation affinetransform

我有一张图片,我必须将它旋转 45、90、135、180 度。我在做什么:

try {
    BufferedImage src = ImageIO.read(new File("src.png"));
    double ang = Math.toRadians(90);

    AffineTransform t = new AffineTransform();
    t.setToRotation(ang, src.getWidth() / 2, src.getHeight() / 2);

    AffineTransformOp op = new AffineTransformOp(t, null);
    BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
    op.filter(src, dst);

    ImageIO.write(dst, "png", new File("output.png"));
} catch(Exception ex) { ex.printStackTrace();
}

问题是图像改变了它的位置并超出了目标图像的范围:

The problem

我用谷歌搜索并找到了这个问题的解决方案:AffineTransform truncates image, what do I wrong?但我很不明白它只适用于象限。我尝试将目标的宽度和高度乘以两倍,但失败了:

Another fail

如何解决这个问题?目标图像不应有任何额外的(对角线旋转所需的除外)空白或截断区域。角度问题(0 == 180 或顺时针方向)并不重要。

感谢您的帮助。

最佳答案

编辑:现在它适用于一般情况。

围绕中心执行旋转,并且中心在目标图像中的位置与在源图像中的位置相同(正确行为)。

我已经修改了您的代码以转换源图像矩形,以便我们可以轻松获得新的尺寸/图像偏移量。这用于构建正确尺寸的目标 BufferedImage,并将转换附加到您的 AffineTransform,以便图像中心位于目标图像的中心。

        BufferedImage src = ImageIO.read(new File(INPUT));
        int w = src.getWidth();
        int h = src.getHeight();

        AffineTransform t = new AffineTransform();
        double ang = Math.toRadians(35);
        t.setToRotation(ang, w / 2d, h / 2d);

        // source image rectangle
        Point[] points = {
            new Point(0, 0),
            new Point(w, 0),
            new Point(w, h),
            new Point(0, h)
        };

        // transform to destination rectangle
        t.transform(points, 0, points, 0, 4);

        // get destination rectangle bounding box
        Point min = new Point(points[0]);
        Point max = new Point(points[0]);
        for (int i = 1, n = points.length; i < n; i ++) {
            Point p = points[i];
            double pX = p.getX(), pY = p.getY();

            // update min/max x
            if (pX < min.getX()) min.setLocation(pX, min.getY());
            if (pX > max.getX()) max.setLocation(pX, max.getY());

            // update min/max y
            if (pY < min.getY()) min.setLocation(min.getX(), pY);
            if (pY > max.getY()) max.setLocation(max.getX(), pY);
        }

        // determine new width, height
        w = (int) (max.getX() - min.getX());
        h = (int) (max.getY() - min.getY());

        // determine required translation
        double tx = min.getX();
        double ty = min.getY();

        // append required translation
        AffineTransform translation = new AffineTransform();
        translation.translate(-tx, -ty);
        t.preConcatenate(translation);

        AffineTransformOp op = new AffineTransformOp(t, null);
        BufferedImage dst = new BufferedImage(w, h, src.getType());
        op.filter(src, dst);

        ImageIO.write(dst, "png", new File(OUTPUT));

关于java - AffineTransform 截断图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10426883/

相关文章:

java - Artifact 丢失 : org. hibernate :hibernate-entitymanager:jar:3. 3.2.ga

java - 关于 Java 动态转换的大学任务

php - 从图像到数字

python - 根据图像 python 内的边框裁剪图像

c# - XNA 中的四元数旋转

c++ - 二维旋转opengl

java - servlet 不可用

Java Unicode 字符宽度

image-processing - 使用opencv进行分词

SVG 变换原点在 Inkscape 中被破坏