0

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

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 周围绘制一个矩形?我正在尝试创建一个碰撞检测系统,以便计算出缓冲图像的边界,以便我可以判断它是否与任何其他对象发生碰撞。

4

2 回答 2

0

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

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

希望这会有所帮助

于 2012-11-24T17:40:38.990 回答
0

您应该能够使用与旋转图像相同的 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);

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

于 2012-11-24T18:10:05.850 回答