【问题标题】:Workout area of rotated Buffered Image旋转缓冲图像的锻炼区域
【发布时间】:2012-11-24 17:36:39
【问题描述】:

在使用仿射平移旋转它后,我正在尝试计算缓冲图像的边界,如下所示:

AffineTransform at = new AffineTransform();
at.translate(x, y);
at.translate(0.5*image.getHeight(), 0.5*image.getWidth());
at.rotate(Math.PI/3);
at.translate(-0.5*image.getWidth(), -0.5*image.getHeight());
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); 
BufferedImage anotherImage = op.filter(image, null);
AffineTransform at = new AffineTransform();

       at.translate(x, y);
       at.translate(0.5*image.getHeight(), 0.5*image.getWidth());
       at.rotate(Math.PI/3);
       at.translate(-0.5*image.getWidth(), -0.5*image.getHeight());
        AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); 
       BufferedImage anotherImage = op.filter(image, null);

翻译后如何计算边界,以便我可以在 BufferedImage 周围绘制一个矩形?我正在尝试创建一个碰撞检测系统,那么如何计算缓冲图像的边界,以便我可以判断它是否与任何其他对象发生碰撞。

【问题讨论】:

    标签: java rotation area


    【解决方案1】:

    如果你想倾斜,那么可以这样做:

    public static BufferedImage tilt(BufferedImage image, double angle, GraphicsConfiguration gc) {
        double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
        int w = image.getWidth(), h = image.getHeight();
        int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
        int transparency = image.getColorModel().getTransparency();
        BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
        Graphics2D g = result.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(angle, w / 2, h / 2);
        g.drawRenderedImage(image, null);
        return result;
    }
    

    然后使用这个调用上面的函数:

    /**
     * Takes an image and tilts it by angle. Positive angle implies tilt in
     * clock-wise direction. Negative angle implies in anti-clockwise. Returns
     * null if invalid file.
     *
     *
     * @param image image to be tilted. It assumes that the image is of valid
     * image format and not some random file.
     * @param angle Angle to be rotate clockwise. Ex: Math.PI/2, -Math.PI/4
     * @retun file after tilting angle. Null if not an image
     */
    public static File tiltImageByAngle(File image, double angle, BufferedImage original) {
    GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage rotated1 = tilt(original, angle, gc);
        try {
            //write iamge
            File ans = new File(System.getProperty("java.io.tmpdir")+"temp"+angle+"."+getFileExtension(image.getName())); 
                    //new File("temp" + (int) (Math.random() * 100000) + "." + getFileExtension(image.getName()));
            ImageIO.write(rotated1, getFileExtension(image.getName()), ans);
            return ans;
        } catch (IOException ex) {
            ImageRename.LOG.log(Level.SEVERE, null, ex);
            return null;
        }
    }
    

    希望这会有所帮助

    【讨论】:

    • 我认为他们不想平铺:他们只想知道图像的矩形边界。
    【解决方案2】:

    您应该能够使用与旋转图像相同的 AffineTransform 对象:

    • 构造一个与图像大小相同的 Rectangle2D;
    • 在您的 AffineTransform 对象上调用 createTransformedShape(),传入矩形;
    • 对返回的 Shape 调用 getBounds()

    所以,一个示例测试用例:

        AffineTransform at = AffineTransform.getRotateInstance(0.1);
        Rectangle2D r = new Rectangle2D.Double(0, 0, 256, 256);
        Shape rotatedShape = at.createTransformedShape(r);
        Rectangle boundsOfRotatedShape = rotatedShape.getBounds();
        System.out.println("Bounds: " + boundsOfRotatedShape);
    

    请注意,对于碰撞检测,您可以使用 rotatedShape.contains() 来查看旋转后的图像/矩形是否实际包含该点。

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多