-->

的AffineTransform截断图像(AffineTransform truncates ima

2019-06-24 03:33发布

我有一个图像和我具有由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();
}

问题是,像改变其位置,并得到了目标图像的边界:

问题http://img32.imageshack.us/img32/3328/resultcs.png

我GOOGLE了这一点,并发现了这个问题解决方案: 为AffineTransform截断的形象,做什么,我错了吗? 不过我挺不理解它,它仅适用于象限。 我试过两次乘以宽度和目标的高度,但它失败:

另一个失败http://img401.imageshack.us/img401/2417/result2a.png

如何解决这一问题? 目标图像不应该有任何额外的(除了需要对角旋转)空格或截面积。 角的问题(0 == 180或者是它的顺时针方向)并不重要。

谢谢你的帮助。

Answer 1:

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

旋转围绕中心进行的,并且中心被放置在目标图像中的相同位置,因为它是在源图像(正确的行为)英寸

我已经修改了代码转换的源图像的矩形,所以我们可以很容易地得到新的尺寸/图像偏移。 这是用于构建目的地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));


Answer 2:

我建议更换

AffineTransformOp op = new AffineTransformOp(t, null);

AffineTransformOp op = new AffineTransformOp(t,  AffineTransformOp.TYPE_BILINEAR);

它会提高很多输出的质量。



文章来源: AffineTransform truncates image