2010-11-11 32 views
7

Estoy buscando rotar una imagen. Tengo un JInternalFrame que contiene un JLabel. La etiqueta contiene la imagen. Después de rotar la imagen, necesito cambiar el tamaño del marco interno. El código que tengo actualmente rota la imagen, pero hay negro alrededor de los bordes de la imagen y está desactivado. Alguna sugerencia en como arreglar esto?Girar una imagen en java

public void rotateIcon(int angle) 
{ 
     int w = theLabel.getIcon().getIconWidth(); 
     int h = theLabel.getIcon().getIconHeight(); 
     int type = BufferedImage.TYPE_INT_RGB; // other options, see api 

     BufferedImage DaImage = new BufferedImage(h, w, type); 
     Graphics2D g2 = DaImage.createGraphics(); 

     double x = (h - w)/2.0; 
     double y = (w - h)/2.0; 
     AffineTransform at = AffineTransform.getTranslateInstance(x, y); 

     at.rotate(Math.toRadians(angle), w/2.0, h/2.0); 
     g2.drawImage(new ImageIcon(getData()).getImage(), at, theLabel); 
     g2.dispose(); 

     theLabel.setIcon(new ImageIcon(DaImage)); 
     this.setSize(DaImage.getWidth(),DaImage.getHeight()); //resize the frame 
} 
+0

En general, la rotación de una imagen cambia la anchura y la altura (en relación con ejes X e Y). Supongo que esto está contribuyendo a que sea "fuera de foco". Tuve que resolver esto calculando el nuevo tamaño y contabilizándolo. En cuanto a los bordes negros, este es un caso bastante común en el que la función de rotación no funciona con un canal alfa. Tal vez esto ayude para el posicionamiento: http://stackoverflow.com/questions/2056338/calculating-the-center-of-rotation-after-translation – Jere

+1

Véase también http://stackoverflow.com/questions/3420651 – trashgod

Respuesta

15

Debe usar la trigonometría para determinar el ancho/alto correcto, usando transparencia para evitar el área negra, y creo que la Transformación está mal, lo que la hace descentrada.

Prueba esto:

public static BufferedImage rotate(BufferedImage image, double angle) { 
    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); 
    GraphicsConfiguration gc = getDefaultConfiguration(); 
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT); 
    Graphics2D g = result.createGraphics(); 
    g.translate((neww - w)/2, (newh - h)/2); 
    g.rotate(angle, w/2, h/2); 
    g.drawRenderedImage(image, null); 
    g.dispose(); 
    return result; 
} 

private static GraphicsConfiguration getDefaultConfiguration() { 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice gd = ge.getDefaultScreenDevice(); 
    return gd.getDefaultConfiguration(); 
} 

de http://flyingdogz.wordpress.com/2008/02/11/image-rotate-in-java-2-easier-to-use/

+0

Esto es realmente genial. Sin embargo, estoy teniendo un problema. Mi imagen solo necesita girar 90 grados. a la vez Si paso en 90 para ángulo, realmente girará más de 90 grados – user489041

+1

Esto está usando radianes, así que solo use Math.toRadians (grado) primero. –

+0

Funcionó como un amuleto. ¡Gracias! – user489041

0

¿Ayuda si cambia:

BufferedImage DaImage = new BufferedImage(height, width, type);

a:

BufferedImage DaImage = new BufferedImage(**width, height**, type);?

+0

Sí, los cambié de uno a otro sin éxito. – user489041