旋转缓冲图像的锻炼区域

时间:2012-11-24 17:36:40

标签: java rotation area

我正在尝试使用仿射转换来调整缓冲图像的边界,如下所示旋转它:

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

2 个答案:

答案 0 :(得分: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;
    }
}

希望这应该有所帮助

答案 1 :(得分: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);

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