简体   繁体   中英

Java rotated image turns all black?

I am making a basic java application and trying to rotate an image. I wrote the following quick method

private Image rotate(double degs){
    ImageIcon img = new ImageIcon("src/inc/img/char_male.png");
    Image temp = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) temp.getGraphics();
    g2.rotate(Math.toRadians(degs));
    g2.drawImage(img.getImage(), 0, 0, Color.WHITE, null);
    System.out.println("Rotating "+degs);
    g2.dispose();
    return temp;
}

The problem is when I run this and repaint the GUI, the image turns pure black. Am I doing something wrong with the BufferedImage creation? I am changing the GUI in the repaint using a JLabel,

label.setIcon(new ImageIcon(rotate(90)));

You need to rotate and translate at the same time so that the center of rotation is the center of the image. The AffineTransform rotate method has an overload for this as does the Graphics2D rotate method. For eg, what if you try,...

private Image rotate(double degs){
    ImageIcon img = new ImageIcon("src/inc/img/char_male.png"); // why an ImageIcon???
    Image temp = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) temp.getGraphics();
    g2.rotate(Math.toRadians(degs), img.getIconWidth()/2, img.getIconHeight()/2); // changed
    g2.drawImage(img.getImage(), 0, 0, Color.WHITE, null);
    System.out.println("Rotating "+degs);
    g2.dispose();
    return temp;
}

Sorry, here's the corrected code:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class ImageRotate {
   private static final String IMAGE_PATH = "src/yr2011/images/guitar_3406632_n.jpg";

   public static void main(String[] args) {
      try {
         BufferedImage image = ImageIO.read(new File(IMAGE_PATH));
         ImageIcon icon = new ImageIcon(image);
         JOptionPane.showMessageDialog(null, new JLabel(icon));

         icon = new ImageIcon(rotate(image, 90));
         JOptionPane.showMessageDialog(null, new JLabel(icon));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private static Image rotate(Image image, double degs) {
      int width = image.getWidth(null);
      int height = image.getHeight(null);
      BufferedImage temp = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = temp.createGraphics();
      g2.rotate(Math.toRadians(degs), height / 2, height / 2);
      g2.drawImage(image, 0, 0, Color.WHITE, null);
      g2.dispose();
      return temp;
   }
}

I don't use Java, so that I can't tell if there's something wrong with your code, but a common error when doing image rotation is rotating out of the viewport. When you imagine the original image sitting at the coordinate origin, rotating it by 90 degrees moves the image below the x-axis (or left of the y-axis, depending on the direction). In both cases the rotated image leaves the viewport and you get an empty image. The solution is to shift the rotated image back to place.

I had the same probleme and I solved it in an other way:

My picture had a transparent background but I didn't know it because I look it always on white background. Anyway, I solved my problem when I use

BufferedImage temp = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);

instead of

BufferedImage temp = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);

I think that graphics2d work with a black background if you don't precise transparent type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM