Java: Rotación de imágenes

Necesito poder rotar imágenes individualmente(en java). Lo único que he encontrado hasta ahora es g2d.drawImage(image, affinetransform, ImageObserver ). Desafortunadamente, necesito dibujar la imagen en un punto específico, y no hay ningún método con un argumento que 1.rote la imagen por separado y 2. me permita establecer la x y la y. cualquier ayuda es apreciada

Solución

Así es como puedes hacerlo. Este código asume la existencia de una imagen en buffer llamada 'imagen' (como dice tu comentario)

// The required drawing location
int drawLocationX = 300;
int drawLocationY = 300;

// Rotation information

double rotationRequired = Math.toRadians (45);
double locationX = image.getWidth() / 2;
double locationY = image.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null);
Comentarios (2)

Las instancias de AffineTransform pueden concatenarse (sumarse). Por tanto, puede tener una transformación que combine 'desplazamiento al origen', 'rotación' y 'desplazamiento de vuelta a la posición deseada'.

Comentarios (1)
public static BufferedImage rotateCw( BufferedImage img )
{
    int         width  = img.getWidth();
    int         height = img.getHeight();
    BufferedImage   newImage = new BufferedImage( height, width, img.getType() );

    for( int i=0 ; i < width ; i++ )
        for( int j=0 ; j < height ; j++ )
            newImage.setRGB( height-1-j, i, img.getRGB(i,j) );

    return newImage;
}

de https://coderanch.com/t/485958/java/Rotating-buffered-image

Comentarios (0)